xref: /csrg-svn/lib/libc/stdio/fwrite.c (revision 20196)
1 /* @(#)fwrite.c	4.5 (Berkeley) 05/14/85 */
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 	if (iop->_flag & _IOLBF)
13 		while (s > 0) {
14 			if (--iop->_cnt > -iop->_bufsiz && *ptr != '\n')
15 				*iop->_ptr++ = *ptr++;
16 			else if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
17 				break;
18 			s--;
19 		}
20 	else while (s > 0) {
21 		if (iop->_cnt < s) {
22 			if (iop->_cnt > 0) {
23 				bcopy(ptr, iop->_ptr, iop->_cnt);
24 				ptr += iop->_cnt;
25 				iop->_ptr += iop->_cnt;
26 				s -= iop->_cnt;
27 			}
28 			if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
29 				break;
30 			s--;
31 		}
32 		if (iop->_cnt >= s) {
33 			bcopy(ptr, iop->_ptr, s);
34 			iop->_ptr += s;
35 			iop->_cnt -= s;
36 			return (count);
37 		}
38 	}
39 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
40 }
41