xref: /original-bsd/lib/libc/stdio/fread.c (revision 40192f2d)
1 /* @(#)fread.c	4.3 (Berkeley) 05/14/85 */
2 #include	<stdio.h>
3 
4 fread(ptr, size, count, iop)
5 	register char *ptr;
6 	unsigned size, count;
7 	register FILE *iop;
8 {
9 	register int s;
10 	int c;
11 
12 	s = size * count;
13 	while (s > 0) {
14 		if (iop->_cnt < s) {
15 			if (iop->_cnt > 0) {
16 				bcopy(iop->_ptr, ptr, iop->_cnt);
17 				ptr += iop->_cnt;
18 				s -= iop->_cnt;
19 			}
20 			/*
21 			 * filbuf clobbers _cnt & _ptr,
22 			 * so don't waste time setting them.
23 			 */
24 			if ((c = _filbuf(iop)) == EOF)
25 				break;
26 			*ptr++ = c;
27 			s--;
28 		}
29 		if (iop->_cnt >= s) {
30 			bcopy(iop->_ptr, ptr, s);
31 			iop->_ptr += s;
32 			iop->_cnt -= s;
33 			return (count);
34 		}
35 	}
36 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
37 }
38