1 /** @file
2 Implementation of freopen as declared in <stdio.h>.
3
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License that accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Copyright (c) 1990, 1993
14 The Regents of the University of California. All rights reserved.
15
16 This code is derived from software contributed to Berkeley by
17 Chris Torek.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
22 - Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 - Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 - Neither the name of the University nor the names of its contributors
28 may be used to endorse or promote products derived from this software
29 without specific prior written permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
35 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 POSSIBILITY OF SUCH DAMAGE.
42
43 NetBSD: freopen.c,v 1.14 2003/08/07 16:43:25 agc Exp
44 freopen.c 8.1 (Berkeley) 6/4/93
45 **/
46 #include <LibConfig.h>
47 #include <sys/EfiCdefs.h>
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #include <assert.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <wchar.h>
58 #include <unistd.h>
59 #include "reentrant.h"
60 #include "local.h"
61
62 /*
63 * Re-direct an existing, open (probably) file to some other file.
64 * ANSI is written such that the original file gets closed if at
65 * all possible, no matter what.
66 */
67 FILE *
freopen(const char * file,const char * mode,FILE * fp)68 freopen(const char *file, const char *mode, FILE *fp)
69 {
70 int f;
71 int flags, isopen, oflags, sverrno, wantfd;
72
73 _DIAGASSERT(file != NULL);
74 _DIAGASSERT(mode != NULL);
75 _DIAGASSERT(fp != NULL);
76 if(fp == NULL) {
77 errno = EINVAL;
78 return (NULL);
79 }
80
81 if ((flags = __sflags(mode, &oflags)) == 0) {
82 (void) fclose(fp);
83 return (NULL);
84 }
85
86 if (!__sdidinit)
87 __sinit();
88
89 /*
90 * There are actually programs that depend on being able to "freopen"
91 * descriptors that weren't originally open. Keep this from breaking.
92 * Remember whether the stream was open to begin with, and which file
93 * descriptor (if any) was associated with it. If it was attached to
94 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
95 * should work. This is unnecessary if it was not a Unix file.
96 */
97 if (fp->_flags == 0) {
98 fp->_flags = __SEOF; /* hold on to it */
99 isopen = 0;
100 wantfd = -1;
101 } else {
102 /* flush the stream; ANSI doesn't require this. */
103 if (fp->_flags & __SWR)
104 (void) __sflush(fp);
105 /* if close is NULL, closing is a no-op, hence pointless */
106 isopen = fp->_close != NULL;
107 if (((wantfd = fp->_file) >= 0) && isopen) {
108 (void) (*fp->_close)(fp->_cookie);
109 isopen = 0;
110 }
111 }
112
113 /* Get a new descriptor to refer to the new file. */
114 f = open(file, oflags, DEFFILEMODE);
115 if (f < 0 && isopen) {
116 /* If out of fd's close the old one and try again. */
117 if (errno == ENFILE || errno == EMFILE) {
118 (void) (*fp->_close)(fp->_cookie);
119 isopen = 0;
120 f = open(file, oflags, DEFFILEMODE);
121 }
122 }
123 sverrno = errno;
124
125 /*
126 * Finish closing fp. Even if the open succeeded above, we cannot
127 * keep fp->_base: it may be the wrong size. This loses the effect
128 * of any setbuffer calls, but stdio has always done this before.
129 */
130 if (isopen && (f != wantfd))
131 (void) (*fp->_close)(fp->_cookie);
132 if (fp->_flags & __SMBF)
133 free((char *)fp->_bf._base);
134 fp->_w = 0;
135 fp->_r = 0;
136 fp->_p = NULL;
137 fp->_bf._base = NULL;
138 fp->_bf._size = 0;
139 fp->_lbfsize = 0;
140 if (HASUB(fp))
141 FREEUB(fp);
142 WCIO_FREE(fp);
143 _UB(fp)._size = 0;
144 if (HASLB(fp))
145 FREELB(fp);
146 fp->_lb._size = 0;
147
148 if (f < 0) { /* did not get it after all */
149 fp->_flags = 0; /* set it free */
150 errno = sverrno; /* restore in case _close clobbered */
151 return (NULL);
152 }
153
154 if (oflags & O_NONBLOCK) {
155 struct stat st;
156 if (fstat(f, &st) == -1) {
157 sverrno = errno;
158 (void)close(f);
159 errno = sverrno;
160 return (NULL);
161 }
162 if (!S_ISREG(st.st_mode)) {
163 (void)close(f);
164 errno = EFTYPE;
165 return (NULL);
166 }
167 }
168
169 /*
170 * If reopening something that was open before on a real file, try
171 * to maintain the descriptor. Various C library routines (perror)
172 * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
173 */
174 if (wantfd >= 0 && f != wantfd) {
175 if (dup2(f, wantfd) >= 0) {
176 (void) close(f);
177 f = wantfd;
178 }
179 }
180
181 fp->_flags = (unsigned short)flags;
182 fp->_file = (short)f;
183 fp->_cookie = fp;
184 fp->_read = __sread;
185 fp->_write = __swrite;
186 fp->_seek = __sseek;
187 fp->_close = __sclose;
188
189 /*
190 * When reopening in append mode, even though we use O_APPEND,
191 * we need to seek to the end so that ftell() gets the right
192 * answer. If the user then alters the seek pointer, or
193 * the file extends, this will fail, but there is not much
194 * we can do about this. (We could set __SAPP and check in
195 * fseek and ftell.)
196 */
197 if (oflags & O_APPEND)
198 (void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
199 return (fp);
200 }
201