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 | #ifndef __DO_LARGEFILE |
| 12 | # define FILEDES_ARG (-1) |
| 13 | #endif |
| 14 | |
| 15 | FILE *freopen(const char * __restrict filename, const char * __restrict mode, |
| 16 | register FILE * __restrict stream) |
| 17 | { |
| 18 | /* |
| 19 | * ANSI/ISO allow (implementation-defined) change of mode for an |
| 20 | * existing file if filename is NULL. It doesn't look like Linux |
| 21 | * supports this, so we don't here. |
| 22 | * |
| 23 | * NOTE: Whether or not the stream is free'd on failure is unclear |
| 24 | * w.r.t. ANSI/ISO. This implementation chooses to NOT free |
| 25 | * the stream and associated buffer if they were dynamically |
| 26 | * allocated. |
| 27 | * NOTE: Previous versions of uClibc did free dynamic storage. |
| 28 | * |
| 29 | * TODO: Apparently linux allows setting append mode. Implement? |
| 30 | */ |
| 31 | unsigned short dynmode; |
| 32 | register FILE *fp; |
| 33 | __STDIO_AUTO_THREADLOCK_VAR; |
| 34 | |
| 35 | __STDIO_AUTO_THREADLOCK(stream); |
| 36 | |
| 37 | __STDIO_STREAM_VALIDATE(stream); |
| 38 | |
| 39 | __STDIO_OPENLIST_INC_USE; /* Do not remove the file from the list. */ |
| 40 | |
| 41 | /* First, flush and close, but don't deallocate, the stream. */ |
| 42 | /* This also removes the stream for the open file list. */ |
| 43 | dynmode = (stream->__modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE)); |
| 44 | |
| 45 | stream->__modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE); |
| 46 | |
| 47 | /* Only call fclose on the stream if it is not already closed. */ |
| 48 | if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY)) |
| 49 | != (__FLAG_READONLY|__FLAG_WRITEONLY) |
| 50 | ) { |
| 51 | fclose(stream); /* Failures are ignored. */ |
| 52 | /* NOTE: fclose always does __STDIO_OPENLIST_INC_DEL_CNT. But we don't |
| 53 | * want to remove this FILE from the open list, even if the freopen fails. |
| 54 | * Consider the case of a failed freopen() on stdin. You probably still |
| 55 | * want to be able to call freopen() again. Similarly for other "malloc'd" |
| 56 | * streams. */ |
| 57 | __STDIO_OPENLIST_DEC_DEL_CNT; |
| 58 | } |
| 59 | |
| 60 | fp = _stdio_fopen(((intptr_t) filename), mode, stream, FILEDES_ARG); |
| 61 | if (!fp) { |
| 62 | /* Don't remove stream from the open file list and (potentially) free it. |
| 63 | * See _stdio_openlist_dec_use() in fflush.c. */ |
| 64 | stream->__modeflags = __FLAG_READONLY|__FLAG_WRITEONLY|__FLAG_FAILED_FREOPEN; |
| 65 | } |
| 66 | |
| 67 | /* Reset the allocation flags. */ |
| 68 | stream->__modeflags |= dynmode; |
| 69 | |
| 70 | __STDIO_OPENLIST_DEC_USE; |
| 71 | |
| 72 | __STDIO_AUTO_THREADUNLOCK(stream); |
| 73 | |
| 74 | return fp; |
| 75 | } |