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_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
138 (__stream)->__user_locking)
140 #define __STDIO_AUTO_THREADUNLOCK(__stream) \
141 __UCLIBC_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
143 #define __STDIO_ALWAYS_THREADLOCK(__stream) \
144 __UCLIBC_MUTEX_LOCK((__stream)->__lock)
146 #define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
147 __UCLIBC_MUTEX_UNLOCK((__stream)->__lock)
149 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
150 __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
152 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
153 __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
155 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
156 __UCLIBC_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 /**********************************************************************/
166 #define __STDIO_IOFBF 0 /* Fully buffered. */
167 #define __STDIO_IOLBF 1 /* Line buffered. */
168 #define __STDIO_IONBF 2 /* No buffering. */
172 #ifdef __STDIO_MBSTATE
173 __mbstate_t __mbstate;
175 #ifdef __UCLIBC_HAS_WCHAR__
180 #ifdef __UCLIBC_HAS_LFS__
183 #ifdef __STDIO_MBSTATE
184 __mbstate_t __mbstate;
186 #ifdef __UCLIBC_HAS_WCHAR__
192 /**********************************************************************/
193 #ifdef __UCLIBC_HAS_LFS__
194 typedef __off64_t __offmax_t; /* TODO -- rename this? */
196 typedef __off_t __offmax_t; /* TODO -- rename this? */
199 /**********************************************************************/
200 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
202 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
203 typedef __ssize_t __io_write_fn(void *__cookie,
204 __const char *__buf, size_t __bufsize);
205 /* NOTE: GLIBC difference!!! -- fopencookie seek function
206 * For glibc, the type of pos is always (__off64_t *) but in our case
207 * it is type (__off_t *) when the lib is built without large file support.
209 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
210 typedef int __io_close_fn(void *__cookie);
214 __io_write_fn *write;
216 __io_close_fn *close;
217 } _IO_cookie_io_functions_t;
219 #if defined(_LIBC) || defined(_GNU_SOURCE)
221 typedef __io_read_fn cookie_read_function_t;
222 typedef __io_write_fn cookie_write_function_t;
223 typedef __io_seek_fn cookie_seek_function_t;
224 typedef __io_close_fn cookie_close_function_t;
226 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
231 /**********************************************************************/
233 struct __STDIO_FILE_STRUCT {
234 unsigned short __modeflags;
235 /* There could be a hole here, but modeflags is used most.*/
236 #ifdef __UCLIBC_HAS_WCHAR__
237 unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
238 /* Move the following futher down to avoid problems with getc/putc
239 * macros breaking shared apps when wchar config support is changed. */
240 /* wchar_t ungot[2]; */
241 #else /* __UCLIBC_HAS_WCHAR__ */
242 unsigned char __ungot[2];
243 #endif /* __UCLIBC_HAS_WCHAR__ */
245 #ifdef __STDIO_BUFFERS
246 unsigned char *__bufstart; /* pointer to buffer */
247 unsigned char *__bufend; /* pointer to 1 past end of buffer */
248 unsigned char *__bufpos;
249 unsigned char *__bufread; /* pointer to 1 past last buffered read char */
251 #ifdef __STDIO_GETC_MACRO
252 unsigned char *__bufgetc_u; /* 1 past last readable by getc_unlocked */
253 #endif /* __STDIO_GETC_MACRO */
254 #ifdef __STDIO_PUTC_MACRO
255 unsigned char *__bufputc_u; /* 1 past last writeable by putc_unlocked */
256 #endif /* __STDIO_PUTC_MACRO */
258 #endif /* __STDIO_BUFFERS */
260 #ifdef __STDIO_HAS_OPENLIST
261 struct __STDIO_FILE_STRUCT *__nextopen;
263 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
265 _IO_cookie_io_functions_t __gcs;
267 #ifdef __UCLIBC_HAS_WCHAR__
270 #ifdef __STDIO_MBSTATE
273 #ifdef __UCLIBC_HAS_XLOCALE__
274 void *__unused; /* Placeholder for codeset binding. */
276 #ifdef __UCLIBC_HAS_THREADS__
278 __UCLIBC_MUTEX(__lock);
280 /* Everything after this is unimplemented... and may be trashed. */
281 #if __STDIO_BUILTIN_BUF_SIZE > 0
282 unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
283 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
287 /***********************************************************************/
288 /* Having ungotten characters implies the stream is reading.
289 * The scheme used here treats the least significant 2 bits of
290 * the stream's modeflags member as follows:
291 * 0 0 Not currently reading.
292 * 0 1 Reading, but no ungetc() or scanf() push back chars.
293 * 1 0 Reading with one ungetc() char (ungot[1] is 1)
294 * or one scanf() pushed back char (ungot[1] is 0).
295 * 1 1 Reading with both an ungetc() char and a scanf()
296 * pushed back char. Note that this must be the result
297 * of a scanf() push back (in ungot[0]) _followed_ by
298 * an ungetc() call (in ungot[1]).
301 * scanf() can NOT use ungetc() to push back characters.
302 * (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
305 #define __MASK_READING 0x0003U /* (0x0001 | 0x0002) */
306 #define __FLAG_READING 0x0001U
307 #define __FLAG_UNGOT 0x0002U
308 #define __FLAG_EOF 0x0004U
309 #define __FLAG_ERROR 0x0008U
310 #define __FLAG_WRITEONLY 0x0010U
311 #define __FLAG_READONLY 0x0020U /* (__FLAG_WRITEONLY << 1) */
312 #define __FLAG_WRITING 0x0040U
313 #define __FLAG_NARROW 0x0080U
315 #define __FLAG_FBF 0x0000U /* must be 0 */
316 #define __FLAG_LBF 0x0100U
317 #define __FLAG_NBF 0x0200U /* (__FLAG_LBF << 1) */
318 #define __MASK_BUFMODE 0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
319 #define __FLAG_APPEND 0x0400U /* fixed! == O_APPEND for linux */
320 #define __FLAG_WIDE 0x0800U
321 /* available slot 0x1000U */
322 #define __FLAG_FREEFILE 0x2000U
323 #define __FLAG_FREEBUF 0x4000U
324 #define __FLAG_LARGEFILE 0x8000U /* fixed! == 0_LARGEFILE for linux */
325 #define __FLAG_FAILED_FREOPEN __FLAG_LARGEFILE
327 /* Note: In no-buffer mode, it would be possible to pack the necessary
328 * flags into one byte. Since we wouldn't be buffering and there would
329 * be no support for wchar, the only flags we would need would be:
330 * 2 bits : ungot count
331 * 2 bits : eof + error
332 * 2 bits : readonly + writeonly
335 * So, for a very small system (< 128 files) we might have a
336 * 4-byte FILE struct of:
337 * unsigned char flags;
338 * signed char filedes;
339 * unsigned char ungot[2];
341 /**********************************************************************
342 * PROTOTYPES OF INTERNAL FUNCTIONS
343 **********************************************************************/
344 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
346 extern void _stdio_init(void) attribute_hidden;
347 extern void _stdio_term(void) attribute_hidden;
349 #ifdef __STDIO_HAS_OPENLIST
351 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
353 #ifdef __UCLIBC_HAS_THREADS__
354 __UCLIBC_MUTEX_EXTERN(_stdio_openlist_add_lock);
355 #ifdef __STDIO_BUFFERS
356 __UCLIBC_MUTEX_EXTERN(_stdio_openlist_del_lock);
357 extern volatile int _stdio_openlist_use_count; /* _stdio_openlist_del_lock */
358 extern int _stdio_openlist_del_count; /* _stdio_openlist_del_lock */
360 extern int _stdio_user_locking;
361 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
367 /**********************************************************************/
369 #define __CLEARERR_UNLOCKED(__stream) \
370 ((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
371 #define __FEOF_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_EOF)
372 #define __FERROR_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_ERROR)
374 #ifdef __UCLIBC_HAS_THREADS__
375 # define __CLEARERR(__stream) (clearerr)(__stream)
376 # define __FERROR(__stream) (ferror)(__stream)
377 # define __FEOF(__stream) (feof)(__stream)
379 # define __CLEARERR(__stream) __CLEARERR_UNLOCKED(__stream)
380 # define __FERROR(__stream) __FERROR_UNLOCKED(__stream)
381 # define __FEOF(__stream) __FEOF_UNLOCKED(__stream)
384 extern int __fgetc_unlocked(FILE *__stream);
385 libc_hidden_proto(__fgetc_unlocked)
386 extern int __fputc_unlocked(int __c, FILE *__stream);
387 libc_hidden_proto(__fputc_unlocked)
389 /* First define the default definitions.
390 They are overridden below as necessary. */
391 #define __FGETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
392 #define __FGETC(__stream) (fgetc)((__stream))
393 #define __GETC_UNLOCKED_MACRO(__stream) (__fgetc_unlocked)((__stream))
394 #define __GETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
395 #define __GETC(__stream) (fgetc)((__stream))
397 #define __FPUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
398 #define __FPUTC(__c, __stream) (fputc)((__c),(__stream))
399 #define __PUTC_UNLOCKED_MACRO(__c, __stream) (__fputc_unlocked)((__c),(__stream))
400 #define __PUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
401 #define __PUTC(__c, __stream) (fputc)((__c),(__stream))
404 #ifdef __STDIO_GETC_MACRO
406 extern FILE *__stdin; /* For getchar() macro. */
408 # undef __GETC_UNLOCKED_MACRO
409 # define __GETC_UNLOCKED_MACRO(__stream) \
410 ( ((__stream)->__bufpos < (__stream)->__bufgetc_u) \
411 ? (*(__stream)->__bufpos++) \
412 : __fgetc_unlocked(__stream) )
415 /* Classic macro approach. getc{_unlocked} can have side effects. */
416 # undef __GETC_UNLOCKED
417 # define __GETC_UNLOCKED(__stream) __GETC_UNLOCKED_MACRO((__stream))
418 # ifndef __UCLIBC_HAS_THREADS__
420 # define __GETC(__stream) __GETC_UNLOCKED_MACRO((__stream))
424 /* Using gcc extension for safety and additional inlining. */
425 # undef __FGETC_UNLOCKED
426 # define __FGETC_UNLOCKED(__stream) \
428 FILE *__S = (__stream); \
429 __GETC_UNLOCKED_MACRO(__S); \
432 # undef __GETC_UNLOCKED
433 # define __GETC_UNLOCKED(__stream) __FGETC_UNLOCKED((__stream))
435 # ifdef __UCLIBC_HAS_THREADS__
437 # define __FGETC(__stream) \
439 FILE *__S = (__stream); \
440 ((__S->__user_locking ) \
441 ? __GETC_UNLOCKED_MACRO(__S) \
446 # define __GETC(__stream) __FGETC((__stream))
451 # define __FGETC(__stream) __FGETC_UNLOCKED((__stream))
453 # define __GETC(__stream) __FGETC_UNLOCKED((__stream))
460 #endif /* __STDIO_GETC_MACRO */
463 #ifdef __STDIO_PUTC_MACRO
465 extern FILE *__stdout; /* For putchar() macro. */
467 # undef __PUTC_UNLOCKED_MACRO
468 # define __PUTC_UNLOCKED_MACRO(__c, __stream) \
469 ( ((__stream)->__bufpos < (__stream)->__bufputc_u) \
470 ? (*(__stream)->__bufpos++) = (__c) \
471 : __fputc_unlocked((__c),(__stream)) )
474 /* Classic macro approach. putc{_unlocked} can have side effects.*/
475 # undef __PUTC_UNLOCKED
476 # define __PUTC_UNLOCKED(__c, __stream) \
477 __PUTC_UNLOCKED_MACRO((__c), (__stream))
478 # ifndef __UCLIBC_HAS_THREADS__
480 # define __PUTC(__c, __stream) __PUTC_UNLOCKED_MACRO((__c), (__stream))
484 /* Using gcc extension for safety and additional inlining. */
486 # undef __FPUTC_UNLOCKED
487 # define __FPUTC_UNLOCKED(__c, __stream) \
489 FILE *__S = (__stream); \
490 __PUTC_UNLOCKED_MACRO((__c),__S); \
493 # undef __PUTC_UNLOCKED
494 # define __PUTC_UNLOCKED(__c, __stream) __FPUTC_UNLOCKED((__c), (__stream))
496 # ifdef __UCLIBC_HAS_THREADS__
498 # define __FPUTC(__c, __stream) \
500 FILE *__S = (__stream); \
501 ((__S->__user_locking) \
502 ? __PUTC_UNLOCKED_MACRO((__c),__S) \
503 : (fputc)((__c),__S)); \
507 # define __PUTC(__c, __stream) __FPUTC((__c), (__stream))
512 # define __FPUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
514 # define __PUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
519 #endif /* __STDIO_PUTC_MACRO */