]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (C) 1993-2017 Free Software Foundation, Inc. |
41bdb6e2 | 2 | This file is part of the GNU C Library. |
40a55d20 | 3 | |
41bdb6e2 AJ |
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. | |
40a55d20 | 8 | |
41bdb6e2 AJ |
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 | |
40a55d20 | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
41bdb6e2 | 12 | Lesser General Public License for more details. |
40a55d20 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. | |
40a55d20 | 17 | |
41bdb6e2 AJ |
18 | As a special exception, if you link the code in this file with |
19 | files compiled with a GNU compiler to produce an executable, | |
20 | that does not cause the resulting executable to be covered by | |
21 | the GNU Lesser General Public License. This exception does not | |
22 | however invalidate any other reasons why the executable file | |
23 | might be covered by the GNU Lesser General Public License. | |
24 | This exception applies to code released by its copyright holders | |
25 | in files containing the exception. */ | |
96aa2d94 RM |
26 | |
27 | #include "libioP.h" | |
2747bf9a | 28 | #include <fcntl.h> |
96aa2d94 | 29 | #include <stdlib.h> |
0469311e | 30 | #include <stddef.h> |
319d719d UD |
31 | #ifdef _LIBC |
32 | # include <shlib-compat.h> | |
33 | #else | |
34 | # define _IO_new_fopen fopen | |
35 | #endif | |
96aa2d94 RM |
36 | |
37 | _IO_FILE * | |
24b97882 | 38 | __fopen_maybe_mmap (_IO_FILE *fp) |
0469311e UD |
39 | { |
40 | #ifdef _G_HAVE_MMAP | |
dd0ee2e1 | 41 | if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES)) |
0469311e | 42 | { |
e42637b6 RM |
43 | /* Since this is read-only, we might be able to mmap the contents |
44 | directly. We delay the decision until the first read attempt by | |
45 | giving it a jump table containing functions that choose mmap or | |
46 | vanilla file operations and reset the jump table accordingly. */ | |
0469311e | 47 | |
e42637b6 | 48 | if (fp->_mode <= 0) |
e69dcccb | 49 | _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps_maybe_mmap; |
e42637b6 | 50 | else |
e69dcccb | 51 | _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps_maybe_mmap; |
e42637b6 | 52 | fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap; |
0469311e UD |
53 | } |
54 | #endif | |
55 | return fp; | |
56 | } | |
57 | ||
58 | ||
59 | _IO_FILE * | |
24b97882 | 60 | __fopen_internal (const char *filename, const char *mode, int is32) |
96aa2d94 | 61 | { |
edf5b2d7 UD |
62 | struct locked_FILE |
63 | { | |
bd355af0 | 64 | struct _IO_FILE_plus fp; |
499e7464 | 65 | #ifdef _IO_MTSAFE_IO |
edf5b2d7 | 66 | _IO_lock_t lock; |
499e7464 | 67 | #endif |
d64b6ad0 | 68 | struct _IO_wide_data wd; |
edf5b2d7 UD |
69 | } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
70 | ||
71 | if (new_f == NULL) | |
96aa2d94 | 72 | return NULL; |
499e7464 | 73 | #ifdef _IO_MTSAFE_IO |
bd355af0 | 74 | new_f->fp.file._lock = &new_f->lock; |
499e7464 | 75 | #endif |
319d719d | 76 | #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T |
15a686af | 77 | _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps); |
319d719d UD |
78 | #else |
79 | _IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL); | |
80 | #endif | |
15a686af | 81 | _IO_JUMPS (&new_f->fp) = &_IO_file_jumps; |
db3476af | 82 | _IO_new_file_init_internal (&new_f->fp); |
96aa2d94 | 83 | #if !_IO_UNIFIED_JUMPTABLES |
bd355af0 | 84 | new_f->fp.vtable = NULL; |
96aa2d94 | 85 | #endif |
d18ea0c5 | 86 | if (_IO_file_fopen ((_IO_FILE *) new_f, filename, mode, is32) != NULL) |
0469311e UD |
87 | return __fopen_maybe_mmap (&new_f->fp.file); |
88 | ||
d18ea0c5 | 89 | _IO_un_link (&new_f->fp); |
edf5b2d7 | 90 | free (new_f); |
96aa2d94 RM |
91 | return NULL; |
92 | } | |
93 | ||
0469311e | 94 | _IO_FILE * |
24b97882 | 95 | _IO_new_fopen (const char *filename, const char *mode) |
0469311e UD |
96 | { |
97 | return __fopen_internal (filename, mode, 1); | |
98 | } | |
99 | ||
319d719d | 100 | #ifdef _LIBC |
1ea89a40 | 101 | strong_alias (_IO_new_fopen, __new_fopen) |
fd091d3f UD |
102 | versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1); |
103 | versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1); | |
2747bf9a RM |
104 | |
105 | # if !defined O_LARGEFILE || O_LARGEFILE == 0 | |
106 | weak_alias (_IO_new_fopen, _IO_fopen64) | |
107 | weak_alias (_IO_new_fopen, fopen64) | |
108 | # endif | |
319d719d | 109 | #endif |