1 /* Copyright (C) 1991, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* Defined in fopen.c. */
24 extern int __getmode
__P ((const char *, __io_mode
*));
26 /* Defined in sysd-stdio.c. */
27 extern int __stdio_reopen
__P ((const char *filename
, __io_mode mode
,
28 void *cookieptr
, __io_close_fn closefn
));
30 /* Replace STREAM, opening it on FILENAME. */
32 freopen (filename
, mode
, stream
)
35 register FILE *stream
;
40 if (!__getmode (mode
, &m
))
42 (void) fclose (stream
);
47 if (stream
->__mode
.__write
)
48 /* Flush the stream. */
49 (void) fflush (stream
);
51 /* Open the file, attempting to preserve the old cookie value. */
52 cookie
= stream
->__cookie
;
53 if (__stdio_reopen (filename
, m
, &cookie
,
55 stream
->__io_funcs
.__close
:
59 (void) fclose (stream
);
64 /* Close the stream, first disabling its cookie close function because
65 __stdio_reopen has already dealt with closing the old cookie. */
66 stream
->__seen
= 1; /* It might have no functions yet. */
67 stream
->__io_funcs
.__close
= NULL
;
68 (void) fclose (stream
);
70 stream
->__magic
= _IOMAGIC
;
71 stream
->__cookie
= cookie
;