netinet/in.h: add IPPROTO_MH
[uclibc-ng.git] / libc / stdio / _stdio.h
blob85464fa42996da1dda1364811eab4ea24ef8e549
1 /* Copyright (C) 2004-2005 Manuel Novoa III <mjn3@codepoet.org>
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
7 #ifndef __STDIO_H_I
8 #define __STDIO_H_I 1
10 #include <features.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <limits.h>
15 #include <signal.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #ifdef __UCLIBC_HAS_WCHAR__
23 #include <wchar.h>
24 #endif
26 #include <bits/uClibc_mutex.h>
28 #define __STDIO_THREADLOCK_OPENLIST_ADD \
29 __UCLIBC_IO_MUTEX_LOCK(_stdio_openlist_add_lock)
31 #define __STDIO_THREADUNLOCK_OPENLIST_ADD \
32 __UCLIBC_IO_MUTEX_UNLOCK(_stdio_openlist_add_lock)
34 #ifdef __STDIO_BUFFERS
36 #define __STDIO_THREADLOCK_OPENLIST_DEL \
37 __UCLIBC_IO_MUTEX_LOCK(_stdio_openlist_del_lock)
39 #define __STDIO_THREADUNLOCK_OPENLIST_DEL \
40 __UCLIBC_IO_MUTEX_UNLOCK(_stdio_openlist_del_lock)
43 #ifdef __UCLIBC_HAS_THREADS__
44 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
46 extern volatile int _stdio_openlist_use_count attribute_hidden; /* _stdio_openlist_del_lock */
47 #define __STDIO_OPENLIST_INC_USE \
48 do { \
49 __STDIO_THREADLOCK_OPENLIST_DEL; \
50 ++_stdio_openlist_use_count; \
51 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
52 } while (0)
54 extern void _stdio_openlist_dec_use(void) attribute_hidden;
56 #define __STDIO_OPENLIST_DEC_USE \
57 _stdio_openlist_dec_use()
59 extern int _stdio_openlist_del_count attribute_hidden; /* _stdio_openlist_del_lock */
60 #define __STDIO_OPENLIST_INC_DEL_CNT \
61 do { \
62 __STDIO_THREADLOCK_OPENLIST_DEL; \
63 ++_stdio_openlist_del_count; \
64 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
65 } while (0)
67 #define __STDIO_OPENLIST_DEC_DEL_CNT \
68 do { \
69 __STDIO_THREADLOCK_OPENLIST_DEL; \
70 --_stdio_openlist_del_count; \
71 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
72 } while (0)
74 #endif /* __UCLIBC_HAS_THREADS__ */
75 #endif /* __STDIO_BUFFERS */
77 #ifndef __STDIO_THREADLOCK_OPENLIST_DEL
78 #define __STDIO_THREADLOCK_OPENLIST_DEL ((void)0)
79 #endif
80 #ifndef __STDIO_THREADUNLOCK_OPENLIST_DEL
81 #define __STDIO_THREADUNLOCK_OPENLIST_DEL ((void)0)
82 #endif
83 #ifndef __STDIO_OPENLIST_INC_USE
84 #define __STDIO_OPENLIST_INC_USE ((void)0)
85 #endif
86 #ifndef __STDIO_OPENLIST_DEC_USE
87 #define __STDIO_OPENLIST_DEC_USE ((void)0)
88 #endif
89 #ifndef __STDIO_OPENLIST_INC_DEL_CNT
90 #define __STDIO_OPENLIST_INC_DEL_CNT ((void)0)
91 #endif
92 #ifndef __STDIO_OPENLIST_DEC_DEL_CNT
93 #define __STDIO_OPENLIST_DEC_DEL_CNT ((void)0)
94 #endif
96 #define __UNDEFINED_OR_NONPORTABLE ((void)0)
98 /**********************************************************************/
99 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
101 #define __STDIO_STREAM_GLIBC_CUSTOM_FILEDES (-2)
103 #define __STDIO_STREAM_IS_CUSTOM(S) \
104 ((S)->__filedes == __STDIO_STREAM_GLIBC_CUSTOM_FILEDES)
106 #define __STDIO_STREAM_CUSTOM_IO_FUNC(S, NAME, RC, ARGS...) \
107 if (__STDIO_STREAM_IS_CUSTOM((S))) { \
108 _IO_cookie_file_t *cfile = (_IO_cookie_file_t *) (S); \
109 return (cfile->__gcs.NAME == NULL) ? (RC) : \
110 cfile->__gcs.NAME(cfile->__cookie, ##ARGS); \
113 #define __STDIO_STREAM_CUSTOM_WRITE_FUNC(S, ARGS...) \
114 if (__STDIO_STREAM_IS_CUSTOM((S))) { \
115 ssize_t w; \
116 _IO_cookie_file_t *cfile = (_IO_cookie_file_t *) (S); \
117 if (cfile->__gcs.write == NULL) { \
118 __set_errno(EINVAL); \
119 return -1; \
121 __set_errno(EAGAIN); \
122 w = cfile->__gcs.write(cfile->__cookie, ##ARGS); \
123 return (w == 0 ? -1 : w); \
126 typedef struct {
127 struct __STDIO_FILE_STRUCT __fp;
128 void *__cookie;
129 _IO_cookie_io_functions_t __gcs;
130 } _IO_cookie_file_t;
132 #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
134 #undef __STDIO_STREAM_GLIBC_CUSTOM_FILEDES
135 #define __STDIO_STREAM_IS_CUSTOM(S) (0)
136 #define __STDIO_STREAM_CUSTOM_IO_FUNC(S, NAME, RC, ARGS...)
137 #define __STDIO_STREAM_CUSTOM_WRITE_FUNC(S, ARGS...)
139 #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
141 extern int __stdio_seek(FILE *stream, register __offmax_t *pos, int whence) attribute_hidden;
143 static inline ssize_t __READ(FILE *stream, char *buf, size_t bufsize)
145 __STDIO_STREAM_CUSTOM_IO_FUNC(stream, read, -1, buf, bufsize);
147 return read(stream->__filedes, buf, bufsize);
150 static inline ssize_t __WRITE(FILE *stream, const char *buf, size_t bufsize)
152 __STDIO_STREAM_CUSTOM_WRITE_FUNC(stream, buf, bufsize);
154 return write(stream->__filedes, buf, bufsize);
157 static inline int __SEEK(FILE *stream, register __offmax_t *pos, int whence)
159 __STDIO_STREAM_CUSTOM_IO_FUNC(stream, seek, -1, pos, whence);
161 return __stdio_seek(stream, pos, whence);
164 static inline int __CLOSE(FILE *stream)
166 __STDIO_STREAM_CUSTOM_IO_FUNC(stream, close, 0);
168 return close(stream->__filedes);
171 /**********************************************************************/
172 #ifdef __UCLIBC_HAS_WCHAR__
174 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w_o((S), (O))
175 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r_o((S), (O))
177 #else
179 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w((S))
180 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r((S))
182 #endif
183 /**********************************************************************/
185 #define __STDIO_STREAM_IS_READING(S) ((S)->__modeflags & __MASK_READING)
186 #define __STDIO_STREAM_IS_WRITING(S) ((S)->__modeflags & __FLAG_WRITING)
188 #define __STDIO_STREAM_SET_READING(S) ((S)->__modeflags |= __FLAG_READING)
189 #define __STDIO_STREAM_SET_WRITING(S) ((S)->__modeflags |= __FLAG_WRITING)
191 #define __STDIO_STREAM_IS_READING_OR_READONLY(S) \
192 ((S)->__modeflags & (__MASK_READING|__FLAG_READONLY))
194 #define __STDIO_STREAM_IS_WRITING_OR_WRITEONLY(S) \
195 ((S)->__modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY))
197 #define __STDIO_STREAM_IS_READONLY(S) ((S)->__modeflags & __FLAG_READONLY)
198 #define __STDIO_STREAM_IS_WRITEONLY(S) ((S)->__modeflags & __FLAG_WRITEONLY)
201 /**********************************************************************/
202 #ifdef __UCLIBC_HAS_WCHAR__
204 #define __STDIO_STREAM_IS_NARROW_WRITING(S) \
205 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_NARROW)) \
206 == (__FLAG_WRITING|__FLAG_NARROW))
208 #define __STDIO_STREAM_IS_WIDE_WRITING(S) \
209 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_WIDE)) \
210 == (__FLAG_WRITING|__FLAG_WIDE))
212 #if (__FLAG_NARROW <= __MASK_READING)
213 #error assumption violated regarding __FLAG_NARROW
214 #endif
216 #define __STDIO_STREAM_IS_NARROW_READING(S) \
217 (((S)->__modeflags & (__MASK_READING|__FLAG_NARROW)) > __FLAG_NARROW)
219 #define __STDIO_STREAM_IS_WIDE_READING(S) \
220 (((S)->__modeflags & (__MASK_READING|__FLAG_WIDE)) > __FLAG_WIDE)
222 #define __STDIO_STREAM_IS_NARROW(S) ((S)->__modeflags & __FLAG_NARROW)
223 #define __STDIO_STREAM_IS_WIDE(S) ((S)->__modeflags & __FLAG_WIDE)
225 #define __STDIO_STREAM_SET_NARROW(S) \
226 ((void)((S)->__modeflags |= __FLAG_NARROW))
227 #define __STDIO_STREAM_SET_WIDE(S) \
228 ((void)((S)->__modeflags |= __FLAG_WIDE))
230 #else
232 #define __STDIO_STREAM_IS_NARROW_WRITING(S) __STDIO_STREAM_IS_WRITING(S)
234 #define __STDIO_STREAM_IS_NARROW_READING(S) __STDIO_STREAM_IS_READING(S)
236 #define __STDIO_STREAM_IS_NARROW(S) (1)
237 #define __STDIO_STREAM_IS_WIDE(S) (0)
239 #define __STDIO_STREAM_SET_NARROW(S) ((void)0)
240 #define __STDIO_STREAM_SET_WIDE(S) ((void)0)
242 #endif
243 /**********************************************************************/
245 #define __STDIO_STREAM_SET_EOF(S) \
246 ((void)((S)->__modeflags |= __FLAG_EOF))
247 #define __STDIO_STREAM_SET_ERROR(S) \
248 ((void)((S)->__modeflags |= __FLAG_ERROR))
250 #define __STDIO_STREAM_CLEAR_EOF(S) \
251 ((void)((S)->__modeflags &= ~__FLAG_EOF))
252 #define __STDIO_STREAM_CLEAR_ERROR(S) \
253 ((void)((S)->__modeflags &= ~__FLAG_ERROR))
255 #define __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(S) \
256 ((void)((S)->__modeflags &= ~__MASK_READING))
257 #define __STDIO_STREAM_CLEAR_WRITING(S) \
258 ((void)((S)->__modeflags &= ~__FLAG_WRITING))
260 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
261 # define __STDIO_STREAM_DISABLE_GETC(S) \
262 ((void)((S)->__bufgetc_u = (S)->__bufstart))
263 # define __STDIO_STREAM_ENABLE_GETC(S) \
264 ((void)((S)->__bufgetc_u = (S)->__bufread))
265 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) \
266 ((S)->__bufpos < (S)->__bufgetc_u)
267 #else
268 # define __STDIO_STREAM_DISABLE_GETC(S) ((void)0)
269 # define __STDIO_STREAM_ENABLE_GETC(S) ((void)0)
270 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) (0)
271 #endif
273 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
274 # define __STDIO_STREAM_DISABLE_PUTC(S) \
275 ((void)((S)->__bufputc_u = (S)->__bufstart))
276 # define __STDIO_STREAM_ENABLE_PUTC(S) \
277 ((void)((S)->__bufputc_u = (S)->__bufend))
278 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) \
279 ((S)->__bufpos < (S)->__bufputc_u)
280 #else
281 # define __STDIO_STREAM_DISABLE_PUTC(S) ((void)0)
282 # define __STDIO_STREAM_ENABLE_PUTC(S) ((void)0)
283 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) (0)
284 #endif
286 /**********************************************************************/
288 #ifdef __STDIO_BUFFERS
289 #define __STDIO_STREAM_FREE_BUFFER(S) \
290 do { if ((S)->__modeflags & __FLAG_FREEBUF) \
291 free((S)->__bufstart); } while (0)
292 #else
293 #define __STDIO_STREAM_FREE_BUFFER(S) ((void)0)
294 #endif
296 #define __STDIO_STREAM_FREE_FILE(S) \
297 do { if ((S)->__modeflags & __FLAG_FREEFILE) \
298 free((S)); } while (0)
301 #define __STDIO_WHEN_LFS(E) E
303 /**********************************************************************/
304 /* The following return 0 on success. */
306 #ifdef __STDIO_BUFFERS
307 /* Assume stream in valid writing state. Do not reset writing flag
308 * or disble putc macro unless error. */
309 /* Should we assume that buffer is not empty to avoid a check? */
310 extern size_t __stdio_wcommit(FILE *__restrict stream) attribute_hidden;
312 /* Remember to fail if at EOF! */
313 extern size_t __stdio_rfill(FILE *__restrict stream) attribute_hidden;
315 extern size_t __stdio_fwrite(const unsigned char *__restrict buffer,
316 size_t bytes, FILE *__restrict stream) attribute_hidden;
317 #else
319 #define __stdio_fwrite(B,N,S) __stdio_WRITE((S),(B),(N))
321 #endif
323 extern size_t __stdio_WRITE(FILE *stream, const unsigned char *buf,
324 size_t bufsize) attribute_hidden;
325 extern size_t __stdio_READ(FILE *stream, unsigned char *buf,
326 size_t bufsize) attribute_hidden;
328 extern int __stdio_trans2r(FILE *__restrict stream) attribute_hidden;
329 extern int __stdio_trans2w(FILE *__restrict stream) attribute_hidden;
331 extern int __stdio_trans2r_o(FILE *__restrict stream, int oflag) attribute_hidden;
332 extern int __stdio_trans2w_o(FILE *__restrict stream, int oflag) attribute_hidden;
334 extern uintmax_t _load_inttype(int desttype, register const void *src, int uflag) attribute_hidden;
335 extern void _store_inttype(void *dest, int desttype, uintmax_t val) attribute_hidden;
337 /**********************************************************************/
338 #ifdef __STDIO_BUFFERS
340 #define __STDIO_STREAM_IS_FBF(S) (!((S)->__modeflags & __MASK_BUFMODE))
341 #define __STDIO_STREAM_IS_LBF(S) ((S)->__modeflags & __FLAG_LBF)
342 #define __STDIO_STREAM_IS_NBF(S) ((S)->__modeflags & __FLAG_NBF)
344 #define __STDIO_STREAM_BUFFER_SIZE(S) ((S)->__bufend - (S)->__bufstart)
346 /* Valid when writing... */
347 #define __STDIO_STREAM_BUFFER_ADD(S,C) (*(S)->__bufpos++ = (C))
348 #define __STDIO_STREAM_BUFFER_UNADD(S) (--(S)->__bufpos)
349 #define __STDIO_STREAM_BUFFER_WAVAIL(S) ((S)->__bufend - (S)->__bufpos)
350 #define __STDIO_STREAM_BUFFER_WUSED(S) ((S)->__bufpos - (S)->__bufstart)
351 #define __STDIO_COMMIT_WRITE_BUFFER(S) __stdio_wcommit((S))
352 #ifdef __UCLIBC_HAS_WCHAR__
353 #define __STDIO_STREAM_IS_NARROW_FBF(S) \
354 (!((S)->__modeflags & (__MASK_BUFMODE|__FLAG_WIDE)))
355 #else
356 #define __STDIO_STREAM_IS_NARROW_FBF(S) __STDIO_STREAM_IS_FBF((S))
357 #endif
359 /* Valid when reading... */
360 #define __STDIO_STREAM_BUFFER_RAVAIL(S) ((S)->__bufread - (S)->__bufpos)
361 #define __STDIO_STREAM_BUFFER_GET(S) (*(S)->__bufpos++)
362 #define __STDIO_FILL_READ_BUFFER(S) __stdio_rfill((S))
364 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) \
365 (S)->__bufread = (S)->__bufpos = (S)->__bufstart
368 #define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES (-3)
369 #define __STDIO_STREAM_FAKE_VSSCANF_FILEDES (-3)
370 #define __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES (-4)
371 #define __STDIO_STREAM_FAKE_VSWSCANF_FILEDES (-4)
373 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) \
374 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES)
375 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) \
376 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES)
377 #define __STDIO_STREAM_IS_FAKE_VSWPRINTF(S) \
378 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES)
379 #define __STDIO_STREAM_IS_FAKE_VSWSCANF(S) \
380 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWSCANF_FILEDES)
382 #else /* __STDIO_BUFFERS */
384 #define __STDIO_STREAM_IS_FBF(S) (0)
385 #define __STDIO_STREAM_IS_LBF(S) (0)
386 #define __STDIO_STREAM_IS_NBF(S) (1)
388 #define __STDIO_STREAM_BUFFER_SIZE(S) (0)
389 #define __STDIO_STREAM_BUFFER_ADD(S,C) ((void)0)
390 #define __STDIO_STREAM_BUFFER_UNADD(S) ((void)0)
391 #define __STDIO_STREAM_BUFFER_WAVAIL(S) (0)
392 #define __STDIO_STREAM_BUFFER_WUSED(S) (0)
393 #define __STDIO_COMMIT_WRITE_BUFFER(S) (0)
394 #define __STDIO_STREAM_IS_NARROW_FBF(S) (0)
396 #define __STDIO_STREAM_BUFFER_RAVAIL(S) (0)
397 #define __STDIO_STREAM_BUFFER_GET(S) (EOF)
398 #define __STDIO_FILL_READ_BUFFER(S) (0)
399 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) ((void)0)
401 #undef __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES
402 #undef __STDIO_STREAM_FAKE_VSSCANF_FILEDES
403 #undef __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES
405 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) (0)
406 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) (0)
407 #undef __STDIO_STREAM_IS_FAKE_VSWPRINTF
408 #undef __STDIO_STREAM_IS_FAKE_VSWSCANF
410 # ifdef __USE_OLD_VFPRINTF__
411 # define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB (-2)
412 # define __STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(S) \
413 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB)
414 # endif
416 # ifndef __UCLIBC_HAS_WCHAR__
417 # define __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB (-2)
418 # define __STDIO_STREAM_IS_FAKE_VSSCANF_NB(S) \
419 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB)
420 # endif
422 #endif /* __STDIO_BUFFERS */
423 /**********************************************************************/
425 extern int __stdio_adjust_position(FILE *__restrict stream, __offmax_t *pos) attribute_hidden;
427 #ifdef __STDIO_HAS_OPENLIST
428 /* Uses an implementation hack!!! */
429 #define __STDIO_FLUSH_LBF_STREAMS \
430 fflush_unlocked((FILE *) &_stdio_openlist)
431 #else
432 #define __STDIO_FLUSH_LBF_STREAMS ((void)0)
433 #endif
435 #ifdef NDEBUG
436 #define __STDIO_STREAM_VALIDATE(S) ((void)0)
437 #else
438 extern void _stdio_validate_FILE(const FILE *stream) attribute_hidden;
439 #define __STDIO_STREAM_VALIDATE(S) _stdio_validate_FILE((S))
440 #endif
442 #ifdef __STDIO_MBSTATE
443 #define __COPY_MBSTATE(dest,src) \
444 ((void)((dest)->__mask = (src)->__mask, (dest)->__wc = (src)->__wc))
445 #define __INIT_MBSTATE(dest) ((void)((dest)->__mask = 0))
446 #else
447 #define __COPY_MBSTATE(dest,src) ((void)0)
448 #define __INIT_MBSTATE(dest) ((void)0)
449 #endif
451 /**********************************************************************/
453 extern FILE *_stdio_fopen(intptr_t fname_or_mode, const char *__restrict mode,
454 FILE *__restrict stream, int filedes) attribute_hidden;
456 #ifdef __UCLIBC_HAS_WCHAR__
457 extern size_t _wstdio_fwrite(const wchar_t *__restrict ws,
458 size_t n, FILE *__restrict stream) attribute_hidden;
459 #endif
461 /**********************************************************************/
463 extern int _vfprintf_internal (FILE * __restrict stream,
464 const char * __restrict format,
465 va_list arg) attribute_hidden;
467 #ifdef __UCLIBC_HAS_WCHAR__
468 extern int _vfwprintf_internal (FILE * __restrict stream,
469 const wchar_t * __restrict format,
470 va_list arg) attribute_hidden;
471 #endif
473 /**********************************************************************/
474 /* Only use the macro below if you know fp is a valid FILE for a valid fd.
475 * This is _not_ true for custom streams! */
476 #define __FILENO_UNLOCKED(fp) ((fp)->__filedes)
478 #define __FEOF_OR_FERROR_UNLOCKED(stream) \
479 ((stream)->__modeflags & (__FLAG_EOF|__FLAG_ERROR))
481 #if defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
482 #define __STDIO_HAS_VSNPRINTF 1
483 #endif
485 #endif /* __STDIO_H_I */