Update.
[glibc.git] / libio / freopen.c
bloba38313c334e20fd12341e9d3c4b3c60fb1c8d363
1 /* Copyright (C) 1993,95,96,97,98,2000,2001 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
17 02111-1307 USA.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
28 #include "libioP.h"
29 #include "stdio.h"
30 #include <stdlib.h>
32 #include <shlib-compat.h>
33 #include <fd_to_filename.h>
35 FILE*
36 freopen (filename, mode, fp)
37 const char* filename;
38 const char* mode;
39 FILE* fp;
41 FILE *result;
42 int fd = -1;
43 CHECK_FILE (fp, NULL);
44 if (!(fp->_flags & _IO_IS_FILEBUF))
45 return NULL;
46 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
47 _IO_flockfile (fp);
48 if (filename == NULL && _IO_fileno (fp) >= 0)
50 fd = __dup (_IO_fileno (fp));
51 if (fd != -1)
52 filename = fd_to_filename (fd);
54 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
55 if (&_IO_stdin_used == NULL)
56 /* If the shared C library is used by the application binary which
57 was linked against the older version of libio, we just use the
58 older one even for internal use to avoid trouble since a pointer
59 to the old libio may be passed into shared C library and wind
60 up here. */
61 result = _IO_old_freopen (filename, mode, fp);
62 else
63 #endif
64 result = _IO_freopen (filename, mode, fp);
65 if (result != NULL)
66 /* unbound stream orientation */
67 result->_mode = 0;
68 if (fd != -1)
70 __close (fd);
71 if (filename != NULL)
72 free ((char *) filename);
74 _IO_funlockfile (fp);
75 _IO_cleanup_region_end (0);
76 return result;