1 /* Copyright (C) 2002-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.
9 #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
12 /**********************************************************************/
14 #define __STDIO_BUFFERS
15 /* ANSI/ISO mandate at least 256. */
16 #if defined(__UCLIBC_HAS_STDIO_BUFSIZ_NONE__)
17 /* Fake this because some apps use stdio.h BUFSIZ. */
18 #define __STDIO_BUFSIZ 256
19 #undef __STDIO_BUFFERS
20 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_256__)
21 #define __STDIO_BUFSIZ 256
22 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_512__)
23 #define __STDIO_BUFSIZ 512
24 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_1024__)
25 #define __STDIO_BUFSIZ 1024
26 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_2048__)
27 #define __STDIO_BUFSIZ 2048
28 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_4096__)
29 #define __STDIO_BUFSIZ 4096
30 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_8192__)
31 #define __STDIO_BUFSIZ 8192
33 #error config seems to be out of sync regarding bufsiz options
36 #ifdef __UCLIBC_HAS_STDIO_BUFSIZ_NONE__
37 #define __STDIO_BUILTIN_BUF_SIZE 0
38 #else /* __UCLIBC_HAS_STDIO_BUFSIZ_NONE__ */
39 #if defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_NONE__)
40 #define __STDIO_BUILTIN_BUF_SIZE 0
41 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_4__)
42 #define __STDIO_BUILTIN_BUF_SIZE 4
43 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_8__)
44 #define __STDIO_BUILTIN_BUF_SIZE 8
46 #error config seems to be out of sync regarding builtin buffer size
50 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) || defined(__UCLIBC_HAS_THREADS__)
51 #define __STDIO_HAS_OPENLIST 1
53 #undef __STDIO_HAS_OPENLIST
56 /**********************************************************************/
57 /* Make sure defines related to large files are consistent. */
60 #ifdef __UCLIBC_HAS_LFS__
61 #undef __USE_LARGEFILE
62 #undef __USE_LARGEFILE64
63 #undef __USE_FILE_OFFSET64
64 /* If we're actually building uClibc with large file support, only define... */
65 #define __USE_LARGEFILE64 1
66 #endif /* __UCLIBC_HAS_LFS__ */
70 #ifndef __UCLIBC_HAS_LFS__
71 #if defined(__LARGEFILE64_SOURCE) || defined(__USE_LARGEFILE64) || defined(__USE_FILE_OFFSET64)
72 #error Sorry... uClibc was built without large file support!
74 #endif /* __UCLIBC_HAS_LFS__ */
77 /**********************************************************************/
78 #ifdef __UCLIBC_HAS_WCHAR__
80 #define __need_wchar_t
83 /* Note: we don't really need mbstate for 8-bit locales. We do for UTF-8.
84 * For now, always use it. */
85 #define __STDIO_MBSTATE
86 #define __need_mbstate_t
90 /**********************************************************************/
91 /* Currently unimplemented/untested */
92 /* #define __STDIO_FLEXIBLE_SETVBUF */
94 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
95 #define __STDIO_GETC_MACRO
98 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
99 #define __STDIO_PUTC_MACRO
103 /* These are consistency checks on the different options */
105 #ifndef __STDIO_BUFFERS
106 #undef __STDIO_GETC_MACRO
107 #undef __STDIO_PUTC_MACRO
111 #undef __UCLIBC_HAS_LFS__
114 #ifndef __UCLIBC_HAS_LFS__
115 #undef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
118 /**********************************************************************/
119 #include <bits/uClibc_mutex.h>
122 * 0 : do auto locking/unlocking
123 * 1 : user does locking/unlocking
124 * 2 : initial state prior to thread initialization
125 * with no auto locking/unlocking
127 * When threading is initialized, walk the stdio open stream list
128 * and do "if (user_locking == 2) user_locking = 0;".
130 * This way, we avoid calling the weak lock/unlock functions.
133 #define __STDIO_AUTO_THREADLOCK_VAR \
134 __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
136 #define __STDIO_AUTO_THREADLOCK(__stream) \
137 __UCLIBC_IO_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
138 (__stream)->__user_locking)
140 #define __STDIO_AUTO_THREADUNLOCK(__stream) \
141 __UCLIBC_IO_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
143 #define __STDIO_ALWAYS_THREADLOCK(__stream) \
144 __UCLIBC_IO_MUTEX_LOCK((__stream)->__lock)
146 #define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
147 __UCLIBC_IO_MUTEX_UNLOCK((__stream)->__lock)
149 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
150 __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
152 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
153 __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
155 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
156 __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
158 #ifdef __UCLIBC_HAS_THREADS__
159 #define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
161 #define __STDIO_SET_USER_LOCKING(__stream) ((void)0)
164 #ifdef __UCLIBC_HAS_THREADS__
165 #ifdef __USE_STDIO_FUTEXES__
166 #define STDIO_INIT_MUTEX(M) _IO_lock_init(M)
168 #define STDIO_INIT_MUTEX(M) __stdio_init_mutex(& M)
172 /**********************************************************************/
174 #define __STDIO_IOFBF 0 /* Fully buffered. */
175 #define __STDIO_IOLBF 1 /* Line buffered. */
176 #define __STDIO_IONBF 2 /* No buffering. */
180 #ifdef __STDIO_MBSTATE
181 __mbstate_t __mbstate;
183 #ifdef __UCLIBC_HAS_WCHAR__
188 #ifdef __UCLIBC_HAS_LFS__
191 #ifdef __STDIO_MBSTATE
192 __mbstate_t __mbstate;
194 #ifdef __UCLIBC_HAS_WCHAR__
200 /**********************************************************************/
201 #ifdef __UCLIBC_HAS_LFS__
202 typedef __off64_t __offmax_t; /* TODO -- rename this? */
204 typedef __off_t __offmax_t; /* TODO -- rename this? */
207 /**********************************************************************/
208 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
210 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
211 typedef __ssize_t __io_write_fn(void *__cookie,
212 __const char *__buf, size_t __bufsize);
213 /* NOTE: GLIBC difference!!! -- fopencookie seek function
214 * For glibc, the type of pos is always (__off64_t *) but in our case
215 * it is type (__off_t *) when the lib is built without large file support.
217 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
218 typedef int __io_close_fn(void *__cookie);
222 __io_write_fn *write;
224 __io_close_fn *close;
225 } _IO_cookie_io_functions_t;
227 #if defined(_LIBC) || defined(_GNU_SOURCE)
229 typedef __io_read_fn cookie_read_function_t;
230 typedef __io_write_fn cookie_write_function_t;
231 typedef __io_seek_fn cookie_seek_function_t;
232 typedef __io_close_fn cookie_close_function_t;
234 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
239 /**********************************************************************/
241 struct __STDIO_FILE_STRUCT {
242 unsigned short __modeflags;
243 /* There could be a hole here, but modeflags is used most.*/
244 #ifdef __UCLIBC_HAS_WCHAR__
245 unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
246 /* Move the following futher down to avoid problems with getc/putc
247 * macros breaking shared apps when wchar config support is changed. */
248 /* wchar_t ungot[2]; */
249 #else /* __UCLIBC_HAS_WCHAR__ */
250 unsigned char __ungot[2];
251 #endif /* __UCLIBC_HAS_WCHAR__ */
253 #ifdef __STDIO_BUFFERS
254 unsigned char *__bufstart; /* pointer to buffer */
255 unsigned char *__bufend; /* pointer to 1 past end of buffer */
256 unsigned char *__bufpos;
257 unsigned char *__bufread; /* pointer to 1 past last buffered read char */
259 #ifdef __STDIO_GETC_MACRO
260 unsigned char *__bufgetc_u; /* 1 past last readable by getc_unlocked */
261 #endif /* __STDIO_GETC_MACRO */
262 #ifdef __STDIO_PUTC_MACRO
263 unsigned char *__bufputc_u; /* 1 past last writeable by putc_unlocked */
264 #endif /* __STDIO_PUTC_MACRO */
266 #endif /* __STDIO_BUFFERS */
268 #ifdef __STDIO_HAS_OPENLIST
269 struct __STDIO_FILE_STRUCT *__nextopen;
271 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
273 _IO_cookie_io_functions_t __gcs;
275 #ifdef __UCLIBC_HAS_WCHAR__
278 #ifdef __STDIO_MBSTATE
281 #ifdef __UCLIBC_HAS_XLOCALE__
282 void *__unused; /* Placeholder for codeset binding. */
284 #ifdef __UCLIBC_HAS_THREADS__
286 __UCLIBC_IO_MUTEX(__lock);
288 /* Everything after this is unimplemented... and may be trashed. */
289 #if __STDIO_BUILTIN_BUF_SIZE > 0
290 unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
291 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
295 /***********************************************************************/
296 /* Having ungotten characters implies the stream is reading.
297 * The scheme used here treats the least significant 2 bits of
298 * the stream's modeflags member as follows:
299 * 0 0 Not currently reading.
300 * 0 1 Reading, but no ungetc() or scanf() push back chars.
301 * 1 0 Reading with one ungetc() char (ungot[1] is 1)
302 * or one scanf() pushed back char (ungot[1] is 0).
303 * 1 1 Reading with both an ungetc() char and a scanf()
304 * pushed back char. Note that this must be the result
305 * of a scanf() push back (in ungot[0]) _followed_ by
306 * an ungetc() call (in ungot[1]).
309 * scanf() can NOT use ungetc() to push back characters.
310 * (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
313 #define __MASK_READING 0x0003U /* (0x0001 | 0x0002) */
314 #define __FLAG_READING 0x0001U
315 #define __FLAG_UNGOT 0x0002U
316 #define __FLAG_EOF 0x0004U
317 #define __FLAG_ERROR 0x0008U
318 #define __FLAG_WRITEONLY 0x0010U
319 #define __FLAG_READONLY 0x0020U /* (__FLAG_WRITEONLY << 1) */
320 #define __FLAG_WRITING 0x0040U
321 #define __FLAG_NARROW 0x0080U
323 #define __FLAG_FBF 0x0000U /* must be 0 */
324 #define __FLAG_LBF 0x0100U
325 #define __FLAG_NBF 0x0200U /* (__FLAG_LBF << 1) */
326 #define __MASK_BUFMODE 0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
327 #define __FLAG_APPEND 0x0400U /* fixed! == O_APPEND for linux */
328 #define __FLAG_WIDE 0x0800U
329 /* available slot 0x1000U */
330 #define __FLAG_FREEFILE 0x2000U
331 #define __FLAG_FREEBUF 0x4000U
332 #define __FLAG_LARGEFILE 0x8000U /* fixed! == 0_LARGEFILE for linux */
333 #define __FLAG_FAILED_FREOPEN __FLAG_LARGEFILE
335 /* Note: In no-buffer mode, it would be possible to pack the necessary
336 * flags into one byte. Since we wouldn't be buffering and there would
337 * be no support for wchar, the only flags we would need would be:
338 * 2 bits : ungot count
339 * 2 bits : eof + error
340 * 2 bits : readonly + writeonly
343 * So, for a very small system (< 128 files) we might have a
344 * 4-byte FILE struct of:
345 * unsigned char flags;
346 * signed char filedes;
347 * unsigned char ungot[2];
349 /**********************************************************************
350 * PROTOTYPES OF INTERNAL FUNCTIONS
351 **********************************************************************/
352 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
354 extern void _stdio_init(void) attribute_hidden;
355 extern void _stdio_term(void) attribute_hidden;
357 #ifdef __STDIO_HAS_OPENLIST
359 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
361 #ifdef __UCLIBC_HAS_THREADS__
362 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock);
363 #ifdef __STDIO_BUFFERS
364 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock);
365 extern volatile int _stdio_openlist_use_count; /* _stdio_openlist_del_lock */
366 extern int _stdio_openlist_del_count; /* _stdio_openlist_del_lock */
368 extern int _stdio_user_locking;
369 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
375 /**********************************************************************/
377 #define __CLEARERR_UNLOCKED(__stream) \
378 ((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
379 #define __FEOF_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_EOF)
380 #define __FERROR_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_ERROR)
382 #ifdef __UCLIBC_HAS_THREADS__
383 # define __CLEARERR(__stream) (clearerr)(__stream)
384 # define __FERROR(__stream) (ferror)(__stream)
385 # define __FEOF(__stream) (feof)(__stream)
387 # define __CLEARERR(__stream) __CLEARERR_UNLOCKED(__stream)
388 # define __FERROR(__stream) __FERROR_UNLOCKED(__stream)
389 # define __FEOF(__stream) __FEOF_UNLOCKED(__stream)
392 extern int __fgetc_unlocked(FILE *__stream);
393 libc_hidden_proto(__fgetc_unlocked)
394 extern int __fputc_unlocked(int __c, FILE *__stream);
395 libc_hidden_proto(__fputc_unlocked)
397 /* First define the default definitions.
398 They are overridden below as necessary. */
399 #define __FGETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
400 #define __FGETC(__stream) (fgetc)((__stream))
401 #define __GETC_UNLOCKED_MACRO(__stream) (__fgetc_unlocked)((__stream))
402 #define __GETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
403 #define __GETC(__stream) (fgetc)((__stream))
405 #define __FPUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
406 #define __FPUTC(__c, __stream) (fputc)((__c),(__stream))
407 #define __PUTC_UNLOCKED_MACRO(__c, __stream) (__fputc_unlocked)((__c),(__stream))
408 #define __PUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
409 #define __PUTC(__c, __stream) (fputc)((__c),(__stream))
412 #ifdef __STDIO_GETC_MACRO
414 extern FILE *__stdin; /* For getchar() macro. */
416 # undef __GETC_UNLOCKED_MACRO
417 # define __GETC_UNLOCKED_MACRO(__stream) \
418 ( ((__stream)->__bufpos < (__stream)->__bufgetc_u) \
419 ? (*(__stream)->__bufpos++) \
420 : __fgetc_unlocked(__stream) )
423 /* Classic macro approach. getc{_unlocked} can have side effects. */
424 # undef __GETC_UNLOCKED
425 # define __GETC_UNLOCKED(__stream) __GETC_UNLOCKED_MACRO((__stream))
426 # ifndef __UCLIBC_HAS_THREADS__
428 # define __GETC(__stream) __GETC_UNLOCKED_MACRO((__stream))
432 /* Using gcc extension for safety and additional inlining. */
433 # undef __FGETC_UNLOCKED
434 # define __FGETC_UNLOCKED(__stream) \
436 FILE *__S = (__stream); \
437 __GETC_UNLOCKED_MACRO(__S); \
440 # undef __GETC_UNLOCKED
441 # define __GETC_UNLOCKED(__stream) __FGETC_UNLOCKED((__stream))
443 # ifdef __UCLIBC_HAS_THREADS__
445 # define __FGETC(__stream) \
447 FILE *__S = (__stream); \
448 ((__S->__user_locking ) \
449 ? __GETC_UNLOCKED_MACRO(__S) \
454 # define __GETC(__stream) __FGETC((__stream))
459 # define __FGETC(__stream) __FGETC_UNLOCKED((__stream))
461 # define __GETC(__stream) __FGETC_UNLOCKED((__stream))
468 #endif /* __STDIO_GETC_MACRO */
471 #ifdef __STDIO_PUTC_MACRO
473 extern FILE *__stdout; /* For putchar() macro. */
475 # undef __PUTC_UNLOCKED_MACRO
476 # define __PUTC_UNLOCKED_MACRO(__c, __stream) \
477 ( ((__stream)->__bufpos < (__stream)->__bufputc_u) \
478 ? (*(__stream)->__bufpos++) = (__c) \
479 : __fputc_unlocked((__c),(__stream)) )
482 /* Classic macro approach. putc{_unlocked} can have side effects.*/
483 # undef __PUTC_UNLOCKED
484 # define __PUTC_UNLOCKED(__c, __stream) \
485 __PUTC_UNLOCKED_MACRO((__c), (__stream))
486 # ifndef __UCLIBC_HAS_THREADS__
488 # define __PUTC(__c, __stream) __PUTC_UNLOCKED_MACRO((__c), (__stream))
492 /* Using gcc extension for safety and additional inlining. */
494 # undef __FPUTC_UNLOCKED
495 # define __FPUTC_UNLOCKED(__c, __stream) \
497 FILE *__S = (__stream); \
498 __PUTC_UNLOCKED_MACRO((__c),__S); \
501 # undef __PUTC_UNLOCKED
502 # define __PUTC_UNLOCKED(__c, __stream) __FPUTC_UNLOCKED((__c), (__stream))
504 # ifdef __UCLIBC_HAS_THREADS__
506 # define __FPUTC(__c, __stream) \
508 FILE *__S = (__stream); \
509 ((__S->__user_locking) \
510 ? __PUTC_UNLOCKED_MACRO((__c),__S) \
511 : (fputc)((__c),__S)); \
515 # define __PUTC(__c, __stream) __FPUTC((__c), (__stream))
520 # define __FPUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
522 # define __PUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
527 #endif /* __STDIO_PUTC_MACRO */