_stdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "_stdio.h"
  8. /* This is pretty much straight from uClibc, but with one important
  9. * difference.
  10. *
  11. * We now initialize the locking flag to user locking instead of
  12. * auto locking (i.e. FSETLOCKING_BYCALLER vs FSETLOCKING_INTERNAL).
  13. * This greatly benefits non-threading applications linked to a
  14. * shared thread-enabled library. In threading applications, we
  15. * walk the stdio open file list and reset the locking mode
  16. * appropriately when the thread subsystem is initialized.
  17. */
  18. /**********************************************************************/
  19. #ifdef __UCLIBC_HAS_WCHAR__
  20. #define __STDIO_FILE_INIT_WUNGOT { 0, 0 },
  21. #else
  22. #define __STDIO_FILE_INIT_WUNGOT
  23. #endif
  24. #ifdef __STDIO_GETC_MACRO
  25. # define __STDIO_FILE_INIT_BUFGETC(x) x,
  26. #else
  27. # define __STDIO_FILE_INIT_BUFGETC(x)
  28. #endif
  29. #ifdef __STDIO_PUTC_MACRO
  30. # define __STDIO_FILE_INIT_BUFPUTC(x) x,
  31. #else
  32. # define __STDIO_FILE_INIT_BUFPUTC(x)
  33. #endif
  34. #ifdef __STDIO_HAS_OPENLIST
  35. #define __STDIO_FILE_INIT_NEXT(next) (next),
  36. #else
  37. #define __STDIO_FILE_INIT_NEXT(next)
  38. #endif
  39. #ifdef __STDIO_BUFFERS
  40. #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
  41. (buf), (buf)+(bufsize), (buf), (buf),
  42. #else
  43. #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
  44. #endif
  45. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  46. #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
  47. &((stream).__filedes), { _cs_read, _cs_write, _cs_seek, _cs_close },
  48. #else
  49. #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
  50. #endif
  51. #ifdef __STDIO_MBSTATE
  52. #define __STDIO_FILE_INIT_MBSTATE \
  53. { 0, 0 },
  54. #else
  55. #define __STDIO_FILE_INIT_MBSTATE
  56. #endif
  57. #ifdef __UCLIBC_HAS_XLOCALE__
  58. #define __STDIO_FILE_INIT_UNUSED \
  59. NULL,
  60. #else
  61. #define __STDIO_FILE_INIT_UNUSED
  62. #endif
  63. #ifdef __UCLIBC_HAS_THREADS__
  64. #define __STDIO_FILE_INIT_THREADSAFE \
  65. 2, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
  66. #else
  67. #define __STDIO_FILE_INIT_THREADSAFE
  68. #endif
  69. #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
  70. { (flags), \
  71. { 0, 0 }, /* ungot[2] (no wchar) or ungot_width[2] (wchar)*/ \
  72. (filedes), \
  73. __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
  74. __STDIO_FILE_INIT_BUFGETC((buf)) \
  75. __STDIO_FILE_INIT_BUFPUTC((buf)) \
  76. __STDIO_FILE_INIT_NEXT(next) \
  77. __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
  78. __STDIO_FILE_INIT_WUNGOT \
  79. __STDIO_FILE_INIT_MBSTATE \
  80. __STDIO_FILE_INIT_UNUSED \
  81. __STDIO_FILE_INIT_THREADSAFE \
  82. } /* TODO: builtin buf */
  83. /**********************************************************************/
  84. /* First we need the standard files. */
  85. #ifdef __STDIO_BUFFERS
  86. static unsigned char _fixed_buffers[2 * BUFSIZ];
  87. #endif
  88. static FILE _stdio_streams[] = {
  89. __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], \
  90. __FLAG_LBF|__FLAG_READONLY, \
  91. 0, \
  92. _stdio_streams + 1, \
  93. _fixed_buffers, \
  94. BUFSIZ ),
  95. __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], \
  96. __FLAG_LBF|__FLAG_WRITEONLY, \
  97. 1, \
  98. _stdio_streams + 2, \
  99. _fixed_buffers + BUFSIZ, \
  100. BUFSIZ ),
  101. __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], \
  102. __FLAG_NBF|__FLAG_WRITEONLY, \
  103. 2, \
  104. NULL, \
  105. NULL, \
  106. 0 )
  107. };
  108. FILE *stdin = _stdio_streams;
  109. FILE *stdout = _stdio_streams + 1;
  110. FILE *stderr = _stdio_streams + 2;
  111. #ifdef __STDIO_GETC_MACRO
  112. FILE *__stdin = _stdio_streams; /* For getchar() macro. */
  113. #endif
  114. #ifdef __STDIO_PUTC_MACRO
  115. FILE *__stdout = _stdio_streams + 1; /* For putchar() macro. */
  116. /* FILE *__stderr = _stdio_streams + 2; */
  117. #endif
  118. /**********************************************************************/
  119. #ifdef __STDIO_HAS_OPENLIST
  120. /* In certain configurations, we need to keep a list of open files.
  121. * 1) buffering enabled - We need to initialize the buffering mode
  122. * (full or line buffering) of stdin and stdout. We also
  123. * need to flush all write buffers prior to normal termination.
  124. * 2) custom streams - Even if we aren't buffering in the library
  125. * itself, we need to fclose() all custom streams when terminating
  126. * so that any special cleanup is done.
  127. * 3) threads enabled - We need to be able to reset the locking mode
  128. * of all open streams when the threading system is initialized.
  129. */
  130. FILE *_stdio_openlist = _stdio_streams;
  131. # ifdef __UCLIBC_HAS_THREADS__
  132. pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  133. int _stdio_openlist_delflag = 0;
  134. # endif
  135. #endif
  136. /**********************************************************************/
  137. #ifdef __UCLIBC_HAS_THREADS__
  138. /* 2 if threading not initialized and 0 otherwise; */
  139. int _stdio_user_locking = 2;
  140. void __stdio_init_mutex(pthread_mutex_t *m)
  141. {
  142. static const pthread_mutex_t __stdio_mutex_initializer
  143. = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  144. memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
  145. }
  146. #endif
  147. /**********************************************************************/
  148. /* We assume here that we are the only remaining thread. */
  149. void _stdio_term(void)
  150. {
  151. #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  152. register FILE *ptr;
  153. #ifdef __UCLIBC_HAS_THREADS__
  154. /* First, make sure the open file list is unlocked. If it was
  155. * locked, then I suppose there is a chance that a pointer in the
  156. * chain might be corrupt due to a partial store.
  157. */
  158. __stdio_init_mutex(&_stdio_openlist_lock);
  159. /* Next we need to worry about the streams themselves. If a stream
  160. * is currently locked, then it may be in an invalid state. So we
  161. * 'disable' it in case a custom stream is stacked on top of it.
  162. * Then we reinitialize the locks.
  163. */
  164. for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
  165. if (__STDIO_ALWAYS_THREADTRYLOCK(ptr)) {
  166. /* The stream is already locked, so we don't want to touch it.
  167. * However, if we have custom streams, we can't just close it
  168. * or leave it locked since a custom stream may be stacked
  169. * on top of it. So we do unlock it, while also disabling it.
  170. */
  171. ptr->__modeflags = (__FLAG_READONLY|__FLAG_WRITEONLY);
  172. __STDIO_STREAM_DISABLE_GETC(ptr);
  173. __STDIO_STREAM_DISABLE_PUTC(ptr);
  174. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
  175. }
  176. ptr->__user_locking = 1; /* Set locking mode to "by caller". */
  177. __stdio_init_mutex(&ptr->__lock); /* Shouldn't be necessary, but... */
  178. }
  179. #endif
  180. /* Finally, flush all writing streams and shut down all custom streams.
  181. * NOTE: We assume that any stacking by custom streams is done on top
  182. * of streams previously allocated, and hence further down the
  183. * list. Otherwise we have no way of knowing the order in which
  184. * to shut them down.
  185. * Remember that freopen() counts as a new allocation here, even
  186. * though the stream is reused. That's because it moves the
  187. * stream to the head of the list.
  188. */
  189. for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
  190. #ifdef __STDIO_BUFFERS
  191. /* Write any pending buffered chars. */
  192. if (__STDIO_STREAM_IS_WRITING(ptr)) {
  193. __STDIO_COMMIT_WRITE_BUFFER(ptr);
  194. }
  195. #endif
  196. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  197. /* Actually close all custom streams to perform any special cleanup. */
  198. if (ptr->__cookie != &ptr->__filedes) {
  199. __CLOSE(ptr);
  200. }
  201. #endif
  202. }
  203. #endif
  204. }
  205. void _stdio_init(void)
  206. {
  207. #ifdef __STDIO_BUFFERS
  208. int old_errno = errno;
  209. /* stdin and stdout uses line buffering when connected to a tty. */
  210. _stdio_streams[0].__modeflags ^= (1-isatty(0)) * __FLAG_LBF;
  211. _stdio_streams[1].__modeflags ^= (1-isatty(1)) * __FLAG_LBF;
  212. __set_errno(old_errno);
  213. #endif
  214. #ifndef __UCLIBC__
  215. /* _stdio_term is done automatically when exiting if stdio is used.
  216. * See misc/internals/__uClibc_main.c and and stdlib/atexit.c. */
  217. atexit(_stdio_term);
  218. #endif /* __UCLIBC__ */
  219. }
  220. /**********************************************************************/
  221. #if !(__MASK_READING & __FLAG_UNGOT)
  222. #error Assumption violated about __MASK_READING and __FLAG_UNGOT
  223. #endif
  224. #ifdef __UCLIBC_HAS_THREADS__
  225. #include <pthread.h>
  226. #endif
  227. #ifndef NDEBUG
  228. void _stdio_validate_FILE(const FILE *stream)
  229. {
  230. #ifdef __UCLIBC_HAS_THREADS__
  231. assert(((unsigned int)(stream->__user_locking)) <= 2);
  232. #endif
  233. #warning Define a constant for minimum possible valid __filedes?
  234. assert(stream->__filedes >= -3);
  235. if (stream->__filedes < 0) {
  236. /* assert((stream->__filedes != -1) */
  237. /* #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
  238. /* || (stream->__cookie == &stream->__filedes) /\* custom *\/ */
  239. /* #endif */
  240. /* ); */
  241. /* assert((stream->__filedes == -1) || __STDIO_STREAM_IS_FBF(stream)); */
  242. assert(!__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)
  243. || __STDIO_STREAM_IS_NARROW(stream));
  244. assert(!__STDIO_STREAM_IS_FAKE_VSSCANF(stream)
  245. || __STDIO_STREAM_IS_NARROW(stream));
  246. #ifdef __STDIO_STREAM_IS_FAKE_VSWPRINTF
  247. assert(!__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)
  248. || __STDIO_STREAM_IS_WIDE(stream));
  249. #endif
  250. #ifdef __STDIO_STREAM_IS_FAKE_VSWSCANF
  251. assert(!__STDIO_STREAM_IS_FAKE_VSWSCANF(stream)
  252. || __STDIO_STREAM_IS_WIDE(stream));
  253. #endif
  254. }
  255. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  256. if (stream->__cookie != &stream->__filedes) { /* custom */
  257. assert(stream->__filedes == -1);
  258. }
  259. #endif
  260. /* Can not be both narrow and wide oriented at the same time. */
  261. assert(!(__STDIO_STREAM_IS_NARROW(stream)
  262. && __STDIO_STREAM_IS_WIDE(stream)));
  263. /* The following impossible case is used to disable a stream. */
  264. if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  265. == (__FLAG_READONLY|__FLAG_WRITEONLY)
  266. ) {
  267. assert(stream->__modeflags == (__FLAG_READONLY|__FLAG_WRITEONLY));
  268. assert(stream->__filedes == -1);
  269. #ifdef __STDIO_BUFFERS
  270. assert(stream->__bufpos == stream->__bufstart);
  271. assert(stream->__bufread == stream->__bufstart);
  272. # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  273. assert(stream->__bufputc_u == stream->__bufstart);
  274. # endif
  275. # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  276. assert(stream->__bufgetc_u == stream->__bufstart);
  277. # endif
  278. #endif
  279. }
  280. if (__STDIO_STREAM_IS_READONLY(stream)) {
  281. /* assert(!__STDIO_STREAM_IS_WRITEONLY(stream)); */
  282. assert(!__STDIO_STREAM_IS_WRITING(stream));
  283. if (stream->__modeflags & __FLAG_UNGOT) {
  284. assert(((unsigned)(stream->__ungot[1])) <= 1);
  285. assert(!__FEOF_UNLOCKED(stream));
  286. }
  287. }
  288. if (__STDIO_STREAM_IS_WRITEONLY(stream)) {
  289. /* assert(!__STDIO_STREAM_IS_READONLY(stream)); */
  290. assert(!__STDIO_STREAM_IS_READING(stream));
  291. assert(!(stream->__modeflags & __FLAG_UNGOT));
  292. }
  293. if (__STDIO_STREAM_IS_NBF(stream)) {
  294. /* We require that all non buffered streams have no buffer. */
  295. assert(!__STDIO_STREAM_BUFFER_SIZE(stream));
  296. }
  297. assert((stream->__modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
  298. #ifdef __STDIO_BUFFERS
  299. /* Ensure __bufstart <= __bufpos <= __bufend. */
  300. assert(stream->__bufpos >= stream->__bufstart);
  301. assert(stream->__bufpos <= stream->__bufend);
  302. /* Ensure __bufstart <= __bufread <= __bufend. */
  303. assert(stream->__bufread >= stream->__bufstart);
  304. assert(stream->__bufread <= stream->__bufend);
  305. #endif
  306. /* If EOF, then we must have no buffered readable or ungots. */
  307. if (__FEOF_UNLOCKED(stream)) {
  308. #ifdef __STDIO_BUFFERS
  309. assert(stream->__bufpos == stream->__bufread);
  310. #endif
  311. assert(!(stream->__modeflags & __FLAG_UNGOT));
  312. }
  313. if (!__STDIO_STREAM_IS_WRITING(stream)) {
  314. #ifdef __STDIO_BUFFERS
  315. /* If not writing, then putc macro must be disabled. */
  316. # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  317. assert(stream->__bufputc_u == stream->__bufstart);
  318. # endif
  319. #endif
  320. }
  321. if (!__STDIO_STREAM_IS_READING(stream)) {
  322. /* If not reading, then can not have ungots. */
  323. assert(!(stream->__modeflags & __FLAG_UNGOT));
  324. #ifdef __STDIO_BUFFERS
  325. /* Ensure __bufread == __bufstart. */
  326. assert(stream->__bufread == stream->__bufstart);
  327. /* If not reading, then getc macro must be disabled. */
  328. # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  329. assert(stream->__bufgetc_u == stream->__bufstart);
  330. # endif
  331. #endif
  332. }
  333. if (__STDIO_STREAM_IS_READING(stream)) {
  334. assert(!__STDIO_STREAM_IS_WRITING(stream));
  335. #ifdef __STDIO_BUFFERS
  336. /* Ensure __bufpos <= __bufread. */
  337. assert(stream->__bufpos <= stream->__bufread);
  338. /* Ensure __bufgetc_u is valid. */
  339. # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  340. assert(stream->__bufgetc_u >= stream->__bufstart);
  341. assert(stream->__bufgetc_u <= stream->__bufread);
  342. # endif
  343. #endif
  344. }
  345. if (__STDIO_STREAM_IS_WRITING(stream)) {
  346. assert(!__STDIO_STREAM_IS_READING(stream));
  347. #ifdef __STDIO_BUFFERS
  348. # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  349. assert(stream->__bufputc_u >= stream->__bufstart);
  350. assert(stream->__bufputc_u <= stream->__bufend);
  351. # endif
  352. #endif
  353. }
  354. /* If have an ungotten char, then getc (and putc) must be disabled. */
  355. /* Also, wide streams must have the getc/putc macros disabled. */
  356. if ((stream->__modeflags & __FLAG_UNGOT)
  357. || __STDIO_STREAM_IS_WIDE(stream)
  358. ) {
  359. #ifdef __STDIO_BUFFERS
  360. # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
  361. assert(stream->__bufputc_u == stream->__bufstart);
  362. # endif
  363. # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  364. assert(stream->__bufgetc_u == stream->__bufstart);
  365. # endif
  366. #endif
  367. }
  368. /* TODO -- filepos? ungot_width? filedes? nextopen? */
  369. }
  370. #endif