xref: /csrg-svn/lib/libc/stdio/fread.c (revision 65097)
146084Sbostic /*-
261180Sbostic  * Copyright (c) 1990, 1993
361180Sbostic  *	The Regents of the University of California.  All rights reserved.
446084Sbostic  *
546084Sbostic  * This code is derived from software contributed to Berkeley by
646084Sbostic  * Chris Torek.
746084Sbostic  *
846084Sbostic  * %sccs.include.redist.c%
921406Sdist  */
1021406Sdist 
1126651Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*65097Storek static char sccsid[] = "@(#)fread.c	8.2 (Berkeley) 12/11/93";
1346084Sbostic #endif /* LIBC_SCCS and not lint */
1421406Sdist 
1546084Sbostic #include <stdio.h>
1646084Sbostic #include <string.h>
1716549Skarels 
1850964Sbostic size_t
fread(buf,size,count,fp)1946084Sbostic fread(buf, size, count, fp)
2046084Sbostic 	void *buf;
2146084Sbostic 	size_t size, count;
2246084Sbostic 	register FILE *fp;
2316549Skarels {
2446084Sbostic 	register size_t resid;
2546084Sbostic 	register char *p;
2646084Sbostic 	register int r;
2746084Sbostic 	size_t total;
2816549Skarels 
29*65097Storek 	/*
30*65097Storek 	 * The ANSI standard requires a return value of 0 for a count
31*65097Storek 	 * or a size of 0.  Peculiarily, it imposes no such requirements
32*65097Storek 	 * on fwrite; it only requires fread to be broken.
33*65097Storek 	 */
3446084Sbostic 	if ((resid = count * size) == 0)
35*65097Storek 		return (0);
3646084Sbostic 	if (fp->_r < 0)
3746084Sbostic 		fp->_r = 0;
3846084Sbostic 	total = resid;
3946084Sbostic 	p = buf;
4046084Sbostic 	while (resid > (r = fp->_r)) {
4158451Storek 		(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
4246084Sbostic 		fp->_p += r;
4346084Sbostic 		/* fp->_r = 0 ... done in __srefill */
4446084Sbostic 		p += r;
4546084Sbostic 		resid -= r;
4646084Sbostic 		if (__srefill(fp)) {
4746084Sbostic 			/* no more input: return partial result */
4846084Sbostic 			return ((total - resid) / size);
4916549Skarels 		}
5016549Skarels 	}
5158451Storek 	(void)memcpy((void *)p, (void *)fp->_p, resid);
5246084Sbostic 	fp->_r -= resid;
5346084Sbostic 	fp->_p += resid;
5446084Sbostic 	return (count);
5516549Skarels }
56