1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
11 /* This is pretty much straight from uClibc, but with one important
14 * We now initialize the locking flag to user locking instead of
15 * auto locking (i.e. FSETLOCKING_BYCALLER vs FSETLOCKING_INTERNAL).
16 * This greatly benefits non-threading applications linked to a
17 * shared thread-enabled library. In threading applications, we
18 * walk the stdio open file list and reset the locking mode
19 * appropriately when the thread subsystem is initialized.
22 /**********************************************************************/
24 #ifdef __UCLIBC_HAS_WCHAR__
25 #define __STDIO_FILE_INIT_WUNGOT { 0, 0 },
27 #define __STDIO_FILE_INIT_WUNGOT
30 #ifdef __STDIO_GETC_MACRO
31 # define __STDIO_FILE_INIT_BUFGETC(x) x,
33 # define __STDIO_FILE_INIT_BUFGETC(x)
36 #ifdef __STDIO_PUTC_MACRO
37 # define __STDIO_FILE_INIT_BUFPUTC(x) x,
39 # define __STDIO_FILE_INIT_BUFPUTC(x)
42 #ifdef __STDIO_HAS_OPENLIST
43 #define __STDIO_FILE_INIT_NEXT(next) (next),
45 #define __STDIO_FILE_INIT_NEXT(next)
48 #ifdef __STDIO_BUFFERS
49 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
50 (buf), (buf)+(bufsize), (buf), (buf),
52 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
55 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
56 #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
57 &((stream).__filedes), { _cs_read, _cs_write, _cs_seek, _cs_close },
59 #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
62 #ifdef __STDIO_MBSTATE
63 #define __STDIO_FILE_INIT_MBSTATE \
66 #define __STDIO_FILE_INIT_MBSTATE
69 #ifdef __UCLIBC_HAS_XLOCALE__
70 #define __STDIO_FILE_INIT_UNUSED \
73 #define __STDIO_FILE_INIT_UNUSED
76 #ifdef __UCLIBC_HAS_THREADS__
77 #ifdef __USE_STDIO_FUTEXES__
78 #define __STDIO_FILE_INIT_THREADSAFE \
79 2, _LIBC_LOCK_RECURSIVE_INITIALIZER,
81 #define __STDIO_FILE_INIT_THREADSAFE \
82 2, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
85 #define __STDIO_FILE_INIT_THREADSAFE
88 #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
90 { 0, 0 }, /* ungot[2] (no wchar) or ungot_width[2] (wchar)*/ \
92 __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
93 __STDIO_FILE_INIT_BUFGETC((buf)) \
94 __STDIO_FILE_INIT_BUFPUTC((buf)) \
95 __STDIO_FILE_INIT_NEXT(next) \
96 __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
97 __STDIO_FILE_INIT_WUNGOT \
98 __STDIO_FILE_INIT_MBSTATE \
99 __STDIO_FILE_INIT_UNUSED \
100 __STDIO_FILE_INIT_THREADSAFE \
101 } /* TODO: builtin buf */
103 /**********************************************************************/
104 /* First we need the standard files. */
106 #ifdef __STDIO_BUFFERS
107 static unsigned char _fixed_buffers[2 * BUFSIZ];
110 static FILE _stdio_streams[] = {
111 __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], \
112 __FLAG_LBF|__FLAG_READONLY, \
114 _stdio_streams + 1, \
117 __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], \
118 __FLAG_LBF|__FLAG_WRITEONLY, \
120 _stdio_streams + 2, \
121 _fixed_buffers + BUFSIZ, \
123 __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], \
124 __FLAG_NBF|__FLAG_WRITEONLY, \
131 FILE *stdin = _stdio_streams;
132 FILE *stdout = _stdio_streams + 1;
133 FILE *stderr = _stdio_streams + 2;
135 #ifdef __STDIO_GETC_MACRO
136 FILE *__stdin = _stdio_streams; /* For getchar() macro. */
138 #ifdef __STDIO_PUTC_MACRO
139 FILE *__stdout = _stdio_streams + 1; /* For putchar() macro. */
140 /* FILE *__stderr = _stdio_streams + 2; */
143 /**********************************************************************/
144 #ifdef __STDIO_HAS_OPENLIST
146 /* In certain configurations, we need to keep a list of open files.
147 * 1) buffering enabled - We need to initialize the buffering mode
148 * (full or line buffering) of stdin and stdout. We also
149 * need to flush all write buffers prior to normal termination.
150 * 2) custom streams - Even if we aren't buffering in the library
151 * itself, we need to fclose() all custom streams when terminating
152 * so that any special cleanup is done.
153 * 3) threads enabled - We need to be able to reset the locking mode
154 * of all open streams when the threading system is initialized.
157 FILE *_stdio_openlist = _stdio_streams;
159 # ifdef __UCLIBC_HAS_THREADS__
160 __UCLIBC_IO_MUTEX_INIT(_stdio_openlist_add_lock);
161 # ifdef __STDIO_BUFFERS
162 __UCLIBC_IO_MUTEX_INIT(_stdio_openlist_del_lock);
163 volatile int _stdio_openlist_use_count = 0;
164 int _stdio_openlist_del_count = 0;
168 /**********************************************************************/
169 #ifdef __UCLIBC_HAS_THREADS__
171 /* 2 if threading not initialized and 0 otherwise; */
172 int _stdio_user_locking = 2;
174 #ifndef __USE_STDIO_FUTEXES__
175 void attribute_hidden __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
177 const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
178 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
180 memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
185 /**********************************************************************/
187 /* We assume here that we are the only remaining thread. */
188 void _stdio_term(void)
190 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
193 #ifdef __UCLIBC_HAS_THREADS__
194 /* First, make sure the open file list is unlocked. If it was
195 * locked, then I suppose there is a chance that a pointer in the
196 * chain might be corrupt due to a partial store.
198 STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
200 #ifdef __STDIO_BUFFERS
201 STDIO_INIT_MUTEX(_stdio_openlist_del_lock);
204 /* Next we need to worry about the streams themselves. If a stream
205 * is currently locked, then it may be in an invalid state. So we
206 * 'disable' it in case a custom stream is stacked on top of it.
207 * Then we reinitialize the locks.
209 for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
210 if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
211 /* The stream is already locked, so we don't want to touch it.
212 * However, if we have custom streams, we can't just close it
213 * or leave it locked since a custom stream may be stacked
214 * on top of it. So we do unlock it, while also disabling it.
216 ptr->__modeflags = (__FLAG_READONLY|__FLAG_WRITEONLY);
217 __STDIO_STREAM_DISABLE_GETC(ptr);
218 __STDIO_STREAM_DISABLE_PUTC(ptr);
219 __STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
222 ptr->__user_locking = 1; /* Set locking mode to "by caller". */
223 STDIO_INIT_MUTEX(ptr->__lock); /* Shouldn't be necessary, but... */
227 /* Finally, flush all writing streams and shut down all custom streams.
228 * NOTE: We assume that any stacking by custom streams is done on top
229 * of streams previously allocated, and hence further down the
230 * list. Otherwise we have no way of knowing the order in which
232 * Remember that freopen() counts as a new allocation here, even
233 * though the stream is reused. That's because it moves the
234 * stream to the head of the list.
236 for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
237 #ifdef __STDIO_BUFFERS
238 /* Write any pending buffered chars. */
239 if (__STDIO_STREAM_IS_WRITING(ptr)) {
240 __STDIO_COMMIT_WRITE_BUFFER(ptr);
243 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
244 /* Actually close all custom streams to perform any special cleanup. */
245 if (ptr->__cookie != &ptr->__filedes) {
254 #if defined __STDIO_BUFFERS || !defined __UCLIBC__
255 void attribute_hidden _stdio_init(void)
257 #ifdef __STDIO_BUFFERS
258 int old_errno = errno;
259 /* stdin and stdout uses line buffering when connected to a tty. */
261 _stdio_streams[0].__modeflags ^= __FLAG_LBF;
263 _stdio_streams[1].__modeflags ^= __FLAG_LBF;
264 __set_errno(old_errno);
267 /* _stdio_term is done automatically when exiting if stdio is used.
268 * See misc/internals/__uClibc_main.c and and stdlib/atexit.c. */
274 /**********************************************************************/
276 #if !(__MASK_READING & __FLAG_UNGOT)
277 #error Assumption violated about __MASK_READING and __FLAG_UNGOT
280 #ifdef __UCLIBC_HAS_THREADS__
286 void attribute_hidden _stdio_validate_FILE(const FILE *stream)
288 #ifdef __UCLIBC_HAS_THREADS__
289 assert(((unsigned int)(stream->__user_locking)) <= 2);
292 #warning Define a constant for minimum possible valid __filedes?
293 assert(stream->__filedes >= -3);
295 if (stream->__filedes < 0) {
296 /* assert((stream->__filedes != -1) */
297 /* #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
298 /* || (stream->__cookie == &stream->__filedes) /\* custom *\/ */
301 /* assert((stream->__filedes == -1) || __STDIO_STREAM_IS_FBF(stream)); */
303 assert(!__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)
304 || __STDIO_STREAM_IS_NARROW(stream));
305 assert(!__STDIO_STREAM_IS_FAKE_VSSCANF(stream)
306 || __STDIO_STREAM_IS_NARROW(stream));
307 #ifdef __STDIO_STREAM_IS_FAKE_VSWPRINTF
308 assert(!__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)
309 || __STDIO_STREAM_IS_WIDE(stream));
311 #ifdef __STDIO_STREAM_IS_FAKE_VSWSCANF
312 assert(!__STDIO_STREAM_IS_FAKE_VSWSCANF(stream)
313 || __STDIO_STREAM_IS_WIDE(stream));
317 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
318 if (stream->__cookie != &stream->__filedes) { /* custom */
319 assert(stream->__filedes == -1);
323 /* Can not be both narrow and wide oriented at the same time. */
324 assert(!(__STDIO_STREAM_IS_NARROW(stream)
325 && __STDIO_STREAM_IS_WIDE(stream)));
328 /* The following impossible case is used to disable a stream. */
329 if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
330 == (__FLAG_READONLY|__FLAG_WRITEONLY)
332 assert(stream->__modeflags == (__FLAG_READONLY|__FLAG_WRITEONLY));
333 assert(stream->__filedes == -1);
334 #ifdef __STDIO_BUFFERS
335 assert(stream->__bufpos == stream->__bufstart);
336 assert(stream->__bufread == stream->__bufstart);
337 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
338 assert(stream->__bufputc_u == stream->__bufstart);
340 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
341 assert(stream->__bufgetc_u == stream->__bufstart);
346 if (__STDIO_STREAM_IS_READONLY(stream)) {
347 /* assert(!__STDIO_STREAM_IS_WRITEONLY(stream)); */
348 assert(!__STDIO_STREAM_IS_WRITING(stream));
349 if (stream->__modeflags & __FLAG_UNGOT) {
350 assert(((unsigned)(stream->__ungot[1])) <= 1);
351 assert(!__FEOF_UNLOCKED(stream));
355 if (__STDIO_STREAM_IS_WRITEONLY(stream)) {
356 /* assert(!__STDIO_STREAM_IS_READONLY(stream)); */
357 assert(!__STDIO_STREAM_IS_READING(stream));
358 assert(!(stream->__modeflags & __FLAG_UNGOT));
361 if (__STDIO_STREAM_IS_NBF(stream)) {
362 /* We require that all non buffered streams have no buffer. */
363 assert(!__STDIO_STREAM_BUFFER_SIZE(stream));
366 assert((stream->__modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
368 #ifdef __STDIO_BUFFERS
369 /* Ensure __bufstart <= __bufpos <= __bufend. */
370 assert(stream->__bufpos >= stream->__bufstart);
371 assert(stream->__bufpos <= stream->__bufend);
372 /* Ensure __bufstart <= __bufread <= __bufend. */
373 assert(stream->__bufread >= stream->__bufstart);
374 assert(stream->__bufread <= stream->__bufend);
377 /* If EOF, then we must have no buffered readable or ungots. */
378 if (__FEOF_UNLOCKED(stream)) {
379 #ifdef __STDIO_BUFFERS
380 assert(stream->__bufpos == stream->__bufread);
382 assert(!(stream->__modeflags & __FLAG_UNGOT));
386 if (!__STDIO_STREAM_IS_WRITING(stream)) {
387 #ifdef __STDIO_BUFFERS
388 /* If not writing, then putc macro must be disabled. */
389 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
390 assert(stream->__bufputc_u == stream->__bufstart);
395 if (!__STDIO_STREAM_IS_READING(stream)) {
396 /* If not reading, then can not have ungots. */
397 assert(!(stream->__modeflags & __FLAG_UNGOT));
398 #ifdef __STDIO_BUFFERS
399 /* Ensure __bufread == __bufstart. */
400 assert(stream->__bufread == stream->__bufstart);
401 /* If not reading, then getc macro must be disabled. */
402 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
403 assert(stream->__bufgetc_u == stream->__bufstart);
408 if (__STDIO_STREAM_IS_READING(stream)) {
409 assert(!__STDIO_STREAM_IS_WRITING(stream));
410 #ifdef __STDIO_BUFFERS
411 /* Ensure __bufpos <= __bufread. */
412 assert(stream->__bufpos <= stream->__bufread);
414 /* Ensure __bufgetc_u is valid. */
415 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
416 assert(stream->__bufgetc_u >= stream->__bufstart);
417 assert(stream->__bufgetc_u <= stream->__bufread);
423 if (__STDIO_STREAM_IS_WRITING(stream)) {
424 assert(!__STDIO_STREAM_IS_READING(stream));
425 #ifdef __STDIO_BUFFERS
426 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
427 assert(stream->__bufputc_u >= stream->__bufstart);
428 assert(stream->__bufputc_u <= stream->__bufend);
433 /* If have an ungotten char, then getc (and putc) must be disabled. */
434 /* Also, wide streams must have the getc/putc macros disabled. */
435 if ((stream->__modeflags & __FLAG_UNGOT)
436 || __STDIO_STREAM_IS_WIDE(stream)
438 #ifdef __STDIO_BUFFERS
439 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
440 assert(stream->__bufputc_u == stream->__bufstart);
442 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
443 assert(stream->__bufgetc_u == stream->__bufstart);
448 /* TODO -- filepos? ungot_width? filedes? nextopen? */