/*
 * pure-purfile.c : file putting routine for PURE network service.
 *
 * $Id: pure-putfile.c,v 1.1 2000/12/30 16:31:42 simm Exp $
 */

#include "pure.h"
#define BUFSIZE (4096)

int pure_open_putfile( dcc_file* file )
{
	/* open file */
	if ( ( file->handle = open( file->name, O_RDONLY | O_BINARY ) ) < 0 )
		return -1;
	/* get size and set position */
	file->size = lseek( file->handle, 0, SEEK_END );
	return 0;
}

int pure_putfile( dcc_file* file, net_peer* peer )
{
	int len, pos, old, new;
	u_long report;
	char buf[BUFSIZE];

	old = -1;
	pos  = lseek( file->handle, file->offset, SEEK_SET );
	while ( ( len = read( file->handle, buf, BUFSIZE ) ) > 0 ) {
		write( peer->handle, buf, len );
		pos += len;
		while ( read( peer->handle, &report, sizeof(u_long)) > 0
			&& ntohl( report ) < pos )
			;
		new = 100 * pos / file->size;
		if ( old / 10 < new / 10 ) {
			printf( "[REPORT] %d %d %d\n", new, pos, file->size );
			old = new;
		}
	}
	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1