blob: e7d84ae983e38201b4bb22930dfa52c7acea8298 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 1993-2015 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, see
16 <http://www.gnu.org/licenses/>.
17
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. */
26
27#include <stdlib.h>
28#include "libioP.h"
29#include <fcntl.h>
30
31#ifdef _LIBC
32# include <shlib-compat.h>
33#endif
34
35#ifndef _IO_fcntl
36#ifdef _LIBC
37#define _IO_fcntl __fcntl
38#else
39#define _IO_fcntl fcntl
40#endif
41#endif
42
43_IO_FILE *
44_IO_new_fdopen (fd, mode)
45 int fd;
46 const char *mode;
47{
48 int read_write;
49 struct locked_FILE
50 {
51 struct _IO_FILE_plus fp;
52#ifdef _IO_MTSAFE_IO
53 _IO_lock_t lock;
54#endif
55 struct _IO_wide_data wd;
56 } *new_f;
57 int i;
58 int use_mmap = 0;
59
60 /* Decide whether we modify the offset of the file we attach to and seek to
61 the end of file. We only do this if the mode is 'a' and if the file
62 descriptor did not have O_APPEND in its flags already. */
63 bool do_seek = false;
64
65 switch (*mode)
66 {
67 case 'r':
68 read_write = _IO_NO_WRITES;
69 break;
70 case 'w':
71 read_write = _IO_NO_READS;
72 break;
73 case 'a':
74 read_write = _IO_NO_READS|_IO_IS_APPENDING;
75 break;
76 default:
77 MAYBE_SET_EINVAL;
78 return NULL;
79 }
80 for (i = 1; i < 5; ++i)
81 {
82 switch (*++mode)
83 {
84 case '\0':
85 break;
86 case '+':
87 read_write &= _IO_IS_APPENDING;
88 break;
89 case 'm':
90 use_mmap = 1;
91 continue;
92 case 'x':
93 case 'b':
94 default:
95 /* Ignore */
96 continue;
97 }
98 break;
99 }
100#ifdef F_GETFL
101 int fd_flags = _IO_fcntl (fd, F_GETFL);
102#ifndef O_ACCMODE
103#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
104#endif
105 if (fd_flags == -1)
106 return NULL;
107
108 if (((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
109 || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
110 {
111 MAYBE_SET_EINVAL;
112 return NULL;
113 }
114
115 /* The May 93 draft of P1003.4/D14.1 (redesignated as 1003.1b)
116 [System Application Program Interface (API) Amendment 1:
117 Realtime Extensions], Rationale B.8.3.3
118 Open a Stream on a File Descriptor says:
119
120 Although not explicitly required by POSIX.1, a good
121 implementation of append ("a") mode would cause the
122 O_APPEND flag to be set.
123
124 (Historical implementations [such as Solaris2] do a one-time
125 seek in fdopen.)
126
127 However, we do not turn O_APPEND off if the mode is "w" (even
128 though that would seem consistent) because that would be more
129 likely to break historical programs.
130 */
131 if ((read_write & _IO_IS_APPENDING) && !(fd_flags & O_APPEND))
132 {
133 do_seek = true;
134#ifdef F_SETFL
135 if (_IO_fcntl (fd, F_SETFL, fd_flags | O_APPEND) == -1)
136#endif
137 return NULL;
138 }
139#endif
140
141 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
142 if (new_f == NULL)
143 return NULL;
144#ifdef _IO_MTSAFE_IO
145 new_f->fp.file._lock = &new_f->lock;
146#endif
147 _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd,
148#ifdef _G_HAVE_MMAP
149 (use_mmap && (read_write & _IO_NO_WRITES))
150 ? &_IO_wfile_jumps_maybe_mmap :
151#endif
152 &_IO_wfile_jumps);
153 _IO_JUMPS (&new_f->fp) =
154#ifdef _G_HAVE_MMAP
155 (use_mmap && (read_write & _IO_NO_WRITES)) ? &_IO_file_jumps_maybe_mmap :
156#endif
157 &_IO_file_jumps;
158 _IO_file_init (&new_f->fp);
159#if !_IO_UNIFIED_JUMPTABLES
160 new_f->fp.vtable = NULL;
161#endif
162 /* We only need to record the fd because _IO_file_init will have unset the
163 offset. It is important to unset the cached offset because the real
164 offset in the file could change between now and when the handle is
165 activated and we would then mislead ftell into believing that we have a
166 valid offset. */
167 new_f->fp.file._fileno = fd;
168 new_f->fp.file._flags &= ~_IO_DELETE_DONT_CLOSE;
169
170 _IO_mask_flags (&new_f->fp.file, read_write,
171 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
172
173 /* For append mode, set the file offset to the end of the file if we added
174 O_APPEND to the file descriptor flags. Don't update the offset cache
175 though, since the file handle is not active. */
176 if (do_seek && ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
177 == (_IO_IS_APPENDING | _IO_NO_READS)))
178 {
179 _IO_off64_t new_pos = _IO_SYSSEEK (&new_f->fp.file, 0, _IO_seek_end);
180 if (new_pos == _IO_pos_BAD && errno != ESPIPE)
181 return NULL;
182 }
183 return &new_f->fp.file;
184}
185libc_hidden_ver (_IO_new_fdopen, _IO_fdopen)
186
187strong_alias (_IO_new_fdopen, __new_fdopen)
188versioned_symbol (libc, _IO_new_fdopen, _IO_fdopen, GLIBC_2_1);
189versioned_symbol (libc, __new_fdopen, fdopen, GLIBC_2_1);