_stdio.h 14 KB

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