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 * advertising materials, and 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 *<[file]>, const char *<[mode]>,
31 FILE *_freopen_r(struct _reent *<[ptr]>, const char *<[file]>,
32 const char *<[mode]>, FILE *<[fp]>);
36 FILE *freopen(<[file]>, <[mode]>, <[fp]>)
41 FILE *_freopen_r(<[ptr]>, <[file]>, <[mode]>, <[fp]>)
42 struct _reent *<[ptr]>;
48 Use this variant of <<fopen>> if you wish to specify a particular file
49 descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
52 If <[fp]> was associated with another file or stream, <<freopen>>
53 closes that other file or stream (but ignores any errors while closing
56 <[file]> and <[mode]> are used just as in <<fopen>>.
59 If successful, the result is the same as the argument <[fp]>. If the
60 file cannot be opened as specified, the result is <<NULL>>.
63 ANSI C requires <<freopen>>.
65 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
66 <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
77 * Re-direct an existing, open (probably) file to some other file.
81 _DEFUN (_freopen_r
, (ptr
, file
, mode
, fp
),
82 struct _reent
*ptr _AND
83 _CONST
char *file _AND
84 _CONST
char *mode _AND
94 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
102 * Remember whether the stream was open to begin with, and
103 * which file descriptor (if any) was associated with it.
104 * If it was attached to a descriptor, defer closing it,
105 * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
106 * This is unnecessary if it was not a Unix file.
110 fp
->_flags
= __SEOF
; /* hold on to it */
113 if (fp
->_flags
& __SWR
)
115 /* if close is NULL, closing is a no-op, hence pointless */
116 if (fp
->_close
!= NULL
)
117 (void) (*fp
->_close
) (fp
->_cookie
);
121 * Now get a new descriptor to refer to the new file.
124 f
= _open_r (ptr
, (char *) file
, oflags
, 0666);
128 * Finish closing fp. Even if the open succeeded above,
129 * we cannot keep fp->_base: it may be the wrong size.
130 * This loses the effect of any setbuffer calls,
131 * but stdio has always done this before.
134 if (fp
->_flags
& __SMBF
)
135 _free_r (ptr
, (char *) fp
->_bf
._base
);
139 fp
->_bf
._base
= NULL
;
150 { /* did not get it after all */
151 ptr
->_errno
= e
; /* restore in case _close clobbered */
153 #ifndef __SINGLE_THREAD__
154 __lock_close_recursive (*(_LOCK_RECURSIVE_T
*)&fp
->_lock
);
156 fp
->_flags
= 0; /* set it free */
162 fp
->_cookie
= (_PTR
) fp
;
164 fp
->_write
= __swrite
;
166 fp
->_close
= __sclose
;
169 if (__stextmode(fp
->_file
))
170 fp
->_flags
|= __SCLE
;
180 _DEFUN (freopen
, (file
, mode
, fp
),
181 _CONST
char *file _AND
182 _CONST
char *mode _AND
185 return _freopen_r (_REENT
, file
, mode
, fp
);
188 #endif /*!_REENT_ONLY */