xref: /csrg-svn/lib/libc/stdio/fread.c (revision 58451)
146084Sbostic /*-
246084Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346084Sbostic  * 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*58451Storek static char sccsid[] = "@(#)fread.c	5.6 (Berkeley) 03/04/93";
1346084Sbostic #endif /* LIBC_SCCS and not lint */
1421406Sdist 
1546084Sbostic #include <stdio.h>
1646084Sbostic #include <string.h>
1716549Skarels 
1850964Sbostic size_t
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 
2946084Sbostic 	if ((resid = count * size) == 0)
3046084Sbostic 		return (count);
3146084Sbostic 	if (fp->_r < 0)
3246084Sbostic 		fp->_r = 0;
3346084Sbostic 	total = resid;
3446084Sbostic 	p = buf;
3546084Sbostic 	while (resid > (r = fp->_r)) {
36*58451Storek 		(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
3746084Sbostic 		fp->_p += r;
3846084Sbostic 		/* fp->_r = 0 ... done in __srefill */
3946084Sbostic 		p += r;
4046084Sbostic 		resid -= r;
4146084Sbostic 		if (__srefill(fp)) {
4246084Sbostic 			/* no more input: return partial result */
4346084Sbostic 			return ((total - resid) / size);
4416549Skarels 		}
4516549Skarels 	}
46*58451Storek 	(void)memcpy((void *)p, (void *)fp->_p, resid);
4746084Sbostic 	fp->_r -= resid;
4846084Sbostic 	fp->_p += resid;
4946084Sbostic 	return (count);
5016549Skarels }
51