e3b98381955976fc65365072f82baa4e2244905f
[platform/upstream/glibc.git] / libio / iofopen.c
1 /* Copyright (C) 1993,1997,1998,1999,2000,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 #ifdef __STDC__
30 #include <stdlib.h>
31 #include <stddef.h>
32 #endif
33 #ifdef _LIBC
34 # include <shlib-compat.h>
35 #else
36 # define _IO_new_fopen fopen
37 #endif
38
39 _IO_FILE *
40 __fopen_maybe_mmap (fp)
41      _IO_FILE *fp;
42 {
43 #ifdef _G_HAVE_MMAP
44   if (fp->_flags & _IO_NO_WRITES)
45     {
46       /* We use the file in read-only mode.  This could mean we can
47          mmap the file and use it without any copying.  But not all
48          file descriptors are for mmap-able objects and on 32-bit
49          machines we don't want to map files which are too large since
50          this would require too much virtual memory.  */
51       struct _G_stat64 st;
52
53       if (_IO_SYSSTAT (fp, &st) == 0
54           && S_ISREG (st.st_mode) && st.st_size != 0
55           /* Limit the file size to 1MB for 32-bit machines.  */
56           && (sizeof (ptrdiff_t) > 4 || st.st_size < 1*1024*1024))
57         {
58           /* Try to map the file.  */
59           void *p;
60
61 # ifdef _G_MMAP64
62           p = _G_MMAP64 (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
63                          fp->_fileno, 0);
64 # else
65           p = __mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
66                       fp->_fileno, 0);
67 # endif
68           if (p != MAP_FAILED)
69             {
70               /* OK, we managed to map the file.  Set the buffer up
71                  and use a special jump table with simplified
72                  underflow functions which never tries to read
73                  anything from the file.  */
74               _IO_setb (fp, p, (char *) p + st.st_size, 0);
75               _IO_setg (fp, p, p, p);
76
77               if (fp->_mode <= 0)
78                 _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps_mmap;
79               else
80                 _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_wfile_jumps_mmap;
81               fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_mmap;
82
83               fp->_offset = 0;
84             }
85         }
86     }
87 #endif
88   return fp;
89 }
90
91
92 _IO_FILE *
93 __fopen_internal (filename, mode, is32)
94      const char *filename;
95      const char *mode;
96      int is32;
97 {
98   struct locked_FILE
99   {
100     struct _IO_FILE_plus fp;
101 #ifdef _IO_MTSAFE_IO
102     _IO_lock_t lock;
103 #endif
104     struct _IO_wide_data wd;
105   } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
106
107   if (new_f == NULL)
108     return NULL;
109 #ifdef _IO_MTSAFE_IO
110   new_f->fp.file._lock = &new_f->lock;
111 #endif
112 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
113   _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
114 #else
115   _IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL);
116 #endif
117   _IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
118   _IO_file_init (&new_f->fp);
119 #if  !_IO_UNIFIED_JUMPTABLES
120   new_f->fp.vtable = NULL;
121 #endif
122   if (_IO_file_fopen ((_IO_FILE *) new_f, filename, mode, is32) != NULL)
123     return __fopen_maybe_mmap (&new_f->fp.file);
124
125   _IO_un_link (&new_f->fp);
126   free (new_f);
127   return NULL;
128 }
129
130 _IO_FILE *
131 _IO_new_fopen (filename, mode)
132      const char *filename;
133      const char *mode;
134 {
135   return __fopen_internal (filename, mode, 1);
136 }
137
138 #ifdef _LIBC
139 strong_alias (_IO_new_fopen, __new_fopen)
140 versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
141 versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
142 #endif