xref: /csrg-svn/lib/libc/stdio/wbuf.c (revision 61180)
146075Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446075Sbostic  *
546075Sbostic  * This code is derived from software contributed to Berkeley by
646075Sbostic  * Chris Torek.
746075Sbostic  *
846075Sbostic  * %sccs.include.redist.c%
921403Sdist  */
1021403Sdist 
1126646Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)wbuf.c	8.1 (Berkeley) 06/04/93";
1346075Sbostic #endif /* LIBC_SCCS and not lint */
1421403Sdist 
1546075Sbostic #include <stdio.h>
1646075Sbostic #include "local.h"
172003Swnj 
1846075Sbostic /*
1946075Sbostic  * Write the given character into the (probably full) buffer for
2046075Sbostic  * the given file.  Flush the buffer out if it is or becomes full,
2146075Sbostic  * or if c=='\n' and the file is line buffered.
2246075Sbostic  */
__swbuf(c,fp)2346075Sbostic __swbuf(c, fp)
2446075Sbostic 	register int c;
2546075Sbostic 	register FILE *fp;
262003Swnj {
2746075Sbostic 	register int n;
282003Swnj 
2946075Sbostic 	/*
3046075Sbostic 	 * In case we cannot write, or longjmp takes us out early,
3146075Sbostic 	 * make sure _w is 0 (if fully- or un-buffered) or -_bf._size
3246075Sbostic 	 * (if line buffered) so that we will get called again.
3346075Sbostic 	 * If we did not do this, a sufficient number of putc()
3446075Sbostic 	 * calls might wrap _w from negative to positive.
3546075Sbostic 	 */
3646075Sbostic 	fp->_w = fp->_lbfsize;
3746075Sbostic 	if (cantwrite(fp))
3846075Sbostic 		return (EOF);
3946075Sbostic 	c = (unsigned char)c;
403163Stoy 
4146075Sbostic 	/*
4246075Sbostic 	 * If it is completely full, flush it out.  Then, in any case,
4346075Sbostic 	 * stuff c into the buffer.  If this causes the buffer to fill
4446075Sbostic 	 * completely, or if c is '\n' and the file is line buffered,
4546075Sbostic 	 * flush it (perhaps a second time).  The second flush will always
4646075Sbostic 	 * happen on unbuffered streams, where _bf._size==1; fflush()
4746075Sbostic 	 * guarantees that putc() will always call wbuf() by setting _w
4846075Sbostic 	 * to 0, so we need not do anything else.
4946075Sbostic 	 */
5046075Sbostic 	n = fp->_p - fp->_bf._base;
5146075Sbostic 	if (n >= fp->_bf._size) {
5246075Sbostic 		if (fflush(fp))
5346075Sbostic 			return (EOF);
5446075Sbostic 		n = 0;
552003Swnj 	}
5646075Sbostic 	fp->_w--;
5746075Sbostic 	*fp->_p++ = c;
5846075Sbostic 	if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
5946075Sbostic 		if (fflush(fp))
6046075Sbostic 			return (EOF);
6146075Sbostic 	return (c);
622003Swnj }
63