netinet/in.h: add IPPROTO_MH
[uclibc-ng.git] / libc / stdio / freopen.c
blobf48a43b7332954c83f674e787cf1d86bb3123357
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
8 #include "_stdio.h"
11 #ifndef __DO_LARGEFILE
12 # define FILEDES_ARG (-1)
13 #endif
15 FILE *freopen(const char * __restrict filename, const char * __restrict mode,
16 register FILE * __restrict stream)
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.
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.
29 * TODO: Apparently linux allows setting append mode. Implement?
31 unsigned short dynmode;
32 register FILE *fp;
33 __STDIO_AUTO_THREADLOCK_VAR;
35 __STDIO_AUTO_THREADLOCK(stream);
37 __STDIO_STREAM_VALIDATE(stream);
39 __STDIO_OPENLIST_INC_USE; /* Do not remove the file from the list. */
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));
45 stream->__modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
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;
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;
67 /* Reset the allocation flags. */
68 stream->__modeflags |= dynmode;
70 __STDIO_OPENLIST_DEC_USE;
72 __STDIO_AUTO_THREADUNLOCK(stream);
74 return fp;