2 * Copyright (c) 1990, 2007 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<freopen>>---open a file using an existing file descriptor
29 FILE *freopen(const char *restrict <[file]>, const char *restrict <[mode]>,
30 FILE *restrict <[fp]>);
31 FILE *_freopen_r(struct _reent *<[ptr]>, const char *restrict <[file]>,
32 const char *restrict <[mode]>, FILE *restrict <[fp]>);
35 Use this variant of <<fopen>> if you wish to specify a particular file
36 descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
39 If <[fp]> was associated with another file or stream, <<freopen>>
40 closes that other file or stream (but ignores any errors while closing
43 <[file]> and <[mode]> are used just as in <<fopen>>.
45 If <[file]> is <<NULL>>, the underlying stream is modified rather than
46 closed. The file cannot be given a more permissive access mode (for
47 example, a <[mode]> of "w" will fail on a read-only file descriptor),
48 but can change status such as append or binary mode. If modification
49 is not possible, failure occurs.
52 If successful, the result is the same as the argument <[fp]>. If the
53 file cannot be opened as specified, the result is <<NULL>>.
56 ANSI C requires <<freopen>>.
58 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
59 <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
74 * Re-direct an existing, open (probably) file to some other file.
78 _freopen_r (struct _reent
*ptr
,
79 const char *__restrict file
,
80 const char *__restrict mode
,
81 register FILE *__restrict fp
)
84 int flags
, oflags
, oflags2
;
89 /* We can't use the _newlib_flockfile_XXX macros here due to the
90 interlocked locking with the sfp_lock. */
91 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
93 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &__oldcancel
);
95 oflags2
= fp
->_flags2
;
96 if (!(oflags2
& __SNLK
))
99 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
101 if (!(oflags2
& __SNLK
))
103 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
104 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
111 * Remember whether the stream was open to begin with, and
112 * which file descriptor (if any) was associated with it.
113 * If it was attached to a descriptor, defer closing it,
114 * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
115 * This is unnecessary if it was not a Unix file.
119 fp
->_flags
= __SEOF
; /* hold on to it */
122 if (fp
->_flags
& __SWR
)
125 * If close is NULL, closing is a no-op, hence pointless.
126 * If file is NULL, the file should not be closed.
128 if (fp
->_close
!= NULL
&& file
!= NULL
)
129 fp
->_close (ptr
, fp
->_cookie
);
133 * Now get a new descriptor to refer to the new file, or reuse the
134 * existing file descriptor if file is NULL.
139 f
= _open_r (ptr
, (char *) file
, oflags
, 0666);
140 e
= _REENT_ERRNO(ptr
);
147 * Reuse the file descriptor, but only if the new access mode is
148 * equal or less permissive than the old. F_SETFL correctly
149 * ignores creation flags.
152 if ((oldflags
= _fcntl_r (ptr
, f
, F_GETFL
, 0)) == -1
153 || ! ((oldflags
& O_ACCMODE
) == O_RDWR
154 || ((oldflags
^ oflags
) & O_ACCMODE
) == 0)
155 || _fcntl_r (ptr
, f
, F_SETFL
, oflags
) == -1)
158 /* We cannot modify without fcntl support. */
164 * F_SETFL doesn't change textmode. Don't mess with modes of ttys.
166 if (0 <= f
&& ! _isatty_r (ptr
, f
)
167 && setmode (f
, oflags
& (O_BINARY
| O_TEXT
)) == -1)
174 if (fp
->_close
!= NULL
)
175 fp
->_close (ptr
, fp
->_cookie
);
180 * Finish closing fp. Even if the open succeeded above,
181 * we cannot keep fp->_base: it may be the wrong size.
182 * This loses the effect of any setbuffer calls,
183 * but stdio has always done this before.
186 if (fp
->_flags
& __SMBF
)
187 _free_r (ptr
, (char *) fp
->_bf
._base
);
191 fp
->_bf
._base
= NULL
;
200 fp
->_flags
&= ~__SORD
;
201 fp
->_flags2
&= ~__SWID
;
202 memset (&fp
->_mbstate
, 0, sizeof (_mbstate_t
));
205 { /* did not get it after all */
206 __sfp_lock_acquire ();
207 fp
->_flags
= 0; /* set it free */
208 _REENT_ERRNO(ptr
) = e
; /* restore in case _close clobbered */
209 if (!(oflags2
& __SNLK
))
211 #ifndef __SINGLE_THREAD__
212 __lock_close_recursive (fp
->_lock
);
214 __sfp_lock_release ();
215 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
216 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
223 fp
->_cookie
= (void *) fp
;
225 fp
->_write
= __swrite
;
227 fp
->_close
= __sclose
;
230 if (__stextmode (fp
->_file
))
231 fp
->_flags
|= __SCLE
;
234 if (!(oflags2
& __SNLK
))
236 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
237 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
245 freopen (const char *__restrict file
,
246 const char *__restrict mode
,
247 register FILE *__restrict fp
)
249 return _freopen_r (_REENT
, file
, mode
, fp
);
252 #endif /*!_REENT_ONLY */