static void munge_stream(register FILE *stream, unsigned char *buf) { stream->__bufend = stream->__bufstart = buf; __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream); __STDIO_STREAM_DISABLE_GETC(stream); __STDIO_STREAM_DISABLE_PUTC(stream); }
int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence) { #if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE) return fseeko64(stream, offset, whence); #else __offmax_t pos = offset; int retval = -1; __STDIO_AUTO_THREADLOCK_VAR; if (((unsigned int) whence) > 2) { __set_errno(EINVAL); } else { __STDIO_AUTO_THREADLOCK(stream); __STDIO_STREAM_VALIDATE(stream); if ((!__STDIO_STREAM_IS_WRITING(stream) || !__STDIO_COMMIT_WRITE_BUFFER(stream)) && ((whence != SEEK_CUR) || (__stdio_adjust_position(stream, &pos) >= 0)) && (__SEEK(stream, &pos, whence) >= 0) ) { /* Clear reading/writing modes, EOF, and ungots. */ stream->__modeflags &= ~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF); /* Make sure all pointers are reset. */ __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream); __STDIO_STREAM_DISABLE_GETC(stream); __STDIO_STREAM_DISABLE_PUTC(stream); /* We reinitialize the mbstate object. Doing so is * implementation defined behavior. */ #ifdef __STDIO_MBSTATE __INIT_MBSTATE(&(stream->__state)); #endif #ifdef __UCLIBC_HAS_WCHAR__ stream->__ungot_width[0] = 0; #endif retval = 0; } __STDIO_STREAM_VALIDATE(stream); __STDIO_AUTO_THREADUNLOCK(stream); } return retval; #endif }
int ungetc(int c, register FILE *stream) { __STDIO_AUTO_THREADLOCK_VAR; __STDIO_AUTO_THREADLOCK(stream); __STDIO_STREAM_VALIDATE(stream); #ifdef __UCLIBC_MJN3_ONLY__ #warning CONSIDER: Make fast ungetc an option? #endif #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__ /* If buffered narrow reading with no ungot slots filled, and if not * ungetting a different char than the one last read from the buffer, * we can simply decrement the position and not worry about disabling * the getc macros. This will cut down on overhead in applications * that use getc/ungetc extensively (like gcc). */ /* NOTE: If we can use getc, then we are buffered narrow reading with * no ungot slots filled. */ if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream) && (c != EOF) && (stream->__bufpos > stream->__bufstart) && (stream->__bufpos[-1] == ((unsigned char)c)) ) { --stream->__bufpos; __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */ } else #endif /* Note: Even if c == EOF, we need to initialize/verify the * stream's orientation and ensure the stream is in reading * mode (if readable and properly oriented). */ if ((!__STDIO_STREAM_IS_NARROW_READING(stream) && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW)) || ((stream->__modeflags & __FLAG_UNGOT) && ((stream->__modeflags & 1) || stream->__ungot[1])) ) { c = EOF; } else if (c != EOF) { __STDIO_STREAM_DISABLE_GETC(stream); /* Flag this as a user ungot, as scanf does the necessary fixup. */ stream->__ungot[1] = 1; stream->__ungot[(++stream->__modeflags) & 1] = c; __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */ } __STDIO_STREAM_VALIDATE(stream); __STDIO_AUTO_THREADUNLOCK(stream); return c; }
/* We assume here that we are the only remaining thread. */ void attribute_hidden _stdio_term(void) { #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) register FILE *ptr; #ifdef __UCLIBC_HAS_THREADS__ /* First, make sure the open file list is unlocked. If it was * locked, then I suppose there is a chance that a pointer in the * chain might be corrupt due to a partial store. */ __stdio_init_mutex(&_stdio_openlist_add_lock); #warning check #ifdef __STDIO_BUFFERS __stdio_init_mutex(&_stdio_openlist_del_lock); #endif /* Next we need to worry about the streams themselves. If a stream * is currently locked, then it may be in an invalid state. So we * 'disable' it in case a custom stream is stacked on top of it. * Then we reinitialize the locks. */ for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) { if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) { /* The stream is already locked, so we don't want to touch it. * However, if we have custom streams, we can't just close it * or leave it locked since a custom stream may be stacked * on top of it. So we do unlock it, while also disabling it. */ 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; /* Set locking mode to "by caller". */ __stdio_init_mutex(&ptr->__lock); /* Shouldn't be necessary, but... */ } #endif /* Finally, flush all writing streams and shut down all custom streams. * NOTE: We assume that any stacking by custom streams is done on top * of streams previously allocated, and hence further down the * list. Otherwise we have no way of knowing the order in which * to shut them down. * Remember that freopen() counts as a new allocation here, even * though the stream is reused. That's because it moves the * stream to the head of the list. */ for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) { #ifdef __STDIO_BUFFERS /* Write any pending buffered chars. */ if (__STDIO_STREAM_IS_WRITING(ptr)) { __STDIO_COMMIT_WRITE_BUFFER(ptr); } #endif #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ /* Actually close all custom streams to perform any special cleanup. */ if (ptr->__cookie != &ptr->__filedes) { __CLOSE(ptr); } #endif } #endif }
int fclose(register FILE *stream) { int rv = 0; __STDIO_AUTO_THREADLOCK_VAR; #ifdef __STDIO_HAS_OPENLIST #if !defined(__UCLIBC_HAS_THREADS__) || !defined(__STDIO_BUFFERS) /* First, remove the file from the open file list. */ { FILE *ptr; __STDIO_THREADLOCK_OPENLIST_DEL; __STDIO_THREADLOCK_OPENLIST_ADD; ptr = _stdio_openlist; if ((ptr = _stdio_openlist) == stream) { _stdio_openlist = stream->__nextopen; } else { while (ptr) { if (ptr->__nextopen == stream) { ptr->__nextopen = stream->__nextopen; break; } ptr = ptr->__nextopen; } } __STDIO_THREADUNLOCK_OPENLIST_ADD; __STDIO_THREADUNLOCK_OPENLIST_DEL; } #endif #endif __STDIO_AUTO_THREADLOCK(stream); __STDIO_STREAM_VALIDATE(stream); #ifdef __STDIO_BUFFERS /* Write any pending buffered chars. */ if (__STDIO_STREAM_IS_WRITING(stream)) { rv = fflush_unlocked(stream); } #endif if (__CLOSE(stream) < 0) { /* Must close even if fflush failed. */ rv = EOF; } stream->__filedes = -1; /* We need a way for freopen to know that a file has been closed. * Since a file can't be both readonly and writeonly, that makes * an effective signal. It also has the benefit of disabling * transitions to either reading or writing. */ #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS) /* Before we mark the file as closed, make sure we increment the openlist use count * so it isn't freed under us while still cleaning up. */ __STDIO_OPENLIST_INC_USE; #endif stream->__modeflags &= (__FLAG_FREEBUF|__FLAG_FREEFILE); stream->__modeflags |= (__FLAG_READONLY|__FLAG_WRITEONLY); #ifndef NDEBUG __STDIO_STREAM_RESET_GCS(stream); /* Reinitialize everything (including putc since fflush could fail). */ __STDIO_STREAM_DISABLE_GETC(stream); __STDIO_STREAM_DISABLE_PUTC(stream); __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream); # ifdef __UCLIBC_HAS_WCHAR__ stream->__ungot_width[0] = 0; # endif # ifdef __STDIO_MBSTATE __INIT_MBSTATE(&(stream->__state)); # endif #endif __STDIO_AUTO_THREADUNLOCK(stream); __STDIO_STREAM_FREE_BUFFER(stream); #ifdef __UCLIBC_MJN3_ONLY__ #warning REMINDER: inefficient - locks and unlocks twice and walks whole list #endif #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS) /* inefficient - locks/unlocks twice and walks whole list */ __STDIO_OPENLIST_INC_DEL_CNT; __STDIO_OPENLIST_DEC_USE; /* This with free the file if necessary. */ #else __STDIO_STREAM_FREE_FILE(stream); #endif return rv; }