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