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*49105Sbostic static char sccsid[] = "@(#)fread.c 5.4 (Berkeley) 05/04/91"; 1346084Sbostic #endif /* LIBC_SCCS and not lint */ 1421406Sdist 1546084Sbostic #include <stdio.h> 1646084Sbostic #include <string.h> 1716549Skarels 1846084Sbostic fread(buf, size, count, fp) 1946084Sbostic void *buf; 2046084Sbostic size_t size, count; 2146084Sbostic register FILE *fp; 2216549Skarels { 2346084Sbostic register size_t resid; 2446084Sbostic register char *p; 2546084Sbostic register int r; 2646084Sbostic size_t total; 2716549Skarels 2846084Sbostic if ((resid = count * size) == 0) 2946084Sbostic return (count); 3046084Sbostic if (fp->_r < 0) 3146084Sbostic fp->_r = 0; 3246084Sbostic total = resid; 3346084Sbostic p = buf; 3446084Sbostic while (resid > (r = fp->_r)) { 35*49105Sbostic (void) bcopy((void *)fp->_p, (void *)p, (size_t)r); 3646084Sbostic fp->_p += r; 3746084Sbostic /* fp->_r = 0 ... done in __srefill */ 3846084Sbostic p += r; 3946084Sbostic resid -= r; 4046084Sbostic if (__srefill(fp)) { 4146084Sbostic /* no more input: return partial result */ 4246084Sbostic return ((total - resid) / size); 4316549Skarels } 4416549Skarels } 45*49105Sbostic (void) bcopy((void *)fp->_p, (void *)p, resid); 4646084Sbostic fp->_r -= resid; 4746084Sbostic fp->_p += resid; 4846084Sbostic return (count); 4916549Skarels } 50