_stdio.c 13 KB

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