]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/freopen.c
2 Implementation of freopen as declared in <stdio.h>.
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.
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.
13 Copyright (c) 1990, 1993
14 The Regents of the University of California. All rights reserved.
16 This code is derived from software contributed to Berkeley by
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
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.
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.
43 NetBSD: freopen.c,v 1.14 2003/08/07 16:43:25 agc Exp
44 freopen.c 8.1 (Berkeley) 6/4/93
46 #include <LibConfig.h>
47 #include <sys/EfiCdefs.h>
49 #include <sys/types.h>
59 #include "reentrant.h"
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.
68 freopen(const char *file
, const char *mode
, FILE *fp
)
71 int flags
, isopen
, oflags
, sverrno
, wantfd
;
73 _DIAGASSERT(file
!= NULL
);
74 _DIAGASSERT(mode
!= NULL
);
75 _DIAGASSERT(fp
!= NULL
);
81 if ((flags
= __sflags(mode
, &oflags
)) == 0) {
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.
97 if (fp
->_flags
== 0) {
98 fp
->_flags
= __SEOF
; /* hold on to it */
102 /* flush the stream; ANSI doesn't require this. */
103 if (fp
->_flags
& __SWR
)
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
);
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
);
120 f
= open(file
, oflags
, DEFFILEMODE
);
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.
130 if (isopen
&& (f
!= wantfd
))
131 (void) (*fp
->_close
)(fp
->_cookie
);
132 if (fp
->_flags
& __SMBF
)
133 free((char *)fp
->_bf
._base
);
137 fp
->_bf
._base
= NULL
;
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 */
154 if (oflags
& O_NONBLOCK
) {
156 if (fstat(f
, &st
) == -1) {
162 if (!S_ISREG(st
.st_mode
)) {
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.
174 if (wantfd
>= 0 && f
!= wantfd
) {
175 if (dup2(f
, wantfd
) >= 0) {
181 fp
->_flags
= (unsigned short)flags
;
182 fp
->_file
= (short)f
;
185 fp
->_write
= __swrite
;
187 fp
->_close
= __sclose
;
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
197 if (oflags
& O_APPEND
)
198 (void) __sseek((void *)fp
, (fpos_t)0, SEEK_END
);