_stdio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Copyright (C) 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. #define _GNU_SOURCE
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <limits.h>
  12. #include <signal.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #ifdef __UCLIBC_HAS_WCHAR__
  19. #include <wchar.h>
  20. #endif
  21. #ifdef __UCLIBC_HAS_THREADS__
  22. #include <pthread.h>
  23. #define __STDIO_THREADLOCK_OPENLIST \
  24. __pthread_mutex_lock(&_stdio_openlist_lock)
  25. #define __STDIO_THREADUNLOCK_OPENLIST \
  26. __pthread_mutex_unlock(&_stdio_openlist_lock)
  27. #define __STDIO_THREADTRYLOCK_OPENLIST \
  28. __pthread_mutex_trylock(&_stdio_openlist_lock)
  29. #else
  30. #define __STDIO_THREADLOCK_OPENLIST ((void)0)
  31. #define __STDIO_THREADUNLOCK_OPENLIST ((void)0)
  32. #endif
  33. #define __UNDEFINED_OR_NONPORTABLE ((void)0)
  34. /**********************************************************************/
  35. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  36. extern __ssize_t _cs_read(void *cookie, char *buf, size_t bufsize);
  37. extern __ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize);
  38. extern int _cs_seek(void *cookie, __offmax_t *pos, int whence);
  39. extern int _cs_close(void *cookie);
  40. #define __STDIO_STREAM_RESET_GCS(S) \
  41. (S)->__cookie = &((S)->__filedes); \
  42. (S)->__gcs.read = _cs_read; \
  43. (S)->__gcs.write = _cs_write; \
  44. (S)->__gcs.seek = _cs_seek; \
  45. (S)->__gcs.close = _cs_close
  46. #define __READ(STREAMPTR,BUF,SIZE) \
  47. ((((STREAMPTR)->__gcs.read) == NULL) ? -1 : \
  48. (((STREAMPTR)->__gcs.read)((STREAMPTR)->__cookie,(BUF),(SIZE))))
  49. #define __WRITE(STREAMPTR,BUF,SIZE) \
  50. ((((STREAMPTR)->__gcs.write) == NULL) ? -1 : \
  51. (((STREAMPTR)->__gcs.write)((STREAMPTR)->__cookie,(BUF),(SIZE))))
  52. #define __SEEK(STREAMPTR,PPOS,WHENCE) \
  53. ((((STREAMPTR)->__gcs.seek) == NULL) ? -1 : \
  54. (((STREAMPTR)->__gcs.seek)((STREAMPTR)->__cookie,(PPOS),(WHENCE))))
  55. #define __CLOSE(STREAMPTR) \
  56. ((((STREAMPTR)->__gcs.close) == NULL) ? 0 : \
  57. (((STREAMPTR)->__gcs.close)((STREAMPTR)->__cookie)))
  58. #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
  59. #define __STDIO_STREAM_RESET_GCS(S) ((void)0)
  60. #define __READ(STREAMPTR,BUF,SIZE) \
  61. (read((STREAMPTR)->__filedes,(BUF),(SIZE)))
  62. #define __WRITE(STREAMPTR,BUF,SIZE) \
  63. (write((STREAMPTR)->__filedes,(BUF),(SIZE)))
  64. #define __SEEK(STREAMPTR,PPOS,WHENCE) \
  65. (__stdio_seek((STREAMPTR),(PPOS),(WHENCE)))
  66. #define __CLOSE(STREAMPTR) \
  67. (close((STREAMPTR)->__filedes))
  68. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
  69. /**********************************************************************/
  70. #ifdef __UCLIBC_HAS_WCHAR__
  71. #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w_o((S), (O))
  72. #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r_o((S), (O))
  73. #else
  74. #define __STDIO_STREAM_TRANS_TO_WRITE(S,O) __stdio_trans2w((S))
  75. #define __STDIO_STREAM_TRANS_TO_READ(S,O) __stdio_trans2r((S))
  76. #endif
  77. /**********************************************************************/
  78. #define __STDIO_STREAM_IS_READING(S) ((S)->__modeflags & __MASK_READING)
  79. #define __STDIO_STREAM_IS_WRITING(S) ((S)->__modeflags & __FLAG_WRITING)
  80. #define __STDIO_STREAM_SET_READING(S) ((S)->__modeflags |= __FLAG_READING)
  81. #define __STDIO_STREAM_SET_WRITING(S) ((S)->__modeflags |= __FLAG_WRITING)
  82. #define __STDIO_STREAM_IS_READING_OR_READONLY(S) \
  83. ((S)->__modeflags & (__MASK_READING|__FLAG_READONLY))
  84. #define __STDIO_STREAM_IS_WRITING_OR_WRITEONLY(S) \
  85. ((S)->__modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY))
  86. #define __STDIO_STREAM_IS_READONLY(S) ((S)->__modeflags & __FLAG_READONLY)
  87. #define __STDIO_STREAM_IS_WRITEONLY(S) ((S)->__modeflags & __FLAG_WRITEONLY)
  88. /**********************************************************************/
  89. #ifdef __UCLIBC_HAS_WCHAR__
  90. #define __STDIO_STREAM_IS_NARROW_WRITING(S) \
  91. (((S)->__modeflags & (__FLAG_WRITING|__FLAG_NARROW)) \
  92. == (__FLAG_WRITING|__FLAG_NARROW))
  93. #define __STDIO_STREAM_IS_WIDE_WRITING(S) \
  94. (((S)->__modeflags & (__FLAG_WRITING|__FLAG_WIDE)) \
  95. == (__FLAG_WRITING|__FLAG_WIDE))
  96. #if (__FLAG_NARROW <= __MASK_READING)
  97. #error assumption violated regarding __FLAG_NARROW
  98. #endif
  99. #define __STDIO_STREAM_IS_NARROW_READING(S) \
  100. (((S)->__modeflags & (__MASK_READING|__FLAG_NARROW)) > __FLAG_NARROW)
  101. #define __STDIO_STREAM_IS_WIDE_READING(S) \
  102. (((S)->__modeflags & (__MASK_READING|__FLAG_WIDE)) > __FLAG_WIDE)
  103. #define __STDIO_STREAM_IS_NARROW(S) ((S)->__modeflags & __FLAG_NARROW)
  104. #define __STDIO_STREAM_IS_WIDE(S) ((S)->__modeflags & __FLAG_WIDE)
  105. #define __STDIO_STREAM_SET_NARROW(S) \
  106. ((void)((S)->__modeflags |= __FLAG_NARROW))
  107. #define __STDIO_STREAM_SET_WIDE(S) \
  108. ((void)((S)->__modeflags |= __FLAG_WIDE))
  109. #else
  110. #define __STDIO_STREAM_IS_NARROW_WRITING(S) __STDIO_STREAM_IS_WRITING(S)
  111. #define __STDIO_STREAM_IS_NARROW_READING(S) __STDIO_STREAM_IS_READING(S)
  112. #define __STDIO_STREAM_IS_NARROW(S) (1)
  113. #define __STDIO_STREAM_IS_WIDE(S) (0)
  114. #define __STDIO_STREAM_SET_NARROW(S) ((void)0)
  115. #define __STDIO_STREAM_SET_WIDE(S) ((void)0)
  116. #endif
  117. /**********************************************************************/
  118. #define __STDIO_STREAM_SET_EOF(S) \
  119. ((void)((S)->__modeflags |= __FLAG_EOF))
  120. #define __STDIO_STREAM_SET_ERROR(S) \
  121. ((void)((S)->__modeflags |= __FLAG_ERROR))
  122. #define __STDIO_STREAM_CLEAR_EOF(S) \
  123. ((void)((S)->__modeflags &= ~__FLAG_EOF))
  124. #define __STDIO_STREAM_CLEAR_ERROR(S) \
  125. ((void)((S)->__modeflags &= ~__FLAG_ERROR))
  126. #define __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(S) \
  127. ((void)((S)->__modeflags &= ~__MASK_READING))
  128. #define __STDIO_STREAM_CLEAR_WRITING(S) \
  129. ((void)((S)->__modeflags &= ~__FLAG_WRITING))
  130. #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  131. # define __STDIO_STREAM_DISABLE_GETC(S) \
  132. ((void)((S)->__bufgetc_u = (S)->__bufstart))
  133. # define __STDIO_STREAM_ENABLE_GETC(S) \
  134. ((void)((S)->__bufgetc_u = (S)->__bufread))
  135. # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) \
  136. ((S)->__bufpos < (S)->__bufgetc_u)
  137. #else
  138. # define __STDIO_STREAM_DISABLE_GETC(S) ((void)0)
  139. # define __STDIO_STREAM_ENABLE_GETC(S) ((void)0)
  140. # define __STDIO_STREAM_CAN_USE_BUFFER_GET(S) (0)
  141. #endif
  142. #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  143. # define __STDIO_STREAM_DISABLE_PUTC(S) \
  144. ((void)((S)->__bufputc_u = (S)->__bufstart))
  145. # define __STDIO_STREAM_ENABLE_PUTC(S) \
  146. ((void)((S)->__bufputc_u = (S)->__bufend))
  147. # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) \
  148. ((S)->__bufpos < (S)->__bufputc_u)
  149. #else
  150. # define __STDIO_STREAM_DISABLE_PUTC(S) ((void)0)
  151. # define __STDIO_STREAM_ENABLE_PUTC(S) ((void)0)
  152. # define __STDIO_STREAM_CAN_USE_BUFFER_ADD(S) (0)
  153. #endif
  154. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  155. #define __STDIO_STREAM_IS_CUSTOM(S) ((S)->__cookie != &((S)->__filedes))
  156. #else
  157. #define __STDIO_STREAM_IS_CUSTOM(S) (0)
  158. #endif
  159. /**********************************************************************/
  160. #ifdef __STDIO_BUFFERS
  161. #define __STDIO_STREAM_FREE_BUFFER(S) \
  162. do { if ((S)->__modeflags & __FLAG_FREEBUF) free((S)->__bufstart); } while (0)
  163. #else
  164. #define __STDIO_STREAM_FREE_BUFFER(S) ((void)0)
  165. #endif
  166. #define __STDIO_STREAM_FREE_FILE(S) \
  167. do { if ((S)->__modeflags & __FLAG_FREEFILE) free((S)); } while (0)
  168. #ifdef __UCLIBC_HAS_LFS__
  169. #define __STDIO_WHEN_LFS(E) E
  170. #else
  171. #define __STDIO_WHEN_LFS(E) ((void)0)
  172. #endif
  173. /**********************************************************************/
  174. /* The following return 0 on success. */
  175. #ifdef __STDIO_BUFFERS
  176. /* Assume stream in valid writing state. Do not reset writing flag
  177. * or disble putc macro unless error. */
  178. /* Should we assume that buffer is not empty to avoid a check? */
  179. extern size_t __stdio_wcommit(FILE *__restrict stream);
  180. /* Remember to fail if at EOF! */
  181. extern size_t __stdio_rfill(FILE *__restrict stream);
  182. extern size_t __stdio_fwrite(const unsigned char *__restrict buffer,
  183. size_t bytes, FILE *__restrict stream);
  184. #else
  185. #define __stdio_fwrite(B,N,S) __stdio_WRITE((S),(B),(N))
  186. #endif
  187. extern size_t __stdio_WRITE(FILE *stream, const unsigned char *buf,
  188. size_t bufsize);
  189. extern size_t __stdio_READ(FILE *stream, unsigned char *buf, size_t bufsize);
  190. extern int __stdio_trans2r(FILE *__restrict stream);
  191. extern int __stdio_trans2w(FILE *__restrict stream);
  192. extern int __stdio_trans2r_o(FILE *__restrict stream, int oflag);
  193. extern int __stdio_trans2w_o(FILE *__restrict stream, int oflag);
  194. /**********************************************************************/
  195. #ifdef __STDIO_BUFFERS
  196. #define __STDIO_STREAM_IS_FBF(S) (!((S)->__modeflags & __MASK_BUFMODE))
  197. #define __STDIO_STREAM_IS_LBF(S) ((S)->__modeflags & __FLAG_LBF)
  198. #define __STDIO_STREAM_IS_NBF(S) ((S)->__modeflags & __FLAG_NBF)
  199. #define __STDIO_STREAM_BUFFER_SIZE(S) ((S)->__bufend - (S)->__bufstart)
  200. /* Valid when writing... */
  201. #define __STDIO_STREAM_BUFFER_ADD(S,C) (*(S)->__bufpos++ = (C))
  202. #define __STDIO_STREAM_BUFFER_UNADD(S) (--(S)->__bufpos)
  203. #define __STDIO_STREAM_BUFFER_WAVAIL(S) ((S)->__bufend - (S)->__bufpos)
  204. #define __STDIO_STREAM_BUFFER_WUSED(S) ((S)->__bufpos - (S)->__bufstart)
  205. #define __STDIO_COMMIT_WRITE_BUFFER(S) __stdio_wcommit((S))
  206. #ifdef __UCLIBC_HAS_WCHAR__
  207. #define __STDIO_STREAM_IS_NARROW_FBF(S) \
  208. (!((S)->__modeflags & (__MASK_BUFMODE|__FLAG_WIDE)))
  209. #else
  210. #define __STDIO_STREAM_IS_NARROW_FBF(S) __STDIO_STREAM_IS_FBF((S))
  211. #endif
  212. /* Valid when reading... */
  213. #define __STDIO_STREAM_BUFFER_RAVAIL(S) ((S)->__bufread - (S)->__bufpos)
  214. #define __STDIO_STREAM_BUFFER_GET(S) (*(S)->__bufpos++)
  215. #define __STDIO_FILL_READ_BUFFER(S) __stdio_rfill((S))
  216. #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) \
  217. (S)->__bufread = (S)->__bufpos = (S)->__bufstart
  218. #define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES (-2)
  219. #define __STDIO_STREAM_FAKE_VSSCANF_FILEDES (-2)
  220. #define __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES (-3)
  221. #define __STDIO_STREAM_FAKE_VSWSCANF_FILEDES (-3)
  222. #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) \
  223. ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES)
  224. #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) \
  225. ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES)
  226. #define __STDIO_STREAM_IS_FAKE_VSWPRINTF(S) \
  227. ((S)->__filedes == __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES)
  228. #define __STDIO_STREAM_IS_FAKE_VSWSCANF(S) \
  229. ((S)->__filedes == __STDIO_STREAM_FAKE_VSWSCANF_FILEDES)
  230. #else /* __STDIO_BUFFERS */
  231. #define __STDIO_STREAM_IS_FBF(S) (0)
  232. #define __STDIO_STREAM_IS_LBF(S) (0)
  233. #define __STDIO_STREAM_IS_NBF(S) (1)
  234. #define __STDIO_STREAM_BUFFER_SIZE(S) (0)
  235. #define __STDIO_STREAM_BUFFER_ADD(S,C) ((void)0)
  236. #define __STDIO_STREAM_BUFFER_UNADD(S) ((void)0)
  237. #define __STDIO_STREAM_BUFFER_WAVAIL(S) (0)
  238. #define __STDIO_STREAM_BUFFER_WUSED(S) (0)
  239. #define __STDIO_COMMIT_WRITE_BUFFER(S) (0)
  240. #define __STDIO_STREAM_IS_NARROW_FBF(S) (0)
  241. #define __STDIO_STREAM_BUFFER_RAVAIL(S) (0)
  242. #define __STDIO_STREAM_BUFFER_GET(S) (EOF)
  243. #define __STDIO_FILL_READ_BUFFER(S) (0)
  244. #define __STDIO_STREAM_INIT_BUFREAD_BUFPOS(S) ((void)0)
  245. #undef __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES
  246. #undef __STDIO_STREAM_FAKE_VSSCANF_FILEDES
  247. #undef __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES
  248. #define __STDIO_STREAM_IS_FAKE_VSNPRINTF(S) (0)
  249. #define __STDIO_STREAM_IS_FAKE_VSSCANF(S) (0)
  250. #undef __STDIO_STREAM_IS_FAKE_VSWPRINTF
  251. # ifdef __USE_OLD_VFPRINTF__
  252. # define __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB (-2)
  253. # define __STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(S) \
  254. ((S)->__filedes == __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB)
  255. # endif
  256. # ifndef __UCLIBC_HAS_WCHAR__
  257. # define __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB (-2)
  258. # define __STDIO_STREAM_IS_FAKE_VSSCANF_NB(S) \
  259. ((S)->__filedes == __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB)
  260. # endif
  261. #endif /* __STDIO_BUFFERS */
  262. /**********************************************************************/
  263. extern int __fputs_unlocked(const char *__restrict s, FILE *__restrict stream);
  264. extern int __putchar_unlocked(int c);
  265. extern size_t __fwrite_unlocked(const void *__restrict ptr, size_t size,
  266. size_t nmemb, FILE *__restrict stream);
  267. extern size_t __fread_unlocked(void *__restrict ptr, size_t size,
  268. size_t nmemb, FILE *__restrict stream);
  269. extern int __fputc_unlocked(int c, FILE *stream);
  270. extern int __fflush_unlocked(FILE *stream);
  271. extern int __stdio_adjust_position(FILE *__restrict stream, __offmax_t *pos);
  272. extern void __clearerr_unlocked(FILE *stream);
  273. extern int __feof_unlocked(FILE *stream);
  274. extern int __ferror_unlocked(FILE *stream);
  275. extern int __fgetc_unlocked(FILE *stream);
  276. extern char *__fgets_unlocked(char *__restrict s, int n,
  277. FILE * __restrict stream);
  278. extern int __fileno_unlocked(FILE *stream);
  279. extern int __getchar_unlocked(void);
  280. #ifdef __UCLIBC_HAS_LFS__
  281. extern int __fseeko64(FILE *stream, __off64_t offset, int whence);
  282. extern __off64_t __ftello64(FILE *stream);
  283. #endif
  284. #ifdef __STDIO_HAS_OPENLIST
  285. /* Uses an implementation hack!!! */
  286. #define __STDIO_FLUSH_LBF_STREAMS __fflush_unlocked((FILE *) &_stdio_openlist)
  287. #else
  288. #define __STDIO_FLUSH_LBF_STREAMS ((void)0)
  289. #endif
  290. #ifdef NDEBUG
  291. #define __STDIO_STREAM_VALIDATE(S) ((void)0)
  292. #else
  293. extern void _stdio_validate_FILE(const FILE *stream);
  294. #define __STDIO_STREAM_VALIDATE(S) _stdio_validate_FILE((S))
  295. #endif
  296. #ifdef __STDIO_MBSTATE
  297. #define __COPY_MBSTATE(dest,src) \
  298. ((void)((dest)->__mask = (src)->__mask, (dest)->__wc = (src)->__wc))
  299. #define __INIT_MBSTATE(dest) ((void)((dest)->__mask = 0))
  300. #else
  301. #define __COPY_MBSTATE(dest,src) ((void)0)
  302. #define __INIT_MBSTATE(dest) ((void)0)
  303. #endif
  304. /**********************************************************************/
  305. extern int _stdio_adjpos(FILE *__restrict stream, __offmax_t * pos);
  306. extern int _stdio_lseek(FILE *stream, __offmax_t *pos, int whence);
  307. extern size_t _stdio_fwrite(const unsigned char *buffer, size_t bytes,
  308. FILE *stream);
  309. extern size_t _stdio_fread(unsigned char *buffer, size_t bytes,
  310. FILE *stream);
  311. extern FILE *_stdio_fopen(intptr_t fname_or_mode,
  312. const char *__restrict mode,
  313. FILE *__restrict stream, int filedes);
  314. #ifdef __UCLIBC_HAS_WCHAR__
  315. extern size_t _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
  316. FILE *__restrict stream);
  317. extern wint_t __fgetwc_unlocked(register FILE *stream);
  318. extern wint_t __fputwc_unlocked(wchar_t wc, FILE *stream);
  319. #endif
  320. /**********************************************************************/
  321. /* Only use the macro below if you know fp is a valid FILE for a valid fd.
  322. * This is _not_ true for custom streams! */
  323. #define __FILENO_UNLOCKED(fp) ((fp)->__filedes)
  324. #define __FEOF_OR_FERROR_UNLOCKED(stream) \
  325. ((stream)->__modeflags & (__FLAG_EOF|__FLAG_ERROR))
  326. #if defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  327. #define __STDIO_HAS_VSNPRINTF 1
  328. #endif