File:  [Power 6/32 Unix Tahoe 4.2BSD] / cci / usr / src / lib / libc / stdio / rdwr.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Sun Jul 28 12:25:23 2019 UTC (7 years ago) by root
Branches: bsd, MAIN
CVS tags: v121, HEAD
Power 6/32 Unix version 1.21

/*
 * Modified by Gershon Shamay, 31 March 1986. To move whole
 * strings at a time in 'fread' instead of a character at a time
 * which is killing us for many bytes/call.
 */
#include	<stdio.h>

#define	MIN(a,b)	((a)<(b) ? (a):(b))

fread(ptr, size, count, iop)
register unsigned size;
register char *ptr;
register FILE *iop;
{
	register int chars_to_read = size * count;
	register int chars_read = 0;
	register int this_transfer;

/*
 * easy case: data to be read is already in file buffer.
 */
	if (chars_to_read <= iop->_cnt) {
		bcopy(iop->_ptr, ptr, chars_to_read);
		iop->_ptr += chars_to_read;
		iop->_cnt -= chars_to_read;
		return(count);
	}
/*
 * else we have to sweat a bit ....
 */
	while (chars_read < chars_to_read) {
		if (iop->_cnt <= 0) {
			if (_filbuf(iop) < 0 ) /* Out of input data */
				return(chars_read/size);
			else {
				iop->_cnt++;  /* Put back first char */
				iop->_ptr--;
			}
		}
		this_transfer = MIN(iop->_cnt, chars_to_read - chars_read);
		bcopy(iop->_ptr, ptr, this_transfer);
		chars_read += this_transfer;
		ptr += this_transfer;
		iop->_ptr += this_transfer;
		iop->_cnt -= this_transfer;
	}
	return(chars_read/size);
}

fwrite(ptr, size, count, iop)
register unsigned size, count;
register char *ptr;
register FILE *iop;
{
	register int chars_to_write = size * count;
	register int chars_written = 0;
	register int this_transfer;

/*
 * easy case: check if there's enough room in buffer for entire
 * 		operation.
 */
	if (chars_to_write <= iop->_cnt) {
		bcopy(ptr, iop->_ptr, chars_to_write);
		iop->_ptr += chars_to_write;
		iop->_cnt -= chars_to_write;
		return(count);
	}
/*
 * else we have to sweat a bit ....
 */
	while (chars_written < chars_to_write) {
		if (iop->_cnt <= 0) {
			_flsbuf((unsigned)*ptr, iop);	/* Full buffer */
			if (ferror(iop))
				break;
			else {
				iop->_cnt++;
				iop->_ptr--;
			}
		}
		this_transfer = MIN(iop->_cnt, chars_to_write - chars_written);
		bcopy(ptr, iop->_ptr, this_transfer);
		chars_written += this_transfer;
		ptr += this_transfer;
		iop->_ptr += this_transfer;
		iop->_cnt -= this_transfer;
	}
	return(chars_written/size);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.