1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)fread.c 8.2 (Berkeley) 12/11/93";
13 #endif /* LIBC_SCCS and not lint */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 size_t
fread(buf,size,count,fp)19 fread(buf, size, count, fp)
20 void *buf;
21 size_t size, count;
22 register FILE *fp;
23 {
24 register size_t resid;
25 register char *p;
26 register int r;
27 size_t total;
28
29 /*
30 * The ANSI standard requires a return value of 0 for a count
31 * or a size of 0. Peculiarily, it imposes no such requirements
32 * on fwrite; it only requires fread to be broken.
33 */
34 if ((resid = count * size) == 0)
35 return (0);
36 if (fp->_r < 0)
37 fp->_r = 0;
38 total = resid;
39 p = buf;
40 while (resid > (r = fp->_r)) {
41 (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
42 fp->_p += r;
43 /* fp->_r = 0 ... done in __srefill */
44 p += r;
45 resid -= r;
46 if (__srefill(fp)) {
47 /* no more input: return partial result */
48 return ((total - resid) / size);
49 }
50 }
51 (void)memcpy((void *)p, (void *)fp->_p, resid);
52 fp->_r -= resid;
53 fp->_p += resid;
54 return (count);
55 }
56