Back to index

glibc  2.9
freopen.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,95,96,97,98,2000,2001,2002,2003,2008
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.
00019 
00020    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 #include "libioP.h"
00030 #include "stdio.h"
00031 #include <stdlib.h>
00032 
00033 #include <shlib-compat.h>
00034 #include <fd_to_filename.h>
00035 
00036 FILE*
00037 freopen (filename, mode, fp)
00038      const char* filename;
00039      const char* mode;
00040      FILE* fp;
00041 {
00042   FILE *result;
00043   int fd = -1;
00044   CHECK_FILE (fp, NULL);
00045   if (!(fp->_flags & _IO_IS_FILEBUF))
00046     return NULL;
00047   _IO_acquire_lock (fp);
00048   if (filename == NULL && _IO_fileno (fp) >= 0)
00049     {
00050       fd = __dup (_IO_fileno (fp));
00051       if (fd != -1)
00052        filename = fd_to_filename (fd);
00053     }
00054 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
00055   if (&_IO_stdin_used == NULL)
00056     {
00057       /* If the shared C library is used by the application binary which
00058         was linked against the older version of libio, we just use the
00059         older one even for internal use to avoid trouble since a pointer
00060         to the old libio may be passed into shared C library and wind
00061         up here. */
00062       _IO_old_file_close_it (fp);
00063       _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_old_file_jumps;
00064       result = _IO_old_file_fopen (fp, filename, mode);
00065     }
00066   else
00067 #endif
00068     {
00069       INTUSE(_IO_file_close_it) (fp);
00070       _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps;
00071       if (_IO_vtable_offset (fp) == 0 && fp->_wide_data != NULL)
00072        fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
00073       result = INTUSE(_IO_file_fopen) (fp, filename, mode, 1);
00074       if (result != NULL)
00075        result = __fopen_maybe_mmap (result);
00076     }
00077   if (result != NULL)
00078     /* unbound stream orientation */
00079     result->_mode = 0;
00080   if (fd != -1)
00081     {
00082       __close (fd);
00083       free ((char *) filename);
00084     }
00085   _IO_release_lock (fp);
00086   return result;
00087 }