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.
20 #ifdef __UCLIBC_HAS_WCHAR__
24 #include <bits/uClibc_mutex.h>
26 #define __STDIO_THREADLOCK_OPENLIST_ADD \
27 __UCLIBC_IO_MUTEX_LOCK(_stdio_openlist_add_lock)
29 #define __STDIO_THREADUNLOCK_OPENLIST_ADD \
30 __UCLIBC_IO_MUTEX_UNLOCK(_stdio_openlist_add_lock)
32 #ifdef __STDIO_BUFFERS
34 #define __STDIO_THREADLOCK_OPENLIST_DEL \
35 __UCLIBC_IO_MUTEX_LOCK(_stdio_openlist_del_lock)
37 #define __STDIO_THREADUNLOCK_OPENLIST_DEL \
38 __UCLIBC_IO_MUTEX_UNLOCK(_stdio_openlist_del_lock)
41 #ifdef __UCLIBC_HAS_THREADS__
42 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
44 extern volatile int _stdio_openlist_use_count attribute_hidden; /* _stdio_openlist_del_lock */
45 #define __STDIO_OPENLIST_INC_USE \
47 __STDIO_THREADLOCK_OPENLIST_DEL; \
48 ++_stdio_openlist_use_count; \
49 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
52 extern void _stdio_openlist_dec_use(void) attribute_hidden;
54 #define __STDIO_OPENLIST_DEC_USE \
55 _stdio_openlist_dec_use()
57 extern int _stdio_openlist_del_count attribute_hidden; /* _stdio_openlist_del_lock */
58 #define __STDIO_OPENLIST_INC_DEL_CNT \
60 __STDIO_THREADLOCK_OPENLIST_DEL; \
61 ++_stdio_openlist_del_count; \
62 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
65 #define __STDIO_OPENLIST_DEC_DEL_CNT \
67 __STDIO_THREADLOCK_OPENLIST_DEL; \
68 --_stdio_openlist_del_count; \
69 __STDIO_THREADUNLOCK_OPENLIST_DEL; \
72 #endif /* __UCLIBC_HAS_THREADS__ */
73 #endif /* __STDIO_BUFFERS */
75 #ifndef __STDIO_THREADLOCK_OPENLIST_DEL
76 #define __STDIO_THREADLOCK_OPENLIST_DEL ((void)0)
78 #ifndef __STDIO_THREADUNLOCK_OPENLIST_DEL
79 #define __STDIO_THREADUNLOCK_OPENLIST_DEL ((void)0)
81 #ifndef __STDIO_OPENLIST_INC_USE
82 #define __STDIO_OPENLIST_INC_USE ((void)0)
84 #ifndef __STDIO_OPENLIST_DEC_USE
85 #define __STDIO_OPENLIST_DEC_USE ((void)0)
87 #ifndef __STDIO_OPENLIST_INC_DEL_CNT
88 #define __STDIO_OPENLIST_INC_DEL_CNT ((void)0)
90 #ifndef __STDIO_OPENLIST_DEC_DEL_CNT
91 #define __STDIO_OPENLIST_DEC_DEL_CNT ((void)0)
94 #define __UNDEFINED_OR_NONPORTABLE ((void)0)
96 /**********************************************************************/
97 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
99 extern __ssize_t _cs_read(void *cookie, char *buf, size_t bufsize) attribute_hidden;
100 extern __ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize) attribute_hidden;
101 extern int _cs_seek(void *cookie, __offmax_t *pos, int whence) attribute_hidden;
102 extern int _cs_close(void *cookie) attribute_hidden;
104 #define __STDIO_STREAM_RESET_GCS(S) \
105 (S)->__cookie = &((S)->__filedes); \
106 (S)->__gcs.read = _cs_read; \
107 (S)->__gcs.write = _cs_write; \
108 (S)->__gcs.seek = _cs_seek; \
109 (S)->__gcs.close = _cs_close
112 #define __READ(STREAMPTR,BUF,SIZE) \
113 ((((STREAMPTR)->__gcs.read) == NULL) ? -1 : \
114 (((STREAMPTR)->__gcs.read)((STREAMPTR)->__cookie,(BUF),(SIZE))))
115 #define __WRITE(STREAMPTR,BUF,SIZE) \
116 ((((STREAMPTR)->__gcs.write) == NULL) ? -1 : \
117 (((STREAMPTR)->__gcs.write)((STREAMPTR)->__cookie,(BUF),(SIZE))))
118 #define __SEEK(STREAMPTR,PPOS,WHENCE) \
119 ((((STREAMPTR)->__gcs.seek) == NULL) ? -1 : \
120 (((STREAMPTR)->__gcs.seek)((STREAMPTR)->__cookie,(PPOS),(WHENCE))))
121 #define __CLOSE(STREAMPTR) \
122 ((((STREAMPTR)->__gcs.close) == NULL) ? 0 : \
123 (((STREAMPTR)->__gcs.close)((STREAMPTR)->__cookie)))
125 #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
127 extern int __stdio_seek(FILE *stream, register __offmax_t *pos, int whence) attribute_hidden;
129 #define __STDIO_STREAM_RESET_GCS(S) ((void)0)
131 #define __READ(STREAMPTR,BUF,SIZE) \
132 (read((STREAMPTR)->__filedes,(BUF),(SIZE)))
133 #define __WRITE(STREAMPTR,BUF,SIZE) \
134 (write((STREAMPTR)->__filedes,(BUF),(SIZE)))
135 #define __SEEK(STREAMPTR,PPOS,WHENCE) \
136 (__stdio_seek((STREAMPTR),(PPOS),(WHENCE)))
137 #define __CLOSE(STREAMPTR) \
138 (close((STREAMPTR)->__filedes))
140 #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
142 /**********************************************************************/
143 #ifdef __UCLIBC_HAS_WCHAR__
145 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w_o((S), (O))
146 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r_o((S), (O))
150 #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w((S))
151 #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r((S))
154 /**********************************************************************/
156 #define __STDIO_STREAM_IS_READING(S) ((S)->__modeflags & __MASK_READING)
157 #define __STDIO_STREAM_IS_WRITING(S) ((S)->__modeflags & __FLAG_WRITING)
159 #define __STDIO_STREAM_SET_READING(S) ((S)->__modeflags |= __FLAG_READING)
160 #define __STDIO_STREAM_SET_WRITING(S) ((S)->__modeflags |= __FLAG_WRITING)
162 #define __STDIO_STREAM_IS_READING_OR_READONLY(S) \
163 ((S)->__modeflags & (__MASK_READING|__FLAG_READONLY))
165 #define __STDIO_STREAM_IS_WRITING_OR_WRITEONLY(S) \
166 ((S)->__modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY))
168 #define __STDIO_STREAM_IS_READONLY(S) ((S)->__modeflags & __FLAG_READONLY)
169 #define __STDIO_STREAM_IS_WRITEONLY(S) ((S)->__modeflags & __FLAG_WRITEONLY)
172 /**********************************************************************/
173 #ifdef __UCLIBC_HAS_WCHAR__
175 #define __STDIO_STREAM_IS_NARROW_WRITING(S) \
176 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_NARROW)) \
177 == (__FLAG_WRITING|__FLAG_NARROW))
179 #define __STDIO_STREAM_IS_WIDE_WRITING(S) \
180 (((S)->__modeflags & (__FLAG_WRITING|__FLAG_WIDE)) \
181 == (__FLAG_WRITING|__FLAG_WIDE))
183 #if (__FLAG_NARROW <= __MASK_READING)
184 #error assumption violated regarding __FLAG_NARROW
187 #define __STDIO_STREAM_IS_NARROW_READING(S) \
188 (((S)->__modeflags & (__MASK_READING|__FLAG_NARROW)) > __FLAG_NARROW)
190 #define __STDIO_STREAM_IS_WIDE_READING(S) \
191 (((S)->__modeflags & (__MASK_READING|__FLAG_WIDE)) > __FLAG_WIDE)
193 #define __STDIO_STREAM_IS_NARROW(S) ((S)->__modeflags & __FLAG_NARROW)
194 #define __STDIO_STREAM_IS_WIDE(S) ((S)->__modeflags & __FLAG_WIDE)
196 #define __STDIO_STREAM_SET_NARROW(S) \
197 ((void)((S)->__modeflags |= __FLAG_NARROW))
198 #define __STDIO_STREAM_SET_WIDE(S) \
199 ((void)((S)->__modeflags |= __FLAG_WIDE))
203 #define __STDIO_STREAM_IS_NARROW_WRITING(S) __STDIO_STREAM_IS_WRITING(S)
205 #define __STDIO_STREAM_IS_NARROW_READING(S) __STDIO_STREAM_IS_READING(S)
207 #define __STDIO_STREAM_IS_NARROW(S) (1)
208 #define __STDIO_STREAM_IS_WIDE(S) (0)
210 #define __STDIO_STREAM_SET_NARROW(S) ((void)0)
211 #define __STDIO_STREAM_SET_WIDE(S) ((void)0)
214 /**********************************************************************/
216 #define __STDIO_STREAM_SET_EOF(S) \
217 ((void)((S)->__modeflags |= __FLAG_EOF))
218 #define __STDIO_STREAM_SET_ERROR(S) \
219 ((void)((S)->__modeflags |= __FLAG_ERROR))
221 #define __STDIO_STREAM_CLEAR_EOF(S) \
222 ((void)((S)->__modeflags &= ~__FLAG_EOF))
223 #define __STDIO_STREAM_CLEAR_ERROR(S) \
224 ((void)((S)->__modeflags &= ~__FLAG_ERROR))
226 #define __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(S) \
227 ((void)((S)->__modeflags &= ~__MASK_READING))
228 #define __STDIO_STREAM_CLEAR_WRITING(S) \
229 ((void)((S)->__modeflags &= ~__FLAG_WRITING))
231 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
232 # define __STDIO_STREAM_DISABLE_GETC(S) \
233 ((void)((S)->__bufgetc_u = (S)->__bufstart))
234 # define __STDIO_STREAM_ENABLE_GETC(S) \
235 ((void)((S)->__bufgetc_u = (S)->__bufread))
236 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) \
237 ((S)->__bufpos < (S)->__bufgetc_u)
239 # define __STDIO_STREAM_DISABLE_GETC(S) ((void)0)
240 # define __STDIO_STREAM_ENABLE_GETC(S) ((void)0)
241 # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) (0)
244 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
245 # define __STDIO_STREAM_DISABLE_PUTC(S) \
246 ((void)((S)->__bufputc_u = (S)->__bufstart))
247 # define __STDIO_STREAM_ENABLE_PUTC(S) \
248 ((void)((S)->__bufputc_u = (S)->__bufend))
249 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) \
250 ((S)->__bufpos < (S)->__bufputc_u)
252 # define __STDIO_STREAM_DISABLE_PUTC(S) ((void)0)
253 # define __STDIO_STREAM_ENABLE_PUTC(S) ((void)0)
254 # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) (0)
257 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
258 #define __STDIO_STREAM_IS_CUSTOM(S) ((S)->__cookie != &((S)->__filedes))
260 #define __STDIO_STREAM_IS_CUSTOM(S) (0)
263 /**********************************************************************/
265 #ifdef __STDIO_BUFFERS
266 #define __STDIO_STREAM_FREE_BUFFER(S) \
267 do { if ((S)->__modeflags & __FLAG_FREEBUF) \
268 free((S)->__bufstart); } while (0)
270 #define __STDIO_STREAM_FREE_BUFFER(S) ((void)0)
273 #define __STDIO_STREAM_FREE_FILE(S) \
274 do { if ((S)->__modeflags & __FLAG_FREEFILE) \
275 free((S)); } while (0)
278 #ifdef __UCLIBC_HAS_LFS__
279 #define __STDIO_WHEN_LFS(E) E
281 #define __STDIO_WHEN_LFS(E) ((void)0)
284 /**********************************************************************/
285 /* The following return 0 on success. */
287 #ifdef __STDIO_BUFFERS
288 /* Assume stream in valid writing state. Do not reset writing flag
289 * or disble putc macro unless error. */
290 /* Should we assume that buffer is not empty to avoid a check? */
291 extern size_t __stdio_wcommit(FILE *__restrict stream) attribute_hidden;
293 /* Remember to fail if at EOF! */
294 extern size_t __stdio_rfill(FILE *__restrict stream) attribute_hidden;
296 extern size_t __stdio_fwrite(const unsigned char *__restrict buffer,
297 size_t bytes, FILE *__restrict stream) attribute_hidden;
300 #define __stdio_fwrite(B,N,S) __stdio_WRITE((S),(B),(N))
304 extern size_t __stdio_WRITE(FILE *stream, const unsigned char *buf,
305 size_t bufsize) attribute_hidden;
306 extern size_t __stdio_READ(FILE *stream, unsigned char *buf,
307 size_t bufsize) attribute_hidden;
309 extern int __stdio_trans2r(FILE *__restrict stream) attribute_hidden;
310 extern int __stdio_trans2w(FILE *__restrict stream) attribute_hidden;
312 extern int __stdio_trans2r_o(FILE *__restrict stream, int oflag) attribute_hidden;
313 extern int __stdio_trans2w_o(FILE *__restrict stream, int oflag) attribute_hidden;
315 extern uintmax_t _load_inttype(int desttype, register const void *src, int uflag) attribute_hidden;
316 extern void _store_inttype(void *dest, int desttype, uintmax_t val) attribute_hidden;
318 /**********************************************************************/
319 #ifdef __STDIO_BUFFERS
321 #define __STDIO_STREAM_IS_FBF(S) (!((S)->__modeflags & __MASK_BUFMODE))
322 #define __STDIO_STREAM_IS_LBF(S) ((S)->__modeflags & __FLAG_LBF)
323 #define __STDIO_STREAM_IS_NBF(S) ((S)->__modeflags & __FLAG_NBF)
325 #define __STDIO_STREAM_BUFFER_SIZE(S) ((S)->__bufend - (S)->__bufstart)
327 /* Valid when writing... */
328 #define __STDIO_STREAM_BUFFER_ADD(S,C) (*(S)->__bufpos++ = (C))
329 #define __STDIO_STREAM_BUFFER_UNADD(S) (--(S)->__bufpos)
330 #define __STDIO_STREAM_BUFFER_WAVAIL(S) ((S)->__bufend - (S)->__bufpos)
331 #define __STDIO_STREAM_BUFFER_WUSED(S) ((S)->__bufpos - (S)->__bufstart)
332 #define __STDIO_COMMIT_WRITE_BUFFER(S) __stdio_wcommit((S))
333 #ifdef __UCLIBC_HAS_WCHAR__
334 #define __STDIO_STREAM_IS_NARROW_FBF(S) \
335 (!((S)->__modeflags & (__MASK_BUFMODE|__FLAG_WIDE)))
337 #define __STDIO_STREAM_IS_NARROW_FBF(S) __STDIO_STREAM_IS_FBF((S))
340 /* Valid when reading... */
341 #define __STDIO_STREAM_BUFFER_RAVAIL(S) ((S)->__bufread - (S)->__bufpos)
342 #define __STDIO_STREAM_BUFFER_GET(S) (*(S)->__bufpos++)
343 #define __STDIO_FILL_READ_BUFFER(S) __stdio_rfill((S))
345 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) \
346 (S)->__bufread = (S)->__bufpos = (S)->__bufstart
349 #define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES (-2)
350 #define __STDIO_STREAM_FAKE_VSSCANF_FILEDES (-2)
351 #define __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES (-3)
352 #define __STDIO_STREAM_FAKE_VSWSCANF_FILEDES (-3)
354 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) \
355 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES)
356 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) \
357 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES)
358 #define __STDIO_STREAM_IS_FAKE_VSWPRINTF(S) \
359 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES)
360 #define __STDIO_STREAM_IS_FAKE_VSWSCANF(S) \
361 ((S)->__filedes == __STDIO_STREAM_FAKE_VSWSCANF_FILEDES)
363 #else /* __STDIO_BUFFERS */
365 #define __STDIO_STREAM_IS_FBF(S) (0)
366 #define __STDIO_STREAM_IS_LBF(S) (0)
367 #define __STDIO_STREAM_IS_NBF(S) (1)
369 #define __STDIO_STREAM_BUFFER_SIZE(S) (0)
370 #define __STDIO_STREAM_BUFFER_ADD(S,C) ((void)0)
371 #define __STDIO_STREAM_BUFFER_UNADD(S) ((void)0)
372 #define __STDIO_STREAM_BUFFER_WAVAIL(S) (0)
373 #define __STDIO_STREAM_BUFFER_WUSED(S) (0)
374 #define __STDIO_COMMIT_WRITE_BUFFER(S) (0)
375 #define __STDIO_STREAM_IS_NARROW_FBF(S) (0)
377 #define __STDIO_STREAM_BUFFER_RAVAIL(S) (0)
378 #define __STDIO_STREAM_BUFFER_GET(S) (EOF)
379 #define __STDIO_FILL_READ_BUFFER(S) (0)
380 #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) ((void)0)
382 #undef __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES
383 #undef __STDIO_STREAM_FAKE_VSSCANF_FILEDES
384 #undef __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES
386 #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) (0)
387 #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) (0)
388 #undef __STDIO_STREAM_IS_FAKE_VSWPRINTF
390 # ifdef __USE_OLD_VFPRINTF__
391 # define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB (-2)
392 # define __STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(S) \
393 ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB)
396 # ifndef __UCLIBC_HAS_WCHAR__
397 # define __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB (-2)
398 # define __STDIO_STREAM_IS_FAKE_VSSCANF_NB(S) \
399 ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB)
402 #endif /* __STDIO_BUFFERS */
403 /**********************************************************************/
405 extern int __stdio_adjust_position(FILE *__restrict stream, __offmax_t *pos) attribute_hidden;
407 #ifdef __STDIO_HAS_OPENLIST
408 /* Uses an implementation hack!!! */
409 #define __STDIO_FLUSH_LBF_STREAMS \
410 fflush_unlocked((FILE *) &_stdio_openlist)
412 #define __STDIO_FLUSH_LBF_STREAMS ((void)0)
416 #define __STDIO_STREAM_VALIDATE(S) ((void)0)
418 extern void _stdio_validate_FILE(const FILE *stream) attribute_hidden;
419 #define __STDIO_STREAM_VALIDATE(S) _stdio_validate_FILE((S))
422 #ifdef __STDIO_MBSTATE
423 #define __COPY_MBSTATE(dest,src) \
424 ((void)((dest)->__mask = (src)->__mask, (dest)->__wc = (src)->__wc))
425 #define __INIT_MBSTATE(dest) ((void)((dest)->__mask = 0))
427 #define __COPY_MBSTATE(dest,src) ((void)0)
428 #define __INIT_MBSTATE(dest) ((void)0)
431 /**********************************************************************/
433 extern FILE *_stdio_fopen(intptr_t fname_or_mode, const char *__restrict mode,
434 FILE *__restrict stream, int filedes) attribute_hidden;
436 #ifdef __UCLIBC_HAS_WCHAR__
437 extern size_t _wstdio_fwrite(const wchar_t *__restrict ws,
438 size_t n, FILE *__restrict stream) attribute_hidden;
441 /**********************************************************************/
443 extern int _vfprintf_internal (FILE * __restrict stream,
444 const char * __restrict format,
445 va_list arg) attribute_hidden;
447 #ifdef __UCLIBC_HAS_WCHAR__
448 extern int _vfwprintf_internal (FILE * __restrict stream,
449 const wchar_t * __restrict format,
450 va_list arg) attribute_hidden;
453 /**********************************************************************/
454 /* Only use the macro below if you know fp is a valid FILE for a valid fd.
455 * This is _not_ true for custom streams! */
456 #define __FILENO_UNLOCKED(fp) ((fp)->__filedes)
458 #define __FEOF_OR_FERROR_UNLOCKED(stream) \
459 ((stream)->__modeflags & (__FLAG_EOF|__FLAG_ERROR))
461 #if defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
462 #define __STDIO_HAS_VSNPRINTF 1