1*46075Sbostic /*- 2*46075Sbostic * Copyright (c) 1990 The Regents of the University of California. 3*46075Sbostic * All rights reserved. 4*46075Sbostic * 5*46075Sbostic * This code is derived from software contributed to Berkeley by 6*46075Sbostic * Chris Torek. 7*46075Sbostic * 8*46075Sbostic * %sccs.include.redist.c% 921403Sdist */ 1021403Sdist 1126646Sdonn #if defined(LIBC_SCCS) && !defined(lint) 12*46075Sbostic static char sccsid[] = "@(#)wbuf.c 5.6 (Berkeley) 01/20/91"; 13*46075Sbostic #endif /* LIBC_SCCS and not lint */ 1421403Sdist 15*46075Sbostic #include <stdio.h> 16*46075Sbostic #include "local.h" 172003Swnj 18*46075Sbostic /* 19*46075Sbostic * Write the given character into the (probably full) buffer for 20*46075Sbostic * the given file. Flush the buffer out if it is or becomes full, 21*46075Sbostic * or if c=='\n' and the file is line buffered. 22*46075Sbostic */ 23*46075Sbostic __swbuf(c, fp) 24*46075Sbostic register int c; 25*46075Sbostic register FILE *fp; 262003Swnj { 27*46075Sbostic register int n; 282003Swnj 29*46075Sbostic /* 30*46075Sbostic * In case we cannot write, or longjmp takes us out early, 31*46075Sbostic * make sure _w is 0 (if fully- or un-buffered) or -_bf._size 32*46075Sbostic * (if line buffered) so that we will get called again. 33*46075Sbostic * If we did not do this, a sufficient number of putc() 34*46075Sbostic * calls might wrap _w from negative to positive. 35*46075Sbostic */ 36*46075Sbostic fp->_w = fp->_lbfsize; 37*46075Sbostic if (cantwrite(fp)) 38*46075Sbostic return (EOF); 39*46075Sbostic c = (unsigned char)c; 403163Stoy 41*46075Sbostic /* 42*46075Sbostic * If it is completely full, flush it out. Then, in any case, 43*46075Sbostic * stuff c into the buffer. If this causes the buffer to fill 44*46075Sbostic * completely, or if c is '\n' and the file is line buffered, 45*46075Sbostic * flush it (perhaps a second time). The second flush will always 46*46075Sbostic * happen on unbuffered streams, where _bf._size==1; fflush() 47*46075Sbostic * guarantees that putc() will always call wbuf() by setting _w 48*46075Sbostic * to 0, so we need not do anything else. 49*46075Sbostic */ 50*46075Sbostic n = fp->_p - fp->_bf._base; 51*46075Sbostic if (n >= fp->_bf._size) { 52*46075Sbostic if (fflush(fp)) 53*46075Sbostic return (EOF); 54*46075Sbostic n = 0; 552003Swnj } 56*46075Sbostic fp->_w--; 57*46075Sbostic *fp->_p++ = c; 58*46075Sbostic if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) 59*46075Sbostic if (fflush(fp)) 60*46075Sbostic return (EOF); 61*46075Sbostic return (c); 622003Swnj } 63