uClibc_stdio.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. #ifndef _STDIO_H
  8. #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
  9. #endif
  10. /**********************************************************************/
  11. #define __STDIO_BUFFERS
  12. /* ANSI/ISO mandate at least 256. */
  13. #if defined(__UCLIBC_HAS_STDIO_BUFSIZ_NONE__)
  14. /* Fake this because some apps use stdio.h BUFSIZ. */
  15. #define __STDIO_BUFSIZ 256
  16. #undef __STDIO_BUFFERS
  17. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_256__)
  18. #define __STDIO_BUFSIZ 256
  19. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_512__)
  20. #define __STDIO_BUFSIZ 512
  21. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_1024__)
  22. #define __STDIO_BUFSIZ 1024
  23. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_2048__)
  24. #define __STDIO_BUFSIZ 2048
  25. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_4096__)
  26. #define __STDIO_BUFSIZ 4096
  27. #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_8192__)
  28. #define __STDIO_BUFSIZ 8192
  29. #else
  30. #error config seems to be out of sync regarding bufsiz options
  31. #endif
  32. #ifdef __UCLIBC_HAS_STDIO_BUFSIZ_NONE__
  33. #define __STDIO_BUILTIN_BUF_SIZE 0
  34. #else /* __UCLIBC_HAS_STDIO_BUFSIZ_NONE__ */
  35. #if defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_NONE__)
  36. #define __STDIO_BUILTIN_BUF_SIZE 0
  37. #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_4__)
  38. #define __STDIO_BUILTIN_BUF_SIZE 4
  39. #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_8__)
  40. #define __STDIO_BUILTIN_BUF_SIZE 8
  41. #else
  42. #error config seems to be out of sync regarding builtin buffer size
  43. #endif
  44. #endif
  45. #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) || defined(__UCLIBC_HAS_THREADS__)
  46. #define __STDIO_HAS_OPENLIST 1
  47. #else
  48. #undef __STDIO_HAS_OPENLIST
  49. #endif
  50. /**********************************************************************/
  51. #ifdef __UCLIBC_HAS_WCHAR__
  52. #define __need_wchar_t
  53. #include <stddef.h>
  54. /* Note: we don't really need mbstate for 8-bit locales. We do for UTF-8.
  55. * For now, always use it. */
  56. #define __STDIO_MBSTATE
  57. #define __need_mbstate_t
  58. #include <wchar.h>
  59. #endif
  60. /**********************************************************************/
  61. /* Currently unimplemented/untested */
  62. /* #define __STDIO_FLEXIBLE_SETVBUF */
  63. #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  64. #define __STDIO_GETC_MACRO
  65. #endif
  66. #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  67. #define __STDIO_PUTC_MACRO
  68. #endif
  69. #ifdef _LIBC
  70. /**********************************************************************/
  71. #include <bits/uClibc_mutex.h>
  72. /* user_locking
  73. * 0 : do auto locking/unlocking
  74. * 1 : user does locking/unlocking
  75. * 2 : initial state prior to thread initialization
  76. * with no auto locking/unlocking
  77. *
  78. * When threading is initialized, walk the stdio open stream list
  79. * and do "if (user_locking == 2) user_locking = 0;".
  80. *
  81. * This way, we avoid calling the weak lock/unlock functions.
  82. */
  83. #define __STDIO_AUTO_THREADLOCK_VAR \
  84. __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
  85. #define __STDIO_AUTO_THREADLOCK(__stream) \
  86. __UCLIBC_IO_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
  87. (__stream)->__user_locking)
  88. #define __STDIO_AUTO_THREADUNLOCK(__stream) \
  89. __UCLIBC_IO_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
  90. #define __STDIO_ALWAYS_THREADLOCK(__stream) \
  91. __UCLIBC_IO_MUTEX_LOCK((__stream)->__lock)
  92. #define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
  93. __UCLIBC_IO_MUTEX_UNLOCK((__stream)->__lock)
  94. #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
  95. __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
  96. #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
  97. __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
  98. #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
  99. __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
  100. #ifdef __UCLIBC_HAS_THREADS__
  101. #define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
  102. #else
  103. #define __STDIO_SET_USER_LOCKING(__stream) ((void)0)
  104. #endif
  105. #ifdef __UCLIBC_HAS_THREADS__
  106. #ifdef __USE_STDIO_FUTEXES__
  107. #define STDIO_INIT_MUTEX(M) _IO_lock_init(M)
  108. #else
  109. #define STDIO_INIT_MUTEX(M) __stdio_init_mutex(& M)
  110. #endif
  111. #endif
  112. #endif /* _LIBC */
  113. /**********************************************************************/
  114. #define __STDIO_IOFBF 0 /* Fully buffered. */
  115. #define __STDIO_IOLBF 1 /* Line buffered. */
  116. #define __STDIO_IONBF 2 /* No buffering. */
  117. typedef struct {
  118. __off_t __pos;
  119. #ifdef __STDIO_MBSTATE
  120. __mbstate_t __mbstate;
  121. #endif
  122. #ifdef __UCLIBC_HAS_WCHAR__
  123. int __mblen_pending;
  124. #endif
  125. } __STDIO_fpos_t;
  126. #ifdef __UCLIBC_HAS_LFS__
  127. typedef struct {
  128. __off64_t __pos;
  129. #ifdef __STDIO_MBSTATE
  130. __mbstate_t __mbstate;
  131. #endif
  132. #ifdef __UCLIBC_HAS_WCHAR__
  133. int __mblen_pending;
  134. #endif
  135. } __STDIO_fpos64_t;
  136. #endif
  137. /**********************************************************************/
  138. #ifdef __UCLIBC_HAS_LFS__
  139. typedef __off64_t __offmax_t; /* TODO -- rename this? */
  140. #else
  141. typedef __off_t __offmax_t; /* TODO -- rename this? */
  142. #endif
  143. /**********************************************************************/
  144. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  145. typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
  146. typedef __ssize_t __io_write_fn(void *__cookie,
  147. const char *__buf, size_t __bufsize);
  148. /* NOTE: GLIBC difference!!! -- fopencookie seek function
  149. * For glibc, the type of pos is always (__off64_t *) but in our case
  150. * it is type (__off_t *) when the lib is built without large file support.
  151. */
  152. typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
  153. typedef int __io_close_fn(void *__cookie);
  154. typedef struct {
  155. __io_read_fn *read;
  156. __io_write_fn *write;
  157. __io_seek_fn *seek;
  158. __io_close_fn *close;
  159. } _IO_cookie_io_functions_t;
  160. #ifdef __USE_GNU
  161. typedef __io_read_fn cookie_read_function_t;
  162. typedef __io_write_fn cookie_write_function_t;
  163. typedef __io_seek_fn cookie_seek_function_t;
  164. typedef __io_close_fn cookie_close_function_t;
  165. typedef _IO_cookie_io_functions_t cookie_io_functions_t;
  166. #endif
  167. #endif
  168. /**********************************************************************/
  169. #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_IO_MUTEX
  170. /* keep this in sync with uClibc_mutex.h */
  171. # ifdef __USE_STDIO_FUTEXES__
  172. # include <bits/stdio-lock.h>
  173. # define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
  174. # else
  175. # include <bits/pthreadtypes.h>
  176. # define __UCLIBC_IO_MUTEX(M) pthread_mutex_t M
  177. # endif /* __UCLIBC_HAS_THREADS_NATIVE__ */
  178. #endif
  179. struct __STDIO_FILE_STRUCT {
  180. unsigned short __modeflags;
  181. /* There could be a hole here, but modeflags is used most.*/
  182. #ifdef __UCLIBC_HAS_WCHAR__
  183. unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
  184. /* Move the following futher down to avoid problems with getc/putc
  185. * macros breaking shared apps when wchar config support is changed. */
  186. /* wchar_t ungot[2]; */
  187. #else /* __UCLIBC_HAS_WCHAR__ */
  188. unsigned char __ungot[2];
  189. #endif /* __UCLIBC_HAS_WCHAR__ */
  190. int __filedes;
  191. #ifdef __STDIO_BUFFERS
  192. unsigned char *__bufstart; /* pointer to buffer */
  193. unsigned char *__bufend; /* pointer to 1 past end of buffer */
  194. unsigned char *__bufpos;
  195. unsigned char *__bufread; /* pointer to 1 past last buffered read char */
  196. #ifdef __STDIO_GETC_MACRO
  197. unsigned char *__bufgetc_u; /* 1 past last readable by getc_unlocked */
  198. #endif /* __STDIO_GETC_MACRO */
  199. #ifdef __STDIO_PUTC_MACRO
  200. unsigned char *__bufputc_u; /* 1 past last writeable by putc_unlocked */
  201. #endif /* __STDIO_PUTC_MACRO */
  202. #endif /* __STDIO_BUFFERS */
  203. #ifdef __STDIO_HAS_OPENLIST
  204. struct __STDIO_FILE_STRUCT *__nextopen;
  205. #endif
  206. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  207. void *__cookie;
  208. _IO_cookie_io_functions_t __gcs;
  209. #endif
  210. #ifdef __UCLIBC_HAS_WCHAR__
  211. wchar_t __ungot[2];
  212. #endif
  213. #ifdef __STDIO_MBSTATE
  214. __mbstate_t __state;
  215. #endif
  216. #ifdef __UCLIBC_HAS_XLOCALE__
  217. void *__unused; /* Placeholder for codeset binding. */
  218. #endif
  219. #ifdef __UCLIBC_HAS_THREADS__
  220. int __user_locking;
  221. __UCLIBC_IO_MUTEX(__lock);
  222. #endif
  223. /* Everything after this is unimplemented... and may be trashed. */
  224. #if __STDIO_BUILTIN_BUF_SIZE > 0
  225. unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
  226. #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  227. };
  228. /***********************************************************************/
  229. /* Having ungotten characters implies the stream is reading.
  230. * The scheme used here treats the least significant 2 bits of
  231. * the stream's modeflags member as follows:
  232. * 0 0 Not currently reading.
  233. * 0 1 Reading, but no ungetc() or scanf() push back chars.
  234. * 1 0 Reading with one ungetc() char (ungot[1] is 1)
  235. * or one scanf() pushed back char (ungot[1] is 0).
  236. * 1 1 Reading with both an ungetc() char and a scanf()
  237. * pushed back char. Note that this must be the result
  238. * of a scanf() push back (in ungot[0]) _followed_ by
  239. * an ungetc() call (in ungot[1]).
  240. *
  241. * Notes:
  242. * scanf() can NOT use ungetc() to push back characters.
  243. * (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
  244. */
  245. #define __MASK_READING 0x0003U /* (0x0001 | 0x0002) */
  246. #define __FLAG_READING 0x0001U
  247. #define __FLAG_UNGOT 0x0002U
  248. #define __FLAG_EOF 0x0004U
  249. #define __FLAG_ERROR 0x0008U
  250. #define __FLAG_WRITEONLY 0x0010U
  251. #define __FLAG_READONLY 0x0020U /* (__FLAG_WRITEONLY << 1) */
  252. #define __FLAG_WRITING 0x0040U
  253. #define __FLAG_NARROW 0x0080U
  254. #define __FLAG_FBF 0x0000U /* must be 0 */
  255. #define __FLAG_LBF 0x0100U
  256. #define __FLAG_NBF 0x0200U /* (__FLAG_LBF << 1) */
  257. #define __MASK_BUFMODE 0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
  258. #define __FLAG_APPEND 0x0400U /* fixed! == O_APPEND for linux */
  259. #define __FLAG_WIDE 0x0800U
  260. /* available slot 0x1000U */
  261. #define __FLAG_FREEFILE 0x2000U
  262. #define __FLAG_FREEBUF 0x4000U
  263. #define __FLAG_LARGEFILE 0x8000U /* fixed! == 0_LARGEFILE for linux */
  264. #define __FLAG_FAILED_FREOPEN __FLAG_LARGEFILE
  265. /* Note: In no-buffer mode, it would be possible to pack the necessary
  266. * flags into one byte. Since we wouldn't be buffering and there would
  267. * be no support for wchar, the only flags we would need would be:
  268. * 2 bits : ungot count
  269. * 2 bits : eof + error
  270. * 2 bits : readonly + writeonly
  271. * 1 bit : freefile
  272. * 1 bit : appending
  273. * So, for a very small system (< 128 files) we might have a
  274. * 4-byte FILE struct of:
  275. * unsigned char flags;
  276. * signed char filedes;
  277. * unsigned char ungot[2];
  278. */
  279. /**********************************************************************
  280. * PROTOTYPES OF INTERNAL FUNCTIONS
  281. **********************************************************************/
  282. #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
  283. extern void weak_function _stdio_init(void) attribute_hidden;
  284. extern void weak_function _stdio_term(void) attribute_hidden;
  285. #ifdef __STDIO_HAS_OPENLIST
  286. extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
  287. #ifdef __UCLIBC_HAS_THREADS__
  288. __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock)
  289. # ifndef __UCLIBC_HAS_THREADS_NATIVE__
  290. attribute_hidden
  291. # endif
  292. ;
  293. #ifdef __STDIO_BUFFERS
  294. __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock)
  295. # ifndef __UCLIBC_HAS_THREADS_NATIVE__
  296. attribute_hidden
  297. # endif
  298. ;
  299. #endif
  300. extern int _stdio_user_locking;
  301. #endif
  302. #endif
  303. #endif
  304. /**********************************************************************/
  305. #define __CLEARERR_UNLOCKED(__stream) \
  306. ((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
  307. #define __FEOF_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_EOF)
  308. #define __FERROR_UNLOCKED(__stream) ((__stream)->__modeflags & __FLAG_ERROR)
  309. #ifdef __UCLIBC_HAS_THREADS__
  310. # define __CLEARERR(__stream) (clearerr)(__stream)
  311. # define __FERROR(__stream) (ferror)(__stream)
  312. # define __FEOF(__stream) (feof)(__stream)
  313. #else
  314. # define __CLEARERR(__stream) __CLEARERR_UNLOCKED(__stream)
  315. # define __FERROR(__stream) __FERROR_UNLOCKED(__stream)
  316. # define __FEOF(__stream) __FEOF_UNLOCKED(__stream)
  317. #endif
  318. extern int __fgetc_unlocked(FILE *__stream);
  319. libc_hidden_proto(__fgetc_unlocked)
  320. extern int __fputc_unlocked(int __c, FILE *__stream);
  321. libc_hidden_proto(__fputc_unlocked)
  322. /* First define the default definitions.
  323. They are overridden below as necessary. */
  324. #define __FGETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
  325. #define __FGETC(__stream) (fgetc)((__stream))
  326. #define __GETC_UNLOCKED_MACRO(__stream) (__fgetc_unlocked)((__stream))
  327. #define __GETC_UNLOCKED(__stream) (__fgetc_unlocked)((__stream))
  328. #define __GETC(__stream) (fgetc)((__stream))
  329. #define __FPUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
  330. #define __FPUTC(__c, __stream) (fputc)((__c),(__stream))
  331. #define __PUTC_UNLOCKED_MACRO(__c, __stream) (__fputc_unlocked)((__c),(__stream))
  332. #define __PUTC_UNLOCKED(__c, __stream) (__fputc_unlocked)((__c),(__stream))
  333. #define __PUTC(__c, __stream) (fputc)((__c),(__stream))
  334. #ifdef __STDIO_GETC_MACRO
  335. extern FILE *__stdin; /* For getchar() macro. */
  336. # undef __GETC_UNLOCKED_MACRO
  337. # define __GETC_UNLOCKED_MACRO(__stream) \
  338. ( ((__stream)->__bufpos < (__stream)->__bufgetc_u) \
  339. ? (*(__stream)->__bufpos++) \
  340. : __fgetc_unlocked(__stream) )
  341. # if 0
  342. /* Classic macro approach. getc{_unlocked} can have side effects. */
  343. # undef __GETC_UNLOCKED
  344. # define __GETC_UNLOCKED(__stream) __GETC_UNLOCKED_MACRO((__stream))
  345. # ifndef __UCLIBC_HAS_THREADS__
  346. # undef __GETC
  347. # define __GETC(__stream) __GETC_UNLOCKED_MACRO((__stream))
  348. # endif
  349. # else
  350. /* Using gcc extension for safety and additional inlining. */
  351. # undef __FGETC_UNLOCKED
  352. # define __FGETC_UNLOCKED(__stream) \
  353. (__extension__ ({ \
  354. FILE *__S = (__stream); \
  355. __GETC_UNLOCKED_MACRO(__S); \
  356. }) )
  357. # undef __GETC_UNLOCKED
  358. # define __GETC_UNLOCKED(__stream) __FGETC_UNLOCKED((__stream))
  359. # ifdef __UCLIBC_HAS_THREADS__
  360. # undef __FGETC
  361. # define __FGETC(__stream) \
  362. (__extension__ ({ \
  363. FILE *__S = (__stream); \
  364. ((__S->__user_locking ) \
  365. ? __GETC_UNLOCKED_MACRO(__S) \
  366. : (fgetc)(__S)); \
  367. }) )
  368. # undef __GETC
  369. # define __GETC(__stream) __FGETC((__stream))
  370. # else
  371. # undef __FGETC
  372. # define __FGETC(__stream) __FGETC_UNLOCKED((__stream))
  373. # undef __GETC
  374. # define __GETC(__stream) __FGETC_UNLOCKED((__stream))
  375. # endif
  376. # endif
  377. #else
  378. # define __stdin stdin
  379. #endif /* __STDIO_GETC_MACRO */
  380. #ifdef __STDIO_PUTC_MACRO
  381. extern FILE *__stdout; /* For putchar() macro. */
  382. # undef __PUTC_UNLOCKED_MACRO
  383. # define __PUTC_UNLOCKED_MACRO(__c, __stream) \
  384. ( ((__stream)->__bufpos < (__stream)->__bufputc_u) \
  385. ? (*(__stream)->__bufpos++) = (__c) \
  386. : __fputc_unlocked((__c),(__stream)) )
  387. # if 0
  388. /* Classic macro approach. putc{_unlocked} can have side effects.*/
  389. # undef __PUTC_UNLOCKED
  390. # define __PUTC_UNLOCKED(__c, __stream) \
  391. __PUTC_UNLOCKED_MACRO((__c), (__stream))
  392. # ifndef __UCLIBC_HAS_THREADS__
  393. # undef __PUTC
  394. # define __PUTC(__c, __stream) __PUTC_UNLOCKED_MACRO((__c), (__stream))
  395. # endif
  396. # else
  397. /* Using gcc extension for safety and additional inlining. */
  398. # undef __FPUTC_UNLOCKED
  399. # define __FPUTC_UNLOCKED(__c, __stream) \
  400. (__extension__ ({ \
  401. FILE *__S = (__stream); \
  402. __PUTC_UNLOCKED_MACRO((__c),__S); \
  403. }) )
  404. # undef __PUTC_UNLOCKED
  405. # define __PUTC_UNLOCKED(__c, __stream) __FPUTC_UNLOCKED((__c), (__stream))
  406. # ifdef __UCLIBC_HAS_THREADS__
  407. # undef __FPUTC
  408. # define __FPUTC(__c, __stream) \
  409. (__extension__ ({ \
  410. FILE *__S = (__stream); \
  411. ((__S->__user_locking) \
  412. ? __PUTC_UNLOCKED_MACRO((__c),__S) \
  413. : (fputc)((__c),__S)); \
  414. }) )
  415. # undef __PUTC
  416. # define __PUTC(__c, __stream) __FPUTC((__c), (__stream))
  417. # else
  418. # undef __FPUTC
  419. # define __FPUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
  420. # undef __PUTC
  421. # define __PUTC(__c, __stream) __FPUTC_UNLOCKED((__c),(__stream))
  422. # endif
  423. # endif
  424. #else
  425. # define __stdout stdout
  426. #endif /* __STDIO_PUTC_MACRO */