xref: /csrg-svn/lib/libc/stdio/fread.c (revision 46084)
1*46084Sbostic /*-
2*46084Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46084Sbostic  * All rights reserved.
4*46084Sbostic  *
5*46084Sbostic  * This code is derived from software contributed to Berkeley by
6*46084Sbostic  * Chris Torek.
7*46084Sbostic  *
8*46084Sbostic  * %sccs.include.redist.c%
921406Sdist  */
1021406Sdist 
1126651Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*46084Sbostic static char sccsid[] = "@(#)fread.c	5.3 (Berkeley) 01/20/91";
13*46084Sbostic #endif /* LIBC_SCCS and not lint */
1421406Sdist 
15*46084Sbostic #include <stdio.h>
16*46084Sbostic #include <string.h>
1716549Skarels 
18*46084Sbostic fread(buf, size, count, fp)
19*46084Sbostic 	void *buf;
20*46084Sbostic 	size_t size, count;
21*46084Sbostic 	register FILE *fp;
2216549Skarels {
23*46084Sbostic 	register size_t resid;
24*46084Sbostic 	register char *p;
25*46084Sbostic 	register int r;
26*46084Sbostic 	size_t total;
2716549Skarels 
28*46084Sbostic 	if ((resid = count * size) == 0)
29*46084Sbostic 		return (count);
30*46084Sbostic 	if (fp->_r < 0)
31*46084Sbostic 		fp->_r = 0;
32*46084Sbostic 	total = resid;
33*46084Sbostic 	p = buf;
34*46084Sbostic 	while (resid > (r = fp->_r)) {
35*46084Sbostic 		(void) memcpy((void *)p, (void *)fp->_p, (size_t)r);
36*46084Sbostic 		fp->_p += r;
37*46084Sbostic 		/* fp->_r = 0 ... done in __srefill */
38*46084Sbostic 		p += r;
39*46084Sbostic 		resid -= r;
40*46084Sbostic 		if (__srefill(fp)) {
41*46084Sbostic 			/* no more input: return partial result */
42*46084Sbostic 			return ((total - resid) / size);
4316549Skarels 		}
4416549Skarels 	}
45*46084Sbostic 	(void) memcpy((void *)p, (void *)fp->_p, resid);
46*46084Sbostic 	fp->_r -= resid;
47*46084Sbostic 	fp->_p += resid;
48*46084Sbostic 	return (count);
4916549Skarels }
50