xref: /csrg-svn/lib/libc/stdio/wsetup.c (revision 61180)
146111Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)wsetup.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <stdio.h>
1646611Sbostic #include <stdlib.h>
1746111Sbostic #include "local.h"
1846111Sbostic 
1946111Sbostic /*
2046111Sbostic  * Various output routines call wsetup to be sure it is safe to write,
2146111Sbostic  * because either _flags does not include __SWR, or _buf is NULL.
2246111Sbostic  * _wsetup returns 0 if OK to write, nonzero otherwise.
2346111Sbostic  */
__swsetup(fp)2446111Sbostic __swsetup(fp)
2546111Sbostic 	register FILE *fp;
2646111Sbostic {
2746111Sbostic 	/* make sure stdio is set up */
2846111Sbostic 	if (!__sdidinit)
2946111Sbostic 		__sinit();
3046111Sbostic 
3146111Sbostic 	/*
3246111Sbostic 	 * If we are not writing, we had better be reading and writing.
3346111Sbostic 	 */
3446111Sbostic 	if ((fp->_flags & __SWR) == 0) {
3546111Sbostic 		if ((fp->_flags & __SRW) == 0)
3646111Sbostic 			return (EOF);
3746111Sbostic 		if (fp->_flags & __SRD) {
3846111Sbostic 			/* clobber any ungetc data */
3946111Sbostic 			if (HASUB(fp))
4046111Sbostic 				FREEUB(fp);
4146111Sbostic 			fp->_flags &= ~(__SRD|__SEOF);
4246111Sbostic 			fp->_r = 0;
4346111Sbostic 			fp->_p = fp->_bf._base;
4446111Sbostic 		}
4546111Sbostic 		fp->_flags |= __SWR;
4646111Sbostic 	}
4746111Sbostic 
4846111Sbostic 	/*
4946111Sbostic 	 * Make a buffer if necessary, then set _w.
5046111Sbostic 	 */
5146111Sbostic 	if (fp->_bf._base == NULL)
5246111Sbostic 		__smakebuf(fp);
5346111Sbostic 	if (fp->_flags & __SLBF) {
5446111Sbostic 		/*
5546111Sbostic 		 * It is line buffered, so make _lbfsize be -_bufsize
5646111Sbostic 		 * for the putc() macro.  We will change _lbfsize back
5746111Sbostic 		 * to 0 whenever we turn off __SWR.
5846111Sbostic 		 */
5946111Sbostic 		fp->_w = 0;
6046111Sbostic 		fp->_lbfsize = -fp->_bf._size;
6146111Sbostic 	} else
6246111Sbostic 		fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size;
6346111Sbostic 	return (0);
6446111Sbostic }
65