Update contrib.
3 * Portions Copyright (c) 1990-2008 Nokia Corporation and/or its subsidiary(-ies).
7 /* No user fns here. Pesch 15apr92. */
10 * Copyright (c) 1990 The Regents of the University of California.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms are permitted
14 * provided that the above copyright notice and this paragraph are
15 * duplicated in all such forms and that any documentation,
16 * advertising materials, and other materials related to such
17 * distribution and use acknowledge that the software was developed
18 * by the University of California, Berkeley. The name of the
19 * University may not be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32 /* Initialise a FILE structure to a starting state */
35 _std (FILE *ptr, int flags, int file, struct _reent *data)
38 std (FILE *ptr, int flags, int file, struct _reent *data)
44 ptr->_flags = (short)flags;
45 ptr->_file = (short)file;
50 ptr->_write = __swrite;
52 ptr->_close = __sclose;
57 __sfmoreglue (struct _reent *d, register int n)
62 g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
69 memset (p, 0, n * sizeof (FILE));
74 * Find a free FILE for fopen et al.
78 __sfp (struct _reent *d)
86 for (g = &d->__sglue;; g = g->_next)
88 for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
91 if (g->_next == NULL &&
92 (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
99 fp->_flags = 1; /* reserve this slot; caller sets real flags */
100 fp->_p = NULL; /* no current pointer */
101 fp->_w = 0; /* nothing to read or write */
103 fp->_bf._base = NULL; /* no buffer */
105 fp->_lbfsize = 0; /* not line buffered */
106 fp->_file = -1; /* no file */
107 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
108 fp->_ub._base = NULL; /* no ungetc buffer */
110 fp->_lb._base = NULL; /* no line buffer */
117 * exit() calls _cleanup() through *__cleanup, set whenever we
118 * open or buffer a file. This chicanery is done so that programs
119 * that do not use stdio need not link it all in.
121 * The name `_cleanup' is, alas, fairly well known outside stdio.
124 A reentrant version of _cleanup().
127 _cleanup_r (struct _reent *ptr)
129 (void) _fwalk(ptr, fclose);
145 * __sinit() is called whenever stdio's internal variables must be set up.
149 __sinit (struct _reent *s)
151 /* make sure we clean up on exit */
152 s->__cleanup = _cleanup_r; /* conservative */
156 _std (_stdin_r(s), __SRD, 0, s);
157 _std (_stdout_r(s), __SWR, 1, s);
158 _std (_stderr_r(s), __SWR | __SNBF, 2, s);
160 std (_stdin_r(s), __SRD, 0, s);
161 std (_stdout_r(s), __SWR, 1, s);
162 std (_stderr_r(s), __SWR | __SNBF, 2, s);
165 /* initialise the head of the glue chain to cover stdim/stdout/stderr */
166 s->__sglue._next = NULL;
167 s->__sglue._niobs = 3;
168 s->__sglue._iobs = &s->_sf[0];
171 /* Function interface to stdin/stdout/stderr
173 * These functions should return a value which is fixed for any given
174 * reentrancy context, but it doesn't have to be fixed at compile time,
175 * and we don't need to have initialised stdio either.
179 Function interface to the "constants" stdin.
180 These functions guarantee to return a fixed value, so that it
181 will be possible to use expressions such as if (fp != stdout) fclose(fp);
182 with complete confidence. Unfortunately it will rule out initialising global
183 variables with stdin/stdout/stderr, as in the common idiom:
185 static FILE *log = stderr;
187 This isn't currently possible with EPOC32.
189 EXPORT_C FILE *__stdin (void)
191 return _stdin_r(_REENT);
195 Function interface to the "constants" stdout
198 EXPORT_C FILE *__stdout (void)
200 return _stdout_r(_REENT);
204 Function interface to the "constants" stderr
207 EXPORT_C FILE *__stderr (void)
209 return _stderr_r(_REENT);