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 #ifdef __UCLIBC_HAS_WCHAR__
59 #define __need_wchar_t
62 /* Note: we don't really need mbstate for 8-bit locales. We do for UTF-8.
63 * For now, always use it. */
64 #define __STDIO_MBSTATE
65 #define __need_mbstate_t
69 /**********************************************************************/
70 /* Currently unimplemented/untested */
71 /* #define __STDIO_FLEXIBLE_SETVBUF */
73 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
74 #define __STDIO_GETC_MACRO
77 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
78 #define __STDIO_PUTC_MACRO
82 /**********************************************************************/
83 #include <bits/uClibc_mutex.h>
86 * 0 : do auto locking/unlocking
87 * 1 : user does locking/unlocking
88 * 2 : initial state prior to thread initialization
89 * with no auto locking/unlocking
91 * When threading is initialized, walk the stdio open stream list
92 * and do "if (user_locking == 2) user_locking = 0;".
94 * This way, we avoid calling the weak lock/unlock functions.
97 #define __STDIO_AUTO_THREADLOCK_VAR \
98 __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
100 #define __STDIO_AUTO_THREADLOCK(__stream) \
101 __UCLIBC_IO_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
102 (__stream)->__user_locking)
104 #define __STDIO_AUTO_THREADUNLOCK(__stream) \
105 __UCLIBC_IO_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
107 #define __STDIO_ALWAYS_THREADLOCK(__stream) \
108 __UCLIBC_IO_MUTEX_LOCK((__stream)->__lock)
110 #define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
111 __UCLIBC_IO_MUTEX_UNLOCK((__stream)->__lock)
113 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
114 __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
116 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
117 __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
119 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
120 __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
122 #ifdef __UCLIBC_HAS_THREADS__
123 #define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
125 #define __STDIO_SET_USER_LOCKING(__stream) ((void)0)
128 #ifdef __UCLIBC_HAS_THREADS__
129 #ifdef __USE_STDIO_FUTEXES__
130 #define STDIO_INIT_MUTEX(M) _IO_lock_init(M)
132 #define STDIO_INIT_MUTEX(M) __stdio_init_mutex(& M)
138 /**********************************************************************/
140 #define __STDIO_IOFBF 0 /* Fully buffered. */
141 #define __STDIO_IOLBF 1 /* Line buffered. */
142 #define __STDIO_IONBF 2 /* No buffering. */
146 #ifdef __STDIO_MBSTATE
147 __mbstate_t __mbstate;
149 #ifdef __UCLIBC_HAS_WCHAR__
154 #ifdef __UCLIBC_HAS_LFS__
157 #ifdef __STDIO_MBSTATE
158 __mbstate_t __mbstate;
160 #ifdef __UCLIBC_HAS_WCHAR__
166 /**********************************************************************/
167 #ifdef __UCLIBC_HAS_LFS__
168 typedef __off64_t __offmax_t; /* TODO -- rename this? */
170 typedef __off_t __offmax_t; /* TODO -- rename this? */
173 /**********************************************************************/
174 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
176 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
177 typedef __ssize_t __io_write_fn(void *__cookie,
178 const char *__buf, size_t __bufsize);
179 /* NOTE: GLIBC difference!!! -- fopencookie seek function
180 * For glibc, the type of pos is always (__off64_t *) but in our case
181 * it is type (__off_t *) when the lib is built without large file support.
183 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
184 typedef int __io_close_fn(void *__cookie);
188 __io_write_fn *write;
190 __io_close_fn *close;
191 } _IO_cookie_io_functions_t;
195 typedef __io_read_fn cookie_read_function_t;
196 typedef __io_write_fn cookie_write_function_t;
197 typedef __io_seek_fn cookie_seek_function_t;
198 typedef __io_close_fn cookie_close_function_t;
200 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
205 /**********************************************************************/
207 #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_IO_MUTEX
208 /* keep this in sync with uClibc_mutex.h */
209 # ifdef __USE_STDIO_FUTEXES__
210 # include <bits/stdio-lock.h>
211 # define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
213 # include <bits/pthreadtypes.h>
214 # define __UCLIBC_IO_MUTEX(M) pthread_mutex_t M
215 # endif /* __UCLIBC_HAS_THREADS_NATIVE__ */
218 struct __STDIO_FILE_STRUCT {
219 unsigned short __modeflags;
220 /* There could be a hole here, but modeflags is used most.*/
221 #ifdef __UCLIBC_HAS_WCHAR__
222 unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
223 /* Move the following futher down to avoid problems with getc/putc
224 * macros breaking shared apps when wchar config support is changed. */
225 /* wchar_t ungot[2]; */
226 #else /* __UCLIBC_HAS_WCHAR__ */
227 unsigned char __ungot[2];
228 #endif /* __UCLIBC_HAS_WCHAR__ */
230 #ifdef __STDIO_BUFFERS
231 unsigned char *__bufstart; /* pointer to buffer */
232 unsigned char *__bufend; /* pointer to 1 past end of buffer */
233 unsigned char *__bufpos;
234 unsigned char *__bufread; /* pointer to 1 past last buffered read char */
236 #ifdef __STDIO_GETC_MACRO
237 unsigned char *__bufgetc_u; /* 1 past last readable by getc_unlocked */
238 #endif /* __STDIO_GETC_MACRO */
239 #ifdef __STDIO_PUTC_MACRO
240 unsigned char *__bufputc_u; /* 1 past last writeable by putc_unlocked */
241 #endif /* __STDIO_PUTC_MACRO */
243 #endif /* __STDIO_BUFFERS */
245 #ifdef __STDIO_HAS_OPENLIST
246 struct __STDIO_FILE_STRUCT *__nextopen;
248 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
250 _IO_cookie_io_functions_t __gcs;
252 #ifdef __UCLIBC_HAS_WCHAR__
255 #ifdef __STDIO_MBSTATE
258 #ifdef __UCLIBC_HAS_XLOCALE__
259 void *__unused; /* Placeholder for codeset binding. */
261 #ifdef __UCLIBC_HAS_THREADS__
263 __UCLIBC_IO_MUTEX(__lock);
265 /* Everything after this is unimplemented... and may be trashed. */
266 #if __STDIO_BUILTIN_BUF_SIZE > 0
267 unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
268 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
272 /***********************************************************************/
273 /* Having ungotten characters implies the stream is reading.
274 * The scheme used here treats the least significant 2 bits of
275 * the stream's modeflags member as follows:
276 * 0 0 Not currently reading.
277 * 0 1 Reading, but no ungetc() or scanf() push back chars.
278 * 1 0 Reading with one ungetc() char (ungot[1] is 1)
279 * or one scanf() pushed back char (ungot[1] is 0).
280 * 1 1 Reading with both an ungetc() char and a scanf()
281 * pushed back char. Note that this must be the result
282 * of a scanf() push back (in ungot[0]) _followed_ by
283 * an ungetc() call (in ungot[1]).
286 * scanf() can NOT use ungetc() to push back characters.
287 * (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
290 #define __MASK_READING 0x0003U /* (0x0001 | 0x0002) */
291 #define __FLAG_READING 0x0001U
292 #define __FLAG_UNGOT 0x0002U
293 #define __FLAG_EOF 0x0004U
294 #define __FLAG_ERROR 0x0008U
295 #define __FLAG_WRITEONLY 0x0010U
296 #define __FLAG_READONLY 0x0020U /* (__FLAG_WRITEONLY << 1) */
297 #define __FLAG_WRITING 0x0040U
298 #define __FLAG_NARROW 0x0080U
300 #define __FLAG_FBF 0x0000U /* must be 0 */
301 #define __FLAG_LBF 0x0100U
302 #define __FLAG_NBF 0x0200U /* (__FLAG_LBF << 1) */
303 #define __MASK_BUFMODE 0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
304 #define __FLAG_APPEND 0x0400U /* fixed! == O_APPEND for linux */
305 #define __FLAG_WIDE 0x0800U
306 /* available slot 0x1000U */
307 #define __FLAG_FREEFILE 0x2000U
308 #define __FLAG_FREEBUF 0x4000U
309 #define __FLAG_LARGEFILE 0x8000U /* fixed! == 0_LARGEFILE for linux */
310 #define __FLAG_FAILED_FREOPEN __FLAG_LARGEFILE
312 /* Note: In no-buffer mode, it would be possible to pack the necessary
313 * flags into one byte. Since we wouldn't be buffering and there would
314 * be no support for wchar, the only flags we would need would be:
315 * 2 bits : ungot count
316 * 2 bits : eof + error
317 * 2 bits : readonly + writeonly
320 * So, for a very small system (< 128 files) we might have a
321 * 4-byte FILE struct of:
322 * unsigned char flags;
323 * signed char filedes;
324 * unsigned char ungot[2];
326 /**********************************************************************
327 * PROTOTYPES OF INTERNAL FUNCTIONS
328 **********************************************************************/
329 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
331 extern void weak_function _stdio_init(void) attribute_hidden;
332 extern void weak_function _stdio_term(void) attribute_hidden;
334 #ifdef __STDIO_HAS_OPENLIST
336 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
338 #ifdef __UCLIBC_HAS_THREADS__
339 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock)
340 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
344 #ifdef __STDIO_BUFFERS
345 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock)
346 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
351 extern int _stdio_user_locking;
357 /**********************************************************************/
359 #define __CLEARERR_UNLOCKED(__stream) \
360 ((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
361 #define __FEOF_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_EOF)
362 #define __FERROR_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_ERROR)
364 #ifdef __UCLIBC_HAS_THREADS__
365 # define __CLEARERR(__stream) (clearerr)(__stream)
366 # define __FERROR(__stream) (ferror)(__stream)
367 # define __FEOF(__stream) (feof)(__stream)
369 # define __CLEARERR(__stream) __CLEARERR_UNLOCKED(__stream)
370 # define __FERROR(__stream) __FERROR_UNLOCKED(__stream)
371 # define __FEOF(__stream) __FEOF_UNLOCKED(__stream)
374 extern int __fgetc_unlocked(FILE *__stream);
375 libc_hidden_proto(__fgetc_unlocked)
376 extern int __fputc_unlocked(int __c, FILE *__stream);
377 libc_hidden_proto(__fputc_unlocked)
379 /* First define the default definitions.
380 They are overridden below as necessary. */
381 #define __FGETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
382 #define __FGETC(__stream) (fgetc)((__stream))
383 #define __GETC_UNLOCKED_MACRO(__stream) (__fgetc_unlocked)((__stream))
384 #define __GETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
385 #define __GETC(__stream) (fgetc)((__stream))
387 #define __FPUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
388 #define __FPUTC(__c, __stream) (fputc)((__c),(__stream))
389 #define __PUTC_UNLOCKED_MACRO(__c, __stream) (__fputc_unlocked)((__c),(__stream))
390 #define __PUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
391 #define __PUTC(__c, __stream) (fputc)((__c),(__stream))
394 #ifdef __STDIO_GETC_MACRO
396 extern FILE *__stdin; /* For getchar() macro. */
398 # undef __GETC_UNLOCKED_MACRO
399 # define __GETC_UNLOCKED_MACRO(__stream) \
400 ( ((__stream)->__bufpos < (__stream)->__bufgetc_u) \
401 ? (*(__stream)->__bufpos++) \
402 : __fgetc_unlocked(__stream) )
405 /* Classic macro approach. getc{_unlocked} can have side effects. */
406 # undef __GETC_UNLOCKED
407 # define __GETC_UNLOCKED(__stream) __GETC_UNLOCKED_MACRO((__stream))
408 # ifndef __UCLIBC_HAS_THREADS__
410 # define __GETC(__stream) __GETC_UNLOCKED_MACRO((__stream))
414 /* Using gcc extension for safety and additional inlining. */
415 # undef __FGETC_UNLOCKED
416 # define __FGETC_UNLOCKED(__stream) \
418 FILE *__S = (__stream); \
419 __GETC_UNLOCKED_MACRO(__S); \
422 # undef __GETC_UNLOCKED
423 # define __GETC_UNLOCKED(__stream) __FGETC_UNLOCKED((__stream))
425 # ifdef __UCLIBC_HAS_THREADS__
427 # define __FGETC(__stream) \
429 FILE *__S = (__stream); \
430 ((__S->__user_locking ) \
431 ? __GETC_UNLOCKED_MACRO(__S) \
436 # define __GETC(__stream) __FGETC((__stream))
441 # define __FGETC(__stream) __FGETC_UNLOCKED((__stream))
443 # define __GETC(__stream) __FGETC_UNLOCKED((__stream))
450 # define __stdin stdin
452 #endif /* __STDIO_GETC_MACRO */
455 #ifdef __STDIO_PUTC_MACRO
457 extern FILE *__stdout; /* For putchar() macro. */
459 # undef __PUTC_UNLOCKED_MACRO
460 # define __PUTC_UNLOCKED_MACRO(__c, __stream) \
461 ( ((__stream)->__bufpos < (__stream)->__bufputc_u) \
462 ? (*(__stream)->__bufpos++) = (__c) \
463 : __fputc_unlocked((__c),(__stream)) )
466 /* Classic macro approach. putc{_unlocked} can have side effects.*/
467 # undef __PUTC_UNLOCKED
468 # define __PUTC_UNLOCKED(__c, __stream) \
469 __PUTC_UNLOCKED_MACRO((__c), (__stream))
470 # ifndef __UCLIBC_HAS_THREADS__
472 # define __PUTC(__c, __stream) __PUTC_UNLOCKED_MACRO((__c), (__stream))
476 /* Using gcc extension for safety and additional inlining. */
478 # undef __FPUTC_UNLOCKED
479 # define __FPUTC_UNLOCKED(__c, __stream) \
481 FILE *__S = (__stream); \
482 __PUTC_UNLOCKED_MACRO((__c),__S); \
485 # undef __PUTC_UNLOCKED
486 # define __PUTC_UNLOCKED(__c, __stream) __FPUTC_UNLOCKED((__c), (__stream))
488 # ifdef __UCLIBC_HAS_THREADS__
490 # define __FPUTC(__c, __stream) \
492 FILE *__S = (__stream); \
493 ((__S->__user_locking) \
494 ? __PUTC_UNLOCKED_MACRO((__c),__S) \
495 : (fputc)((__c),__S)); \
499 # define __PUTC(__c, __stream) __FPUTC((__c), (__stream))
504 # define __FPUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
506 # define __PUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
513 # define __stdout stdout
515 #endif /* __STDIO_PUTC_MACRO */