Avoid dup3 PLT usage.
[platform/upstream/glibc.git] / libio / iofopen.c
1 /* Copyright (C) 1993,1997,1998,1999,2000,2002,2003,2012
2         Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.
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 <stdlib.h>
30 #include <stddef.h>
31 #ifdef _LIBC
32 # include <shlib-compat.h>
33 #else
34 # define _IO_new_fopen fopen
35 #endif
36
37 _IO_FILE *
38 __fopen_maybe_mmap (fp)
39      _IO_FILE *fp;
40 {
41 #ifdef _G_HAVE_MMAP
42   if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES))
43     {
44       /* Since this is read-only, we might be able to mmap the contents
45          directly.  We delay the decision until the first read attempt by
46          giving it a jump table containing functions that choose mmap or
47          vanilla file operations and reset the jump table accordingly.  */
48
49       if (fp->_mode <= 0)
50         _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps_maybe_mmap;
51       else
52         _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_wfile_jumps_maybe_mmap;
53       fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap;
54     }
55 #endif
56   return fp;
57 }
58
59
60 _IO_FILE *
61 __fopen_internal (filename, mode, is32)
62      const char *filename;
63      const char *mode;
64      int is32;
65 {
66   struct locked_FILE
67   {
68     struct _IO_FILE_plus fp;
69 #ifdef _IO_MTSAFE_IO
70     _IO_lock_t lock;
71 #endif
72     struct _IO_wide_data wd;
73   } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
74
75   if (new_f == NULL)
76     return NULL;
77 #ifdef _IO_MTSAFE_IO
78   new_f->fp.file._lock = &new_f->lock;
79 #endif
80 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
81   _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
82 #else
83   _IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL);
84 #endif
85   _IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
86   INTUSE(_IO_file_init) (&new_f->fp);
87 #if  !_IO_UNIFIED_JUMPTABLES
88   new_f->fp.vtable = NULL;
89 #endif
90   if (INTUSE(_IO_file_fopen) ((_IO_FILE *) new_f, filename, mode, is32)
91       != NULL)
92     return __fopen_maybe_mmap (&new_f->fp.file);
93
94   INTUSE(_IO_un_link) (&new_f->fp);
95   free (new_f);
96   return NULL;
97 }
98
99 _IO_FILE *
100 _IO_new_fopen (filename, mode)
101      const char *filename;
102      const char *mode;
103 {
104   return __fopen_internal (filename, mode, 1);
105 }
106
107 #ifdef _LIBC
108 strong_alias (_IO_new_fopen, __new_fopen)
109 versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
110 versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
111 #endif