xref: /csrg-svn/lib/libc/stdio/fwrite.c (revision 26655)
121408Sdist /*
221408Sdist  * Copyright (c) 1980 Regents of the University of California.
321408Sdist  * All rights reserved.  The Berkeley software License Agreement
421408Sdist  * specifies the terms and conditions for redistribution.
521408Sdist  */
621408Sdist 
7*26655Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*26655Sdonn static char sccsid[] = "@(#)fwrite.c	5.2 (Berkeley) 03/09/86";
9*26655Sdonn #endif LIBC_SCCS and not lint
1021408Sdist 
1116550Skarels #include	<stdio.h>
1216550Skarels 
1316550Skarels fwrite(ptr, size, count, iop)
1416550Skarels 	register char *ptr;
1516550Skarels 	unsigned size, count;
1616550Skarels 	register FILE *iop;
1716550Skarels {
1816550Skarels 	register int s;
1916550Skarels 
2016550Skarels 	s = size * count;
2117443Sralph 	if (iop->_flag & _IOLBF)
2217861Sralph 		while (s > 0) {
2317443Sralph 			if (--iop->_cnt > -iop->_bufsiz && *ptr != '\n')
2417443Sralph 				*iop->_ptr++ = *ptr++;
2517443Sralph 			else if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
2617443Sralph 				break;
2717861Sralph 			s--;
2817443Sralph 		}
2917443Sralph 	else while (s > 0) {
3016550Skarels 		if (iop->_cnt < s) {
3116550Skarels 			if (iop->_cnt > 0) {
3216550Skarels 				bcopy(ptr, iop->_ptr, iop->_cnt);
3316550Skarels 				ptr += iop->_cnt;
3416550Skarels 				iop->_ptr += iop->_cnt;
3516550Skarels 				s -= iop->_cnt;
3616550Skarels 			}
3717443Sralph 			if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
3816550Skarels 				break;
3916550Skarels 			s--;
4016550Skarels 		}
4116550Skarels 		if (iop->_cnt >= s) {
4216550Skarels 			bcopy(ptr, iop->_ptr, s);
4316550Skarels 			iop->_ptr += s;
4416550Skarels 			iop->_cnt -= s;
4516550Skarels 			return (count);
4616550Skarels 		}
4716550Skarels 	}
4820196Ssam 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
4916550Skarels }
50