xref: /original-bsd/lib/libc/stdio/fwrite.c (revision f82e54c4)
1 /* @(#)fwrite.c	4.2 (Berkeley) 08/03/84 */
2 #include	<stdio.h>
3 
4 fwrite(ptr, size, count, iop)
5 	register char *ptr;
6 	unsigned size, count;
7 	register FILE *iop;
8 {
9 	register int s;
10 
11 	s = size * count;
12 	while (s > 0) {
13 		if (iop->_cnt < s) {
14 			if (iop->_cnt > 0) {
15 				bcopy(ptr, iop->_ptr, iop->_cnt);
16 				ptr += iop->_cnt;
17 				iop->_ptr += iop->_cnt;
18 				s -= iop->_cnt;
19 			}
20 			if (_flsbuf((unsigned)*ptr++ & 0377, iop) == EOF)
21 				break;
22 			s--;
23 		}
24 		if (iop->_cnt >= s) {
25 			bcopy(ptr, iop->_ptr, s);
26 			iop->_ptr += s;
27 			iop->_cnt -= s;
28 			return (count);
29 		}
30 	}
31 	return (count - ((s + size - 1) / size));
32 }
33