1 /* No user fns here. Pesch 15apr92. */
4 * Copyright (c) 1990, 2007 The Regents of the University of California.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * and/or other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 * Various output routines call wsetup to be sure it is safe to write,
28 * because either _flags does not include __SWR, or _buf is NULL.
29 * _wsetup returns 0 if OK to write, nonzero and set errno otherwise.
33 __swsetup_r (struct _reent
*ptr
,
36 /* Make sure stdio is set up. */
38 CHECK_INIT (_REENT
, fp
);
41 * If we are not writing, we had better be reading and writing.
44 if ((fp
->_flags
& __SWR
) == 0)
46 if ((fp
->_flags
& __SRW
) == 0)
48 _REENT_ERRNO(ptr
) = EBADF
;
52 if (fp
->_flags
& __SRD
)
54 /* clobber any ungetc data */
57 fp
->_flags
&= ~(__SRD
| __SEOF
);
59 fp
->_p
= fp
->_bf
._base
;
65 * Make a buffer if necessary, then set _w.
66 * A string I/O file should not explicitly allocate a buffer
67 * unless asprintf is being used.
69 if (fp
->_bf
._base
== NULL
70 && (!(fp
->_flags
& __SSTR
) || (fp
->_flags
& __SMBF
)))
71 __smakebuf_r (ptr
, fp
);
73 if (fp
->_flags
& __SLBF
)
76 * It is line buffered, so make _lbfsize be -_bufsize
77 * for the putc() macro. We will change _lbfsize back
78 * to 0 whenever we turn off __SWR.
81 fp
->_lbfsize
= -fp
->_bf
._size
;
84 fp
->_w
= fp
->_flags
& __SNBF
? 0 : fp
->_bf
._size
;
86 if (!fp
->_bf
._base
&& (fp
->_flags
& __SMBF
))
88 /* __smakebuf_r set errno, but not flag */