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