1*46111Sbostic /*- 2*46111Sbostic * Copyright (c) 1990 The Regents of the University of California. 3*46111Sbostic * All rights reserved. 4*46111Sbostic * 5*46111Sbostic * This code is derived from software contributed to Berkeley by 6*46111Sbostic * Chris Torek. 7*46111Sbostic * 8*46111Sbostic * %sccs.include.redist.c% 9*46111Sbostic */ 10*46111Sbostic 11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*46111Sbostic static char sccsid[] = "@(#)wsetup.c 5.1 (Berkeley) 01/20/91"; 13*46111Sbostic #endif /* LIBC_SCCS and not lint */ 14*46111Sbostic 15*46111Sbostic #include <stdio.h> 16*46111Sbostic #include "local.h" 17*46111Sbostic 18*46111Sbostic /* 19*46111Sbostic * Various output routines call wsetup to be sure it is safe to write, 20*46111Sbostic * because either _flags does not include __SWR, or _buf is NULL. 21*46111Sbostic * _wsetup returns 0 if OK to write, nonzero otherwise. 22*46111Sbostic */ 23*46111Sbostic __swsetup(fp) 24*46111Sbostic register FILE *fp; 25*46111Sbostic { 26*46111Sbostic /* make sure stdio is set up */ 27*46111Sbostic if (!__sdidinit) 28*46111Sbostic __sinit(); 29*46111Sbostic 30*46111Sbostic /* 31*46111Sbostic * If we are not writing, we had better be reading and writing. 32*46111Sbostic */ 33*46111Sbostic if ((fp->_flags & __SWR) == 0) { 34*46111Sbostic if ((fp->_flags & __SRW) == 0) 35*46111Sbostic return (EOF); 36*46111Sbostic if (fp->_flags & __SRD) { 37*46111Sbostic /* clobber any ungetc data */ 38*46111Sbostic if (HASUB(fp)) 39*46111Sbostic FREEUB(fp); 40*46111Sbostic fp->_flags &= ~(__SRD|__SEOF); 41*46111Sbostic fp->_r = 0; 42*46111Sbostic fp->_p = fp->_bf._base; 43*46111Sbostic } 44*46111Sbostic fp->_flags |= __SWR; 45*46111Sbostic } 46*46111Sbostic 47*46111Sbostic /* 48*46111Sbostic * Make a buffer if necessary, then set _w. 49*46111Sbostic */ 50*46111Sbostic if (fp->_bf._base == NULL) 51*46111Sbostic __smakebuf(fp); 52*46111Sbostic if (fp->_flags & __SLBF) { 53*46111Sbostic /* 54*46111Sbostic * It is line buffered, so make _lbfsize be -_bufsize 55*46111Sbostic * for the putc() macro. We will change _lbfsize back 56*46111Sbostic * to 0 whenever we turn off __SWR. 57*46111Sbostic */ 58*46111Sbostic fp->_w = 0; 59*46111Sbostic fp->_lbfsize = -fp->_bf._size; 60*46111Sbostic } else 61*46111Sbostic fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size; 62*46111Sbostic return (0); 63*46111Sbostic } 64