]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/common/bits/uClibc_stdio.h
3631ef79f8a64f3e750268244ea2a0a24f3712fd
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / common / bits / uClibc_stdio.h
1 /* Copyright (C) 2002-2004   Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #ifndef _STDIO_H
9 #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
10 #endif
11
12 /**********************************************************************/
13
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
32 #else
33 #error config seems to be out of sync regarding bufsiz options
34 #endif
35
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
45 #else
46 #error config seems to be out of sync regarding builtin buffer size
47 #endif
48 #endif
49
50 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) || defined(__UCLIBC_HAS_THREADS__)
51 #define __STDIO_HAS_OPENLIST 1
52 #else
53 #undef __STDIO_HAS_OPENLIST
54 #endif
55
56 /**********************************************************************/
57 /* Make sure defines related to large files are consistent. */
58 #ifdef _LIBC
59
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__ */
67
68 #else  /* not _LIBC */
69
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!
73 #endif
74 #endif /* __UCLIBC_HAS_LFS__ */
75
76 #endif /* _LIBC */
77 /**********************************************************************/
78 #ifdef __UCLIBC_HAS_WCHAR__
79
80 #define __need_wchar_t
81 #include <stddef.h>
82
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
87 #include <wchar.h>
88
89 #endif
90 /**********************************************************************/
91 /* Currently unimplemented/untested */
92 /* #define __STDIO_FLEXIBLE_SETVBUF */
93
94 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
95 #define __STDIO_GETC_MACRO
96 #endif
97
98 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
99 #define __STDIO_PUTC_MACRO
100 #endif
101
102
103 /* These are consistency checks on the different options */
104
105 #ifndef __STDIO_BUFFERS
106 #undef __STDIO_GETC_MACRO
107 #undef __STDIO_PUTC_MACRO
108 #endif
109
110 #ifdef __BCC__
111 #undef __UCLIBC_HAS_LFS__
112 #endif
113
114 #ifndef __UCLIBC_HAS_LFS__
115 #undef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
116 #endif
117
118 /**********************************************************************/
119 #include <bits/uClibc_mutex.h>
120
121 /* user_locking
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
126  *
127  * When threading is initialized, walk the stdio open stream list
128  * and do  "if (user_locking == 2) user_locking = 0;".
129  *
130  * This way, we avoid calling the weak lock/unlock functions.
131  */
132
133 #define __STDIO_AUTO_THREADLOCK_VAR                                             \
134         __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
135
136 #define __STDIO_AUTO_THREADLOCK(__stream)                                       \
137         __UCLIBC_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking,     \
138         (__stream)->__user_locking)
139
140 #define __STDIO_AUTO_THREADUNLOCK(__stream)                                     \
141         __UCLIBC_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
142
143 #define __STDIO_ALWAYS_THREADLOCK(__stream)                                     \
144         __UCLIBC_MUTEX_LOCK((__stream)->__lock)
145
146 #define __STDIO_ALWAYS_THREADUNLOCK(__stream)                                   \
147         __UCLIBC_MUTEX_UNLOCK((__stream)->__lock)
148
149 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream)                       \
150         __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
151
152 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream)                    \
153         __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
154
155 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream)                     \
156         __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
157
158 #ifdef __UCLIBC_HAS_THREADS__
159 #define __STDIO_SET_USER_LOCKING(__stream)      ((__stream)->__user_locking = 1)
160 #else
161 #define __STDIO_SET_USER_LOCKING(__stream)              ((void)0)
162 #endif
163
164 /**********************************************************************/
165
166 #define __STDIO_IOFBF 0         /* Fully buffered.  */
167 #define __STDIO_IOLBF 1         /* Line buffered.  */
168 #define __STDIO_IONBF 2         /* No buffering.  */
169
170 typedef struct {
171         __off_t __pos;
172 #ifdef __STDIO_MBSTATE
173         __mbstate_t __mbstate;
174 #endif
175 #ifdef __UCLIBC_HAS_WCHAR__
176         int __mblen_pending;
177 #endif
178 } __STDIO_fpos_t;
179
180 #ifdef __UCLIBC_HAS_LFS__
181 typedef struct {
182         __off64_t __pos;
183 #ifdef __STDIO_MBSTATE
184         __mbstate_t __mbstate;
185 #endif
186 #ifdef __UCLIBC_HAS_WCHAR__
187         int __mblen_pending;
188 #endif
189 } __STDIO_fpos64_t;
190 #endif
191
192 /**********************************************************************/
193 #ifdef __UCLIBC_HAS_LFS__
194 typedef __off64_t __offmax_t;           /* TODO -- rename this? */
195 #else
196 typedef __off_t __offmax_t;             /* TODO -- rename this? */
197 #endif
198
199 /**********************************************************************/
200 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
201
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.
208  */
209 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
210 typedef int __io_close_fn(void *__cookie);
211
212 typedef struct {
213         __io_read_fn  *read;
214         __io_write_fn *write;
215         __io_seek_fn  *seek;
216         __io_close_fn *close;
217 } _IO_cookie_io_functions_t;
218
219 #if defined(_LIBC) || defined(_GNU_SOURCE)
220
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;
225
226 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
227
228 #endif
229
230 #endif
231 /**********************************************************************/
232
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__ */
244         int __filedes;
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 */
250
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 */
257
258 #endif /* __STDIO_BUFFERS */
259
260 #ifdef __STDIO_HAS_OPENLIST
261         struct __STDIO_FILE_STRUCT *__nextopen;
262 #endif
263 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
264         void *__cookie;
265         _IO_cookie_io_functions_t __gcs;
266 #endif
267 #ifdef __UCLIBC_HAS_WCHAR__
268         wchar_t __ungot[2];
269 #endif
270 #ifdef __STDIO_MBSTATE
271         __mbstate_t __state;
272 #endif
273 #ifdef __UCLIBC_HAS_XLOCALE__
274         void *__unused;                         /* Placeholder for codeset binding. */
275 #endif
276 #ifdef __UCLIBC_HAS_THREADS__
277         int __user_locking;
278         __UCLIBC_MUTEX(__lock);
279 #endif
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 */
284 };
285
286
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]).
299  *
300  * Notes:
301  *   scanf() can NOT use ungetc() to push back characters.
302  *     (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
303  */
304
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
314
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
326
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
333  *   1 bit  : freefile
334  *   1 bit  : appending
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];
340  */
341 /**********************************************************************
342  * PROTOTYPES OF INTERNAL FUNCTIONS
343  **********************************************************************/
344 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
345
346 extern void _stdio_init(void) attribute_hidden;
347 extern void _stdio_term(void) attribute_hidden;
348
349 #ifdef __STDIO_HAS_OPENLIST
350
351 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
352
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 */
359 #endif
360 extern int _stdio_user_locking;
361 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
362 #endif
363
364 #endif
365
366 #endif
367 /**********************************************************************/
368
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)
373
374 #ifdef __UCLIBC_HAS_THREADS__
375 # define __CLEARERR(__stream)           (clearerr)(__stream)
376 # define __FERROR(__stream)             (ferror)(__stream)
377 # define __FEOF(__stream)               (feof)(__stream)
378 #else
379 # define __CLEARERR(__stream)           __CLEARERR_UNLOCKED(__stream)
380 # define __FERROR(__stream)             __FERROR_UNLOCKED(__stream)
381 # define __FEOF(__stream)               __FEOF_UNLOCKED(__stream)
382 #endif
383
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)
388
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))
396
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))
402
403
404 #ifdef __STDIO_GETC_MACRO
405
406 extern FILE *__stdin;                   /* For getchar() macro. */
407
408 # undef  __GETC_UNLOCKED_MACRO
409 # define __GETC_UNLOCKED_MACRO(__stream)                                \
410                 ( ((__stream)->__bufpos < (__stream)->__bufgetc_u)      \
411                   ? (*(__stream)->__bufpos++)                           \
412                   : __fgetc_unlocked(__stream) )
413
414 # if 0
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__
419 #   undef  __GETC
420 #   define __GETC(__stream)                             __GETC_UNLOCKED_MACRO((__stream))
421 #  endif
422
423 # else
424         /* Using gcc extension for safety and additional inlining. */
425 #  undef  __FGETC_UNLOCKED
426 #  define __FGETC_UNLOCKED(__stream)                                    \
427                 (__extension__ ({                                       \
428                         FILE *__S = (__stream);                         \
429                         __GETC_UNLOCKED_MACRO(__S);                     \
430                 }) )
431
432 #  undef  __GETC_UNLOCKED
433 #  define __GETC_UNLOCKED(__stream)             __FGETC_UNLOCKED((__stream))
434
435 #  ifdef __UCLIBC_HAS_THREADS__
436 #   undef  __FGETC
437 #   define __FGETC(__stream)                                            \
438                 (__extension__ ({                                       \
439                         FILE *__S = (__stream);                         \
440                         ((__S->__user_locking )                         \
441                          ? __GETC_UNLOCKED_MACRO(__S)                   \
442                          : (fgetc)(__S));                               \
443                 }) )
444
445 #   undef  __GETC
446 #   define __GETC(__stream)                     __FGETC((__stream))
447
448 #  else
449
450 #   undef  __FGETC
451 #   define __FGETC(__stream)                    __FGETC_UNLOCKED((__stream))
452 #   undef  __GETC
453 #   define __GETC(__stream)                     __FGETC_UNLOCKED((__stream))
454
455 #  endif
456 # endif
457
458 #else
459
460 #endif /* __STDIO_GETC_MACRO */
461
462
463 #ifdef __STDIO_PUTC_MACRO
464
465 extern FILE *__stdout;                  /* For putchar() macro. */
466
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)) )
472
473 # if 0
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__
479 #   undef  __PUTC
480 #   define __PUTC(__c, __stream)        __PUTC_UNLOCKED_MACRO((__c), (__stream))
481 #  endif
482
483 # else
484         /* Using gcc extension for safety and additional inlining. */
485
486 #  undef  __FPUTC_UNLOCKED
487 #  define __FPUTC_UNLOCKED(__c, __stream)                               \
488                 (__extension__ ({                                       \
489                         FILE *__S = (__stream);                         \
490                         __PUTC_UNLOCKED_MACRO((__c),__S);               \
491                 }) )
492
493 #  undef  __PUTC_UNLOCKED
494 #  define __PUTC_UNLOCKED(__c, __stream)        __FPUTC_UNLOCKED((__c), (__stream))
495
496 #  ifdef __UCLIBC_HAS_THREADS__
497 #   undef  __FPUTC
498 #   define __FPUTC(__c, __stream)                                       \
499                 (__extension__ ({                                       \
500                         FILE *__S = (__stream);                         \
501                         ((__S->__user_locking)                          \
502                          ? __PUTC_UNLOCKED_MACRO((__c),__S)             \
503                          : (fputc)((__c),__S));                         \
504                 }) )
505
506 #   undef  __PUTC
507 #   define __PUTC(__c, __stream)                __FPUTC((__c), (__stream))
508
509 #  else
510
511 #   undef  __FPUTC
512 #   define __FPUTC(__c, __stream)               __FPUTC_UNLOCKED((__c),(__stream))
513 #   undef  __PUTC
514 #   define __PUTC(__c, __stream)                __FPUTC_UNLOCKED((__c),(__stream))
515
516 #  endif
517 # endif
518
519 #endif /* __STDIO_PUTC_MACRO */