xref: /haiku/src/system/libroot/posix/glibc/libio/freopen.c (revision 5af32e752606778be5dd7379f319fe43cb3f6b8c)
1 /* Copyright (C) 1993,95,96,97,98,2000,2001,2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
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.
8 
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.
13 
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.
18 
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.  */
27 
28 #include "libioP.h"
29 #include "stdio.h"
30 
31 #include <shlib-compat.h>
32 
33 FILE*
freopen(filename,mode,fp)34 freopen (filename, mode, fp)
35      const char* filename;
36      const char* mode;
37      FILE* fp;
38 {
39   FILE *result;
40   CHECK_FILE (fp, NULL);
41   if (!(fp->_flags & _IO_IS_FILEBUF))
42     return NULL;
43   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
44   _IO_flockfile (fp);
45 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
46   if (&_IO_stdin_used == NULL)
47     /* If the shared C library is used by the application binary which
48        was linked against the older version of libio, we just use the
49        older one even for internal use to avoid trouble since a pointer
50        to the old libio may be passed into shared C library and wind
51        up here. */
52     result = _IO_old_freopen (filename, mode, fp);
53   else
54 #endif
55     result = _IO_freopen (filename, mode, fp);
56   if (result != NULL)
57     /* unbound stream orientation */
58     result->_mode = 0;
59   _IO_funlockfile (fp);
60   _IO_cleanup_region_end (0);
61   return result;
62 }
63 
64 #if 0
65 #include <fd_to_filename.h>
66 
67 FILE*
68 freopen (filename, mode, fp)
69      const char* filename;
70      const char* mode;
71      FILE* fp;
72 {
73   FILE *result;
74   int fd = -1;
75   CHECK_FILE (fp, NULL);
76   if (!(fp->_flags & _IO_IS_FILEBUF))
77     return NULL;
78   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
79   _IO_flockfile (fp);
80   if (filename == NULL && _IO_fileno (fp) >= 0)
81     {
82       fd = __dup (_IO_fileno (fp));
83       if (fd != -1)
84 	filename = fd_to_filename (fd);
85     }
86 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
87   if (&_IO_stdin_used == NULL)
88     {
89       /* If the shared C library is used by the application binary which
90 	 was linked against the older version of libio, we just use the
91 	 older one even for internal use to avoid trouble since a pointer
92 	 to the old libio may be passed into shared C library and wind
93 	 up here. */
94       _IO_old_file_close_it (fp);
95       _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_old_file_jumps;
96       result = _IO_old_file_fopen (fp, filename, mode);
97     }
98   else
99 #endif
100     {
101       INTUSE(_IO_file_close_it) (fp);
102       _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &INTUSE(_IO_file_jumps);
103 //      if (fp->_vtable_offset == 0 && fp->_wide_data != NULL)
104 //	fp->_wide_data->_wide_vtable = &INTUSE(_IO_wfile_jumps);
105       result = INTUSE(_IO_file_fopen) (fp, filename, mode, 1);
106       if (result != NULL)
107 	result = __fopen_maybe_mmap (result);
108     }
109   if (result != NULL)
110     /* unbound stream orientation */
111     result->_mode = 0;
112   if (fd != -1)
113     {
114       __close (fd);
115       if (filename != NULL)
116 	free ((char *) filename);
117     }
118   _IO_funlockfile (fp);
119   _IO_cleanup_region_end (0);
120   return result;
121 }
122 #endif
123