123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- #include "_stdio.h"
- #ifdef __UCLIBC_HAS_WCHAR__
- #define __STDIO_FILE_INIT_WUNGOT { 0, 0 },
- #else
- #define __STDIO_FILE_INIT_WUNGOT
- #endif
- #ifdef __STDIO_GETC_MACRO
- # define __STDIO_FILE_INIT_BUFGETC(x) x,
- #else
- # define __STDIO_FILE_INIT_BUFGETC(x)
- #endif
- #ifdef __STDIO_PUTC_MACRO
- # define __STDIO_FILE_INIT_BUFPUTC(x) x,
- #else
- # define __STDIO_FILE_INIT_BUFPUTC(x)
- #endif
- #ifdef __STDIO_HAS_OPENLIST
- #define __STDIO_FILE_INIT_NEXT(next) (next),
- #else
- #define __STDIO_FILE_INIT_NEXT(next)
- #endif
- #ifdef __STDIO_BUFFERS
- #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
- (buf), (buf)+(bufsize), (buf), (buf),
- #else
- #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
- #endif
- #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
- #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
- &((stream).__filedes), { _cs_read, _cs_write, _cs_seek, _cs_close },
- #else
- #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
- #endif
- #ifdef __STDIO_MBSTATE
- #define __STDIO_FILE_INIT_MBSTATE \
- { 0, 0 },
- #else
- #define __STDIO_FILE_INIT_MBSTATE
- #endif
- #ifdef __UCLIBC_HAS_XLOCALE__
- #define __STDIO_FILE_INIT_UNUSED \
- NULL,
- #else
- #define __STDIO_FILE_INIT_UNUSED
- #endif
- #ifdef __UCLIBC_HAS_THREADS__
- #define __STDIO_FILE_INIT_THREADSAFE \
- 2, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
- #else
- #define __STDIO_FILE_INIT_THREADSAFE
- #endif
- #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
- { (flags), \
- { 0, 0 }, \
- (filedes), \
- __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
- __STDIO_FILE_INIT_BUFGETC((buf)) \
- __STDIO_FILE_INIT_BUFPUTC((buf)) \
- __STDIO_FILE_INIT_NEXT(next) \
- __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
- __STDIO_FILE_INIT_WUNGOT \
- __STDIO_FILE_INIT_MBSTATE \
- __STDIO_FILE_INIT_UNUSED \
- __STDIO_FILE_INIT_THREADSAFE \
- }
- #ifdef __STDIO_BUFFERS
- static unsigned char _fixed_buffers[2 * BUFSIZ];
- #endif
- static FILE _stdio_streams[] = {
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], \
- __FLAG_LBF|__FLAG_READONLY, \
- 0, \
- _stdio_streams + 1, \
- _fixed_buffers, \
- BUFSIZ ),
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], \
- __FLAG_LBF|__FLAG_WRITEONLY, \
- 1, \
- _stdio_streams + 2, \
- _fixed_buffers + BUFSIZ, \
- BUFSIZ ),
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], \
- __FLAG_NBF|__FLAG_WRITEONLY, \
- 2, \
- NULL, \
- NULL, \
- 0 )
- };
- FILE *stdin = _stdio_streams;
- FILE *stdout = _stdio_streams + 1;
- FILE *stderr = _stdio_streams + 2;
- #ifdef __STDIO_GETC_MACRO
- FILE *__stdin = _stdio_streams;
- #endif
- #ifdef __STDIO_PUTC_MACRO
- FILE *__stdout = _stdio_streams + 1;
- #endif
- #ifdef __STDIO_HAS_OPENLIST
- FILE *_stdio_openlist = _stdio_streams;
- # ifdef __UCLIBC_HAS_THREADS__
- pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- int _stdio_openlist_delflag = 0;
- # endif
- #endif
- #ifdef __UCLIBC_HAS_THREADS__
- int _stdio_user_locking = 2;
- void attribute_hidden __stdio_init_mutex(pthread_mutex_t *m)
- {
- static const pthread_mutex_t __stdio_mutex_initializer
- = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- __memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
- }
- #endif
- void _stdio_term(void)
- {
- #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
- register FILE *ptr;
- #ifdef __UCLIBC_HAS_THREADS__
-
-
- __stdio_init_mutex(&_stdio_openlist_lock);
-
- for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
- if (__STDIO_ALWAYS_THREADTRYLOCK(ptr)) {
-
- ptr->__modeflags = (__FLAG_READONLY|__FLAG_WRITEONLY);
- __STDIO_STREAM_DISABLE_GETC(ptr);
- __STDIO_STREAM_DISABLE_PUTC(ptr);
- __STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
- }
-
- ptr->__user_locking = 1;
- __stdio_init_mutex(&ptr->__lock);
- }
- #endif
-
- for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
- #ifdef __STDIO_BUFFERS
-
- if (__STDIO_STREAM_IS_WRITING(ptr)) {
- __STDIO_COMMIT_WRITE_BUFFER(ptr);
- }
- #endif
- #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
-
- if (ptr->__cookie != &ptr->__filedes) {
- __CLOSE(ptr);
- }
- #endif
- }
- #endif
- }
- void _stdio_init(void)
- {
- #ifdef __STDIO_BUFFERS
- int old_errno = errno;
-
- _stdio_streams[0].__modeflags ^= (1-isatty(0)) * __FLAG_LBF;
- _stdio_streams[1].__modeflags ^= (1-isatty(1)) * __FLAG_LBF;
- __set_errno(old_errno);
- #endif
- #ifndef __UCLIBC__
-
- atexit(_stdio_term);
- #endif
- }
- #if !(__MASK_READING & __FLAG_UNGOT)
- #error Assumption violated about __MASK_READING and __FLAG_UNGOT
- #endif
- #ifdef __UCLIBC_HAS_THREADS__
- #include <pthread.h>
- #endif
- #ifndef NDEBUG
- void _stdio_validate_FILE(const FILE *stream)
- {
- #ifdef __UCLIBC_HAS_THREADS__
- assert(((unsigned int)(stream->__user_locking)) <= 2);
- #endif
- #warning Define a constant for minimum possible valid __filedes?
- assert(stream->__filedes >= -3);
- if (stream->__filedes < 0) {
- assert(!__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)
- || __STDIO_STREAM_IS_NARROW(stream));
- assert(!__STDIO_STREAM_IS_FAKE_VSSCANF(stream)
- || __STDIO_STREAM_IS_NARROW(stream));
- #ifdef __STDIO_STREAM_IS_FAKE_VSWPRINTF
- assert(!__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)
- || __STDIO_STREAM_IS_WIDE(stream));
- #endif
- #ifdef __STDIO_STREAM_IS_FAKE_VSWSCANF
- assert(!__STDIO_STREAM_IS_FAKE_VSWSCANF(stream)
- || __STDIO_STREAM_IS_WIDE(stream));
- #endif
- }
- #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
- if (stream->__cookie != &stream->__filedes) {
- assert(stream->__filedes == -1);
- }
- #endif
-
- assert(!(__STDIO_STREAM_IS_NARROW(stream)
- && __STDIO_STREAM_IS_WIDE(stream)));
-
- if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
- == (__FLAG_READONLY|__FLAG_WRITEONLY)
- ) {
- assert(stream->__modeflags == (__FLAG_READONLY|__FLAG_WRITEONLY));
- assert(stream->__filedes == -1);
- #ifdef __STDIO_BUFFERS
- assert(stream->__bufpos == stream->__bufstart);
- assert(stream->__bufread == stream->__bufstart);
- # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
- assert(stream->__bufputc_u == stream->__bufstart);
- # endif
- # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
- assert(stream->__bufgetc_u == stream->__bufstart);
- # endif
- #endif
- }
- if (__STDIO_STREAM_IS_READONLY(stream)) {
- assert(!__STDIO_STREAM_IS_WRITING(stream));
- if (stream->__modeflags & __FLAG_UNGOT) {
- assert(((unsigned)(stream->__ungot[1])) <= 1);
- assert(!__FEOF_UNLOCKED(stream));
- }
- }
- if (__STDIO_STREAM_IS_WRITEONLY(stream)) {
- assert(!__STDIO_STREAM_IS_READING(stream));
- assert(!(stream->__modeflags & __FLAG_UNGOT));
- }
- if (__STDIO_STREAM_IS_NBF(stream)) {
-
- assert(!__STDIO_STREAM_BUFFER_SIZE(stream));
- }
- assert((stream->__modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
- #ifdef __STDIO_BUFFERS
-
- assert(stream->__bufpos >= stream->__bufstart);
- assert(stream->__bufpos <= stream->__bufend);
-
- assert(stream->__bufread >= stream->__bufstart);
- assert(stream->__bufread <= stream->__bufend);
- #endif
-
- if (__FEOF_UNLOCKED(stream)) {
- #ifdef __STDIO_BUFFERS
- assert(stream->__bufpos == stream->__bufread);
- #endif
- assert(!(stream->__modeflags & __FLAG_UNGOT));
- }
- if (!__STDIO_STREAM_IS_WRITING(stream)) {
- #ifdef __STDIO_BUFFERS
-
- # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
- assert(stream->__bufputc_u == stream->__bufstart);
- # endif
- #endif
- }
- if (!__STDIO_STREAM_IS_READING(stream)) {
-
- assert(!(stream->__modeflags & __FLAG_UNGOT));
- #ifdef __STDIO_BUFFERS
-
- assert(stream->__bufread == stream->__bufstart);
-
- # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
- assert(stream->__bufgetc_u == stream->__bufstart);
- # endif
- #endif
- }
- if (__STDIO_STREAM_IS_READING(stream)) {
- assert(!__STDIO_STREAM_IS_WRITING(stream));
- #ifdef __STDIO_BUFFERS
-
- assert(stream->__bufpos <= stream->__bufread);
-
- # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
- assert(stream->__bufgetc_u >= stream->__bufstart);
- assert(stream->__bufgetc_u <= stream->__bufread);
- # endif
- #endif
- }
- if (__STDIO_STREAM_IS_WRITING(stream)) {
- assert(!__STDIO_STREAM_IS_READING(stream));
- #ifdef __STDIO_BUFFERS
- # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
- assert(stream->__bufputc_u >= stream->__bufstart);
- assert(stream->__bufputc_u <= stream->__bufend);
- # endif
- #endif
- }
-
-
- if ((stream->__modeflags & __FLAG_UNGOT)
- || __STDIO_STREAM_IS_WIDE(stream)
- ) {
- #ifdef __STDIO_BUFFERS
- # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
- assert(stream->__bufputc_u == stream->__bufstart);
- # endif
- # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
- assert(stream->__bufgetc_u == stream->__bufstart);
- # endif
- #endif
- }
-
- }
- #endif
|