xref: /csrg-svn/lib/libc/stdio/fread.c (revision 21406)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)fread.c	5.1 (Berkeley) 05/30/85";
9 #endif not lint
10 
11 #include	<stdio.h>
12 
13 fread(ptr, size, count, iop)
14 	register char *ptr;
15 	unsigned size, count;
16 	register FILE *iop;
17 {
18 	register int s;
19 	int c;
20 
21 	s = size * count;
22 	while (s > 0) {
23 		if (iop->_cnt < s) {
24 			if (iop->_cnt > 0) {
25 				bcopy(iop->_ptr, ptr, iop->_cnt);
26 				ptr += iop->_cnt;
27 				s -= iop->_cnt;
28 			}
29 			/*
30 			 * filbuf clobbers _cnt & _ptr,
31 			 * so don't waste time setting them.
32 			 */
33 			if ((c = _filbuf(iop)) == EOF)
34 				break;
35 			*ptr++ = c;
36 			s--;
37 		}
38 		if (iop->_cnt >= s) {
39 			bcopy(iop->_ptr, ptr, s);
40 			iop->_ptr += s;
41 			iop->_cnt -= s;
42 			return (count);
43 		}
44 	}
45 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
46 }
47