12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "_stdio.h"
- libc_hidden_proto(fclose)
- libc_hidden_proto(close)
- libc_hidden_proto(fflush_unlocked)
- int fclose(register FILE *stream)
- {
- int rv = 0;
- __STDIO_AUTO_THREADLOCK_VAR;
-
- #ifdef __STDIO_HAS_OPENLIST
- {
- register FILE *ptr;
- __STDIO_THREADLOCK_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;
- if (!ptr) {
- return EOF;
- }
- }
- #endif
- __STDIO_AUTO_THREADLOCK(stream);
- __STDIO_STREAM_VALIDATE(stream);
- #ifdef __STDIO_BUFFERS
-
- if (__STDIO_STREAM_IS_WRITING(stream)) {
- rv = fflush_unlocked(stream);
- }
- #endif
- if (__CLOSE(stream) < 0) {
- rv = EOF;
- }
- stream->__filedes = -1;
-
- stream->__modeflags &= (__FLAG_FREEBUF|__FLAG_FREEFILE);
- stream->__modeflags |= (__FLAG_READONLY|__FLAG_WRITEONLY);
- #ifndef NDEBUG
- __STDIO_STREAM_RESET_GCS(stream);
-
- __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);
- __STDIO_STREAM_FREE_FILE(stream);
- return rv;
- }
- libc_hidden_def(fclose)
|