]> 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
53e1e5c6 4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
2aa62f2b 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
53e1e5c6 8 http://opensource.org/licenses/bsd-license.\r
2aa62f2b 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
53e1e5c6 76 if(fp == NULL) {\r
77 errno = EINVAL;\r
78 return (NULL);\r
79 }\r
2aa62f2b 80\r
81 if ((flags = __sflags(mode, &oflags)) == 0) {\r
82 (void) fclose(fp);\r
83 return (NULL);\r
84 }\r
85\r
86 if (!__sdidinit)\r
87 __sinit();\r
88\r
89 /*\r
90 * There are actually programs that depend on being able to "freopen"\r
91 * descriptors that weren't originally open. Keep this from breaking.\r
92 * Remember whether the stream was open to begin with, and which file\r
93 * descriptor (if any) was associated with it. If it was attached to\r
94 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)\r
95 * should work. This is unnecessary if it was not a Unix file.\r
96 */\r
97 if (fp->_flags == 0) {\r
98 fp->_flags = __SEOF; /* hold on to it */\r
99 isopen = 0;\r
100 wantfd = -1;\r
101 } else {\r
102 /* flush the stream; ANSI doesn't require this. */\r
103 if (fp->_flags & __SWR)\r
104 (void) __sflush(fp);\r
105 /* if close is NULL, closing is a no-op, hence pointless */\r
106 isopen = fp->_close != NULL;\r
107 if ((wantfd = fp->_file) < 0 && isopen) {\r
108 (void) (*fp->_close)(fp->_cookie);\r
109 isopen = 0;\r
110 }\r
111 }\r
112\r
113 /* Get a new descriptor to refer to the new file. */\r
114 f = open(file, oflags, DEFFILEMODE);\r
115 if (f < 0 && isopen) {\r
116 /* If out of fd's close the old one and try again. */\r
117 if (errno == ENFILE || errno == EMFILE) {\r
118 (void) (*fp->_close)(fp->_cookie);\r
119 isopen = 0;\r
120 f = open(file, oflags, DEFFILEMODE);\r
121 }\r
122 }\r
123 sverrno = errno;\r
124\r
125 /*\r
126 * Finish closing fp. Even if the open succeeded above, we cannot\r
127 * keep fp->_base: it may be the wrong size. This loses the effect\r
128 * of any setbuffer calls, but stdio has always done this before.\r
129 */\r
130 if (isopen && f != wantfd)\r
131 (void) (*fp->_close)(fp->_cookie);\r
132 if (fp->_flags & __SMBF)\r
133 free((char *)fp->_bf._base);\r
134 fp->_w = 0;\r
135 fp->_r = 0;\r
136 fp->_p = NULL;\r
137 fp->_bf._base = NULL;\r
138 fp->_bf._size = 0;\r
139 fp->_lbfsize = 0;\r
140 if (HASUB(fp))\r
141 FREEUB(fp);\r
142 WCIO_FREE(fp);\r
143 _UB(fp)._size = 0;\r
144 if (HASLB(fp))\r
145 FREELB(fp);\r
146 fp->_lb._size = 0;\r
147\r
148 if (f < 0) { /* did not get it after all */\r
149 fp->_flags = 0; /* set it free */\r
150 errno = sverrno; /* restore in case _close clobbered */\r
151 return (NULL);\r
152 }\r
153\r
154 if (oflags & O_NONBLOCK) {\r
155 struct stat st;\r
156 if (fstat(f, &st) == -1) {\r
157 sverrno = errno;\r
158 (void)close(f);\r
159 errno = sverrno;\r
160 return (NULL);\r
161 }\r
162 if (!S_ISREG(st.st_mode)) {\r
163 (void)close(f);\r
164 errno = EFTYPE;\r
165 return (NULL);\r
166 }\r
167 }\r
168\r
169 /*\r
170 * If reopening something that was open before on a real file, try\r
171 * to maintain the descriptor. Various C library routines (perror)\r
172 * assume stderr is always fd STDERR_FILENO, even if being freopen'd.\r
173 */\r
174 if (wantfd >= 0 && f != wantfd) {\r
175 if (dup2(f, wantfd) >= 0) {\r
176 (void) close(f);\r
177 f = wantfd;\r
178 }\r
179 }\r
180\r
181 fp->_flags = (unsigned short)flags;\r
182 fp->_file = (short)f;\r
183 fp->_cookie = fp;\r
184 fp->_read = __sread;\r
185 fp->_write = __swrite;\r
186 fp->_seek = __sseek;\r
187 fp->_close = __sclose;\r
188\r
189 /*\r
190 * When reopening in append mode, even though we use O_APPEND,\r
191 * we need to seek to the end so that ftell() gets the right\r
192 * answer. If the user then alters the seek pointer, or\r
193 * the file extends, this will fail, but there is not much\r
194 * we can do about this. (We could set __SAPP and check in\r
195 * fseek and ftell.)\r
196 */\r
197 if (oflags & O_APPEND)\r
198 (void) __sseek((void *)fp, (fpos_t)0, SEEK_END);\r
199 return (fp);\r
200}\r