Add code for _dl_platform handling.
[glibc/history.git] / stdio / freopen.c
blob13aae4672b04878b0c49c6e6b97e86d60fc1d603
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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <stdio.h>
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. */
31 FILE *
32 freopen (filename, mode, stream)
33 const char *filename;
34 const char *mode;
35 register FILE *stream;
37 __io_mode m;
38 void *cookie;
40 if (!__getmode (mode, &m))
42 (void) fclose (stream);
43 __set_errno (EINVAL);
44 return NULL;
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,
54 stream->__seen ?
55 stream->__io_funcs.__close :
56 __stdio_close))
58 int save = errno;
59 (void) fclose (stream);
60 __set_errno (save);
61 return NULL;
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;
72 stream->__mode = m;
74 return stream;