2 * Copyright (c) 1990 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 <<freopen64>>---open a large file using an existing file descriptor
29 FILE *freopen64(const char *<[file]>, const char *<[mode]>,
31 FILE *_freopen64_r(struct _reent *<[ptr]>, const char *<[file]>,
32 const char *<[mode]>, FILE *<[fp]>);
35 Use this variant of <<fopen64>> 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, <<freopen64>>
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 <<freopen>> is a glibc extension.
58 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
59 <<lseek64>>, <<open64>>, <<read>>, <<sbrk>>, <<write>>.
72 * Re-direct an existing, open (probably) file to some other file.
75 #ifdef __LARGE64_FILES
78 _freopen64_r (struct _reent
*ptr
,
84 int flags
, oflags
, oflags2
;
90 /* We can't use the _newlib_flockfile_XXX macros here due to the
91 interlocked locking with the sfp_lock. */
92 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
94 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &__oldcancel
);
96 oflags2
= fp
->_flags2
;
97 if (!(oflags2
& __SNLK
))
100 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
102 if (!(oflags2
& __SNLK
))
104 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
105 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
112 * Remember whether the stream was open to begin with, and
113 * which file descriptor (if any) was associated with it.
114 * If it was attached to a descriptor, defer closing it,
115 * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
116 * This is unnecessary if it was not a Unix file.
120 fp
->_flags
= __SEOF
; /* hold on to it */
123 if (fp
->_flags
& __SWR
)
126 * If close is NULL, closing is a no-op, hence pointless.
127 * If file is NULL, the file should not be closed.
129 if (fp
->_close
!= NULL
&& file
!= NULL
)
130 fp
->_close (ptr
, fp
->_cookie
);
134 * Now get a new descriptor to refer to the new file, or reuse the
135 * existing file descriptor if file is NULL.
140 f
= _open64_r (ptr
, (char *) file
, oflags
, 0666);
141 e
= _REENT_ERRNO(ptr
);
148 * Reuse the file descriptor, but only if the new access mode is
149 * equal or less permissive than the old. F_SETFL correctly
150 * ignores creation flags.
153 if ((oldflags
= _fcntl_r (ptr
, f
, F_GETFL
, 0)) == -1
154 || ! ((oldflags
& O_ACCMODE
) == O_RDWR
155 || ((oldflags
^ oflags
) & O_ACCMODE
) == 0)
156 || _fcntl_r (ptr
, f
, F_SETFL
, oflags
) == -1)
159 /* We cannot modify without fcntl support. */
165 * F_SETFL doesn't change textmode. Don't mess with modes of ttys.
167 if (0 <= f
&& ! isatty (f
)
168 && setmode (f
, oflags
& (O_BINARY
| O_TEXT
)) == -1)
175 if (fp
->_close
!= NULL
)
176 fp
->_close (ptr
, fp
->_cookie
);
181 * Finish closing fp. Even if the open succeeded above,
182 * we cannot keep fp->_base: it may be the wrong size.
183 * This loses the effect of any setbuffer calls,
184 * but stdio has always done this before.
187 if (fp
->_flags
& __SMBF
)
188 _free_r (ptr
, (char *) fp
->_bf
._base
);
192 fp
->_bf
._base
= NULL
;
201 fp
->_flags
&= ~__SORD
;
202 fp
->_flags2
&= ~__SWID
;
203 memset (&fp
->_mbstate
, 0, sizeof (_mbstate_t
));
206 { /* did not get it after all */
207 __sfp_lock_acquire ();
208 fp
->_flags
= 0; /* set it free */
209 _REENT_ERRNO(ptr
) = e
; /* restore in case _close clobbered */
210 if (!(oflags2
& __SNLK
))
212 #ifndef __SINGLE_THREAD__
213 __lock_close_recursive (fp
->_lock
);
215 __sfp_lock_release ();
216 #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS)
217 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
224 fp
->_cookie
= (void *) fp
;
226 fp
->_write
= __swrite64
;
228 fp
->_seek64
= __sseek64
;
229 fp
->_close
= __sclose
;
232 if (__stextmode(fp
->_file
))
233 fp
->_flags
|= __SCLE
;
236 fp
->_flags
|= __SL64
;
238 if (!(oflags2
& __SNLK
))
240 #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS)
241 pthread_setcancelstate (__oldcancel
, &__oldcancel
);
249 freopen64 (const char *file
,
253 return _freopen64_r (_REENT
, file
, mode
, fp
);
256 #endif /* !_REENT_ONLY */
258 #endif /* __LARGE64_FILES */