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