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.
22 #ifdef __UCLIBC_HAS_WCHAR__
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 \
49 __STDIO_THREADLOCK_OPENLIST_DEL; \
50 ++_stdio_openlist_use_count; \
51 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
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 \
62 __STDIO_THREADLOCK_OPENLIST_DEL; \
63 ++_stdio_openlist_del_count; \
64 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
67 #define __STDIO_OPENLIST_DEC_DEL_CNT \
69 __STDIO_THREADLOCK_OPENLIST_DEL; \
70 --_stdio_openlist_del_count; \
71 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
74 #endif /* __UCLIBC_HAS_THREADS__ */
75 #endif /* __STDIO_BUFFERS */
77 #ifndef __STDIO_THREADLOCK_OPENLIST_DEL
78 #define __STDIO_THREADLOCK_OPENLIST_DEL ((void)0)
80 #ifndef __STDIO_THREADUNLOCK_OPENLIST_DEL
81 #define __STDIO_THREADUNLOCK_OPENLIST_DEL ((void)0)
83 #ifndef __STDIO_OPENLIST_INC_USE
84 #define __STDIO_OPENLIST_INC_USE ((void)0)
86 #ifndef __STDIO_OPENLIST_DEC_USE
87 #define __STDIO_OPENLIST_DEC_USE ((void)0)
89 #ifndef __STDIO_OPENLIST_INC_DEL_CNT
90 #define __STDIO_OPENLIST_INC_DEL_CNT ((void)0)
92 #ifndef __STDIO_OPENLIST_DEC_DEL_CNT
93 #define __STDIO_OPENLIST_DEC_DEL_CNT ((void)0)
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 _IO_cookie_file_t *cfile = (_IO_cookie_file_t *) (S); \
116 if (cfile->__gcs.write == NULL) { \
117 __set_errno(EINVAL); \
120 __set_errno(EAGAIN); \
121 ssize_t w = cfile->__gcs.write(cfile->__cookie, ##ARGS); \
122 return (w == 0 ? -1 : w); \
126 struct __STDIO_FILE_STRUCT __fp
;
128 _IO_cookie_io_functions_t __gcs
;
131 #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
133 #undef __STDIO_STREAM_GLIBC_CUSTOM_FILEDES
134 #define __STDIO_STREAM_IS_CUSTOM(S) (0)
135 #define __STDIO_STREAM_CUSTOM_IO_FUNC(S, NAME, RC, ARGS...)
136 #define __STDIO_STREAM_CUSTOM_WRITE_FUNC(S, ARGS...)
138 #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
140 extern int __stdio_seek(FILE *stream
, register __offmax_t
*pos
, int whence
) attribute_hidden
;
142 static inline ssize_t
__READ(FILE *stream
, char *buf
, size_t bufsize
)
144 __STDIO_STREAM_CUSTOM_IO_FUNC(stream
, read
, -1, buf
, bufsize
);
146 return read(stream
->__filedes
, buf
, bufsize
);
149 static inline ssize_t
__WRITE(FILE *stream
, const char *buf
, size_t bufsize
)
151 __STDIO_STREAM_CUSTOM_WRITE_FUNC(stream
, buf
, bufsize
);
153 return write(stream
->__filedes
, buf
, bufsize
);
156 static inline int __SEEK(FILE *stream
, register __offmax_t
*pos
, int whence
)
158 __STDIO_STREAM_CUSTOM_IO_FUNC(stream
, seek
, -1, pos
, whence
);
160 return __stdio_seek(stream
, pos
, whence
);
163 static inline int __CLOSE(FILE *stream
)
165 __STDIO_STREAM_CUSTOM_IO_FUNC(stream
, close
, 0);
167 return close(stream
->__filedes
);
170 /**********************************************************************/
171 #ifdef __UCLIBC_HAS_WCHAR__
173 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w_o((S), (O))
174 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r_o((S), (O))
178 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w((S))
179 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r((S))
182 /**********************************************************************/
184 #define __STDIO_STREAM_IS_READING(S) ((S)->__modeflags & __MASK_READING)
185 #define __STDIO_STREAM_IS_WRITING(S) ((S)->__modeflags & __FLAG_WRITING)
187 #define __STDIO_STREAM_SET_READING(S) ((S)->__modeflags |= __FLAG_READING)
188 #define __STDIO_STREAM_SET_WRITING(S) ((S)->__modeflags |= __FLAG_WRITING)
190 #define __STDIO_STREAM_IS_READING_OR_READONLY(S) \
191 ((S)->__modeflags & (__MASK_READING|__FLAG_READONLY))
193 #define __STDIO_STREAM_IS_WRITING_OR_WRITEONLY(S) \
194 ((S)->__modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY))
196 #define __STDIO_STREAM_IS_READONLY(S) ((S)->__modeflags & __FLAG_READONLY)
197 #define __STDIO_STREAM_IS_WRITEONLY(S) ((S)->__modeflags & __FLAG_WRITEONLY)
200 /**********************************************************************/
201 #ifdef __UCLIBC_HAS_WCHAR__
203 #define __STDIO_STREAM_IS_NARROW_WRITING(S) \
204 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_NARROW)) \
205 == (__FLAG_WRITING|__FLAG_NARROW))
207 #define __STDIO_STREAM_IS_WIDE_WRITING(S) \
208 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_WIDE)) \
209 == (__FLAG_WRITING|__FLAG_WIDE))
211 #if (__FLAG_NARROW <= __MASK_READING)
212 #error assumption violated regarding __FLAG_NARROW
215 #define __STDIO_STREAM_IS_NARROW_READING(S) \
216 (((S)->__modeflags & (__MASK_READING|__FLAG_NARROW)) > __FLAG_NARROW)
218 #define __STDIO_STREAM_IS_WIDE_READING(S) \
219 (((S)->__modeflags & (__MASK_READING|__FLAG_WIDE)) > __FLAG_WIDE)
221 #define __STDIO_STREAM_IS_NARROW(S) ((S)->__modeflags & __FLAG_NARROW)
222 #define __STDIO_STREAM_IS_WIDE(S) ((S)->__modeflags & __FLAG_WIDE)
224 #define __STDIO_STREAM_SET_NARROW(S) \
225 ((void)((S)->__modeflags |= __FLAG_NARROW))
226 #define __STDIO_STREAM_SET_WIDE(S) \
227 ((void)((S)->__modeflags |= __FLAG_WIDE))
231 #define __STDIO_STREAM_IS_NARROW_WRITING(S) __STDIO_STREAM_IS_WRITING(S)
233 #define __STDIO_STREAM_IS_NARROW_READING(S) __STDIO_STREAM_IS_READING(S)
235 #define __STDIO_STREAM_IS_NARROW(S) (1)
236 #define __STDIO_STREAM_IS_WIDE(S) (0)
238 #define __STDIO_STREAM_SET_NARROW(S) ((void)0)
239 #define __STDIO_STREAM_SET_WIDE(S) ((void)0)
242 /**********************************************************************/
244 #define __STDIO_STREAM_SET_EOF(S) \
245 ((void)((S)->__modeflags |= __FLAG_EOF))
246 #define __STDIO_STREAM_SET_ERROR(S) \
247 ((void)((S)->__modeflags |= __FLAG_ERROR))
249 #define __STDIO_STREAM_CLEAR_EOF(S) \
250 ((void)((S)->__modeflags &= ~__FLAG_EOF))
251 #define __STDIO_STREAM_CLEAR_ERROR(S) \
252 ((void)((S)->__modeflags &= ~__FLAG_ERROR))
254 #define __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(S) \
255 ((void)((S)->__modeflags &= ~__MASK_READING))
256 #define __STDIO_STREAM_CLEAR_WRITING(S) \
257 ((void)((S)->__modeflags &= ~__FLAG_WRITING))
259 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
260 # define __STDIO_STREAM_DISABLE_GETC(S) \
261 ((void)((S)->__bufgetc_u = (S)->__bufstart))
262 # define __STDIO_STREAM_ENABLE_GETC(S) \
263 ((void)((S)->__bufgetc_u = (S)->__bufread))
264 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) \
265 ((S)->__bufpos < (S)->__bufgetc_u)
267 # define __STDIO_STREAM_DISABLE_GETC(S) ((void)0)
268 # define __STDIO_STREAM_ENABLE_GETC(S) ((void)0)
269 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) (0)
272 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
273 # define __STDIO_STREAM_DISABLE_PUTC(S) \
274 ((void)((S)->__bufputc_u = (S)->__bufstart))
275 # define __STDIO_STREAM_ENABLE_PUTC(S) \
276 ((void)((S)->__bufputc_u = (S)->__bufend))
277 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) \
278 ((S)->__bufpos < (S)->__bufputc_u)
280 # define __STDIO_STREAM_DISABLE_PUTC(S) ((void)0)
281 # define __STDIO_STREAM_ENABLE_PUTC(S) ((void)0)
282 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) (0)
285 /**********************************************************************/
287 #ifdef __STDIO_BUFFERS
288 #define __STDIO_STREAM_FREE_BUFFER(S) \
289 do { if ((S)->__modeflags & __FLAG_FREEBUF) \
290 free((S)->__bufstart); } while (0)
292 #define __STDIO_STREAM_FREE_BUFFER(S) ((void)0)
295 #define __STDIO_STREAM_FREE_FILE(S) \
296 do { if ((S)->__modeflags & __FLAG_FREEFILE) \
297 free((S)); } while (0)
300 #ifdef __UCLIBC_HAS_LFS__
301 #define __STDIO_WHEN_LFS(E) E
303 #define __STDIO_WHEN_LFS(E) ((void)0)
306 /**********************************************************************/
307 /* The following return 0 on success. */
309 #ifdef __STDIO_BUFFERS
310 /* Assume stream in valid writing state. Do not reset writing flag
311 * or disble putc macro unless error. */
312 /* Should we assume that buffer is not empty to avoid a check? */
313 extern size_t __stdio_wcommit(FILE *__restrict stream
) attribute_hidden
;
315 /* Remember to fail if at EOF! */
316 extern size_t __stdio_rfill(FILE *__restrict stream
) attribute_hidden
;
318 extern size_t __stdio_fwrite(const unsigned char *__restrict buffer
,
319 size_t bytes
, FILE *__restrict stream
) attribute_hidden
;
322 #define __stdio_fwrite(B,N,S) __stdio_WRITE((S),(B),(N))
326 extern size_t __stdio_WRITE(FILE *stream
, const unsigned char *buf
,
327 size_t bufsize
) attribute_hidden
;
328 extern size_t __stdio_READ(FILE *stream
, unsigned char *buf
,
329 size_t bufsize
) attribute_hidden
;
331 extern int __stdio_trans2r(FILE *__restrict stream
) attribute_hidden
;
332 extern int __stdio_trans2w(FILE *__restrict stream
) attribute_hidden
;
334 extern int __stdio_trans2r_o(FILE *__restrict stream
, int oflag
) attribute_hidden
;
335 extern int __stdio_trans2w_o(FILE *__restrict stream
, int oflag
) attribute_hidden
;
337 extern uintmax_t _load_inttype(int desttype
, register const void *src
, int uflag
) attribute_hidden
;
338 extern void _store_inttype(void *dest
, int desttype
, uintmax_t val
) attribute_hidden
;
340 /**********************************************************************/
341 #ifdef __STDIO_BUFFERS
343 #define __STDIO_STREAM_IS_FBF(S) (!((S)->__modeflags & __MASK_BUFMODE))
344 #define __STDIO_STREAM_IS_LBF(S) ((S)->__modeflags & __FLAG_LBF)
345 #define __STDIO_STREAM_IS_NBF(S) ((S)->__modeflags & __FLAG_NBF)
347 #define __STDIO_STREAM_BUFFER_SIZE(S) ((S)->__bufend - (S)->__bufstart)
349 /* Valid when writing... */
350 #define __STDIO_STREAM_BUFFER_ADD(S,C) (*(S)->__bufpos++ = (C))
351 #define __STDIO_STREAM_BUFFER_UNADD(S) (--(S)->__bufpos)
352 #define __STDIO_STREAM_BUFFER_WAVAIL(S) ((S)->__bufend - (S)->__bufpos)
353 #define __STDIO_STREAM_BUFFER_WUSED(S) ((S)->__bufpos - (S)->__bufstart)
354 #define __STDIO_COMMIT_WRITE_BUFFER(S) __stdio_wcommit((S))
355 #ifdef __UCLIBC_HAS_WCHAR__
356 #define __STDIO_STREAM_IS_NARROW_FBF(S) \
357 (!((S)->__modeflags & (__MASK_BUFMODE|__FLAG_WIDE)))
359 #define __STDIO_STREAM_IS_NARROW_FBF(S) __STDIO_STREAM_IS_FBF((S))
362 /* Valid when reading... */
363 #define __STDIO_STREAM_BUFFER_RAVAIL(S) ((S)->__bufread - (S)->__bufpos)
364 #define __STDIO_STREAM_BUFFER_GET(S) (*(S)->__bufpos++)
365 #define __STDIO_FILL_READ_BUFFER(S) __stdio_rfill((S))
367 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) \
368 (S)->__bufread = (S)->__bufpos = (S)->__bufstart
371 #define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES (-3)
372 #define __STDIO_STREAM_FAKE_VSSCANF_FILEDES (-3)
373 #define __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES (-4)
374 #define __STDIO_STREAM_FAKE_VSWSCANF_FILEDES (-4)
376 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) \
377 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES)
378 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) \
379 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES)
380 #define __STDIO_STREAM_IS_FAKE_VSWPRINTF(S) \
381 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES)
382 #define __STDIO_STREAM_IS_FAKE_VSWSCANF(S) \
383 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWSCANF_FILEDES)
385 #else /* __STDIO_BUFFERS */
387 #define __STDIO_STREAM_IS_FBF(S) (0)
388 #define __STDIO_STREAM_IS_LBF(S) (0)
389 #define __STDIO_STREAM_IS_NBF(S) (1)
391 #define __STDIO_STREAM_BUFFER_SIZE(S) (0)
392 #define __STDIO_STREAM_BUFFER_ADD(S,C) ((void)0)
393 #define __STDIO_STREAM_BUFFER_UNADD(S) ((void)0)
394 #define __STDIO_STREAM_BUFFER_WAVAIL(S) (0)
395 #define __STDIO_STREAM_BUFFER_WUSED(S) (0)
396 #define __STDIO_COMMIT_WRITE_BUFFER(S) (0)
397 #define __STDIO_STREAM_IS_NARROW_FBF(S) (0)
399 #define __STDIO_STREAM_BUFFER_RAVAIL(S) (0)
400 #define __STDIO_STREAM_BUFFER_GET(S) (EOF)
401 #define __STDIO_FILL_READ_BUFFER(S) (0)
402 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) ((void)0)
404 #undef __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES
405 #undef __STDIO_STREAM_FAKE_VSSCANF_FILEDES
406 #undef __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES
408 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) (0)
409 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) (0)
410 #undef __STDIO_STREAM_IS_FAKE_VSWPRINTF
411 #undef __STDIO_STREAM_IS_FAKE_VSWSCANF
413 # ifdef __USE_OLD_VFPRINTF__
414 # define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB (-2)
415 # define __STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(S) \
416 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB)
419 # ifndef __UCLIBC_HAS_WCHAR__
420 # define __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB (-2)
421 # define __STDIO_STREAM_IS_FAKE_VSSCANF_NB(S) \
422 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB)
425 #endif /* __STDIO_BUFFERS */
426 /**********************************************************************/
428 extern int __stdio_adjust_position(FILE *__restrict stream
, __offmax_t
*pos
) attribute_hidden
;
430 #ifdef __STDIO_HAS_OPENLIST
431 /* Uses an implementation hack!!! */
432 #define __STDIO_FLUSH_LBF_STREAMS \
433 fflush_unlocked((FILE *) &_stdio_openlist)
435 #define __STDIO_FLUSH_LBF_STREAMS ((void)0)
439 #define __STDIO_STREAM_VALIDATE(S) ((void)0)
441 extern void _stdio_validate_FILE(const FILE *stream
) attribute_hidden
;
442 #define __STDIO_STREAM_VALIDATE(S) _stdio_validate_FILE((S))
445 #ifdef __STDIO_MBSTATE
446 #define __COPY_MBSTATE(dest,src) \
447 ((void)((dest)->__mask = (src)->__mask, (dest)->__wc = (src)->__wc))
448 #define __INIT_MBSTATE(dest) ((void)((dest)->__mask = 0))
450 #define __COPY_MBSTATE(dest,src) ((void)0)
451 #define __INIT_MBSTATE(dest) ((void)0)
454 /**********************************************************************/
456 extern FILE *_stdio_fopen(intptr_t fname_or_mode
, const char *__restrict mode
,
457 FILE *__restrict stream
, int filedes
) attribute_hidden
;
459 #ifdef __UCLIBC_HAS_WCHAR__
460 extern size_t _wstdio_fwrite(const wchar_t *__restrict ws
,
461 size_t n
, FILE *__restrict stream
) attribute_hidden
;
464 /**********************************************************************/
466 extern int _vfprintf_internal (FILE * __restrict stream
,
467 const char * __restrict format
,
468 va_list arg
) attribute_hidden
;
470 #ifdef __UCLIBC_HAS_WCHAR__
471 extern int _vfwprintf_internal (FILE * __restrict stream
,
472 const wchar_t * __restrict format
,
473 va_list arg
) attribute_hidden
;
476 /**********************************************************************/
477 /* Only use the macro below if you know fp is a valid FILE for a valid fd.
478 * This is _not_ true for custom streams! */
479 #define __FILENO_UNLOCKED(fp) ((fp)->__filedes)
481 #define __FEOF_OR_FERROR_UNLOCKED(stream) \
482 ((stream)->__modeflags & (__FLAG_EOF|__FLAG_ERROR))
484 #if defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
485 #define __STDIO_HAS_VSNPRINTF 1
488 #endif /* __STDIO_H_I */