Back to index

glibc  2.9
iofopen.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1997,1998,1999,2000,2002,2003
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 #ifdef __STDC__
00031 #include <stdlib.h>
00032 #include <stddef.h>
00033 #endif
00034 #ifdef _LIBC
00035 # include <shlib-compat.h>
00036 #else
00037 # define _IO_new_fopen fopen
00038 #endif
00039 
00040 _IO_FILE *
00041 __fopen_maybe_mmap (fp)
00042      _IO_FILE *fp;
00043 {
00044 #ifdef _G_HAVE_MMAP
00045   if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES))
00046     {
00047       /* Since this is read-only, we might be able to mmap the contents
00048         directly.  We delay the decision until the first read attempt by
00049         giving it a jump table containing functions that choose mmap or
00050         vanilla file operations and reset the jump table accordingly.  */
00051 
00052       if (fp->_mode <= 0)
00053        _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps_maybe_mmap;
00054       else
00055        _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_wfile_jumps_maybe_mmap;
00056       fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap;
00057     }
00058 #endif
00059   return fp;
00060 }
00061 
00062 
00063 _IO_FILE *
00064 __fopen_internal (filename, mode, is32)
00065      const char *filename;
00066      const char *mode;
00067      int is32;
00068 {
00069   struct locked_FILE
00070   {
00071     struct _IO_FILE_plus fp;
00072 #ifdef _IO_MTSAFE_IO
00073     _IO_lock_t lock;
00074 #endif
00075     struct _IO_wide_data wd;
00076   } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
00077 
00078   if (new_f == NULL)
00079     return NULL;
00080 #ifdef _IO_MTSAFE_IO
00081   new_f->fp.file._lock = &new_f->lock;
00082 #endif
00083 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
00084   _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
00085 #else
00086   _IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL);
00087 #endif
00088   _IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
00089   INTUSE(_IO_file_init) (&new_f->fp);
00090 #if  !_IO_UNIFIED_JUMPTABLES
00091   new_f->fp.vtable = NULL;
00092 #endif
00093   if (INTUSE(_IO_file_fopen) ((_IO_FILE *) new_f, filename, mode, is32)
00094       != NULL)
00095     return __fopen_maybe_mmap (&new_f->fp.file);
00096 
00097   INTUSE(_IO_un_link) (&new_f->fp);
00098   free (new_f);
00099   return NULL;
00100 }
00101 
00102 _IO_FILE *
00103 _IO_new_fopen (filename, mode)
00104      const char *filename;
00105      const char *mode;
00106 {
00107   return __fopen_internal (filename, mode, 1);
00108 }
00109 
00110 #ifdef _LIBC
00111 strong_alias (_IO_new_fopen, __new_fopen)
00112 versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
00113 versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
00114 #endif