_stdio.c 13 KB

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