Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / wsetup.c
blob0fd9690aa629d6fa164410785c04fcfa3ff9a60a
1 /* No user fns here. Pesch 15apr92. */
3 /*
4 * Copyright (c) 1990, 2007 The Regents of the University of California.
5 * All rights reserved.
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.
20 #include <_ansi.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include "local.h"
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.
32 int
33 __swsetup_r (struct _reent *ptr,
34 register FILE * fp)
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;
49 fp->_flags |= __SERR;
50 return EOF;
52 if (fp->_flags & __SRD)
54 /* clobber any ungetc data */
55 if (HASUB (fp))
56 FREEUB (ptr, fp);
57 fp->_flags &= ~(__SRD | __SEOF);
58 fp->_r = 0;
59 fp->_p = fp->_bf._base;
61 fp->_flags |= __SWR;
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.
80 fp->_w = 0;
81 fp->_lbfsize = -fp->_bf._size;
83 else
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 */
89 fp->_flags |= __SERR;
90 return EOF;
92 return 0;