Line | Count | Source (jump to first uncovered line) |
1 | | /* Open a stream to a file. |
2 | | Copyright (C) 2007-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | This file 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 |
12 | | GNU Lesser General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public License |
15 | | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
16 | | |
17 | | /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ |
18 | | |
19 | | /* If the user's config.h happens to include <stdio.h>, let it include only |
20 | | the system's <stdio.h> here, so that orig_fopen doesn't recurse to |
21 | | rpl_fopen. */ |
22 | | #define _GL_SKIP_GNULIB_STDIO_H |
23 | | #include <config.h> |
24 | | |
25 | | /* Get the original definition of fopen. It might be defined as a macro. */ |
26 | | #include <stdio.h> |
27 | | #undef _GL_SKIP_GNULIB_STDIO_H |
28 | | |
29 | | static FILE * |
30 | | orig_fopen (const char *filename, const char *mode) |
31 | 0 | { |
32 | 0 | return fopen (filename, mode); |
33 | 0 | } |
34 | | |
35 | | /* Specification. */ |
36 | | #ifdef __osf__ |
37 | | /* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates |
38 | | this include because of the preliminary #include <stdio.h> above. */ |
39 | | # include "stdio.h" |
40 | | #else |
41 | | # include <stdio.h> |
42 | | #endif |
43 | | |
44 | | #include <errno.h> |
45 | | #include <fcntl.h> |
46 | | #include <string.h> |
47 | | #include <unistd.h> |
48 | | #include <sys/types.h> |
49 | | #include <sys/stat.h> |
50 | | |
51 | | FILE * |
52 | | rpl_fopen (const char *filename, const char *mode) |
53 | 0 | { |
54 | 0 | int open_direction; |
55 | 0 | int open_flags; |
56 | 0 | #if GNULIB_FOPEN_GNU |
57 | 0 | bool open_flags_gnu; |
58 | 0 | # define BUF_SIZE 80 |
59 | 0 | char fdopen_mode_buf[BUF_SIZE + 1]; |
60 | 0 | #endif |
61 | |
|
62 | | #if defined _WIN32 && ! defined __CYGWIN__ |
63 | | if (strcmp (filename, "/dev/null") == 0) |
64 | | filename = "NUL"; |
65 | | #endif |
66 | | |
67 | | /* Parse the mode. */ |
68 | 0 | open_direction = 0; |
69 | 0 | open_flags = 0; |
70 | 0 | #if GNULIB_FOPEN_GNU |
71 | 0 | open_flags_gnu = false; |
72 | 0 | #endif |
73 | 0 | { |
74 | 0 | const char *p = mode; |
75 | 0 | #if GNULIB_FOPEN_GNU |
76 | 0 | char *q = fdopen_mode_buf; |
77 | 0 | #endif |
78 | |
|
79 | 0 | for (; *p != '\0'; p++) |
80 | 0 | { |
81 | 0 | switch (*p) |
82 | 0 | { |
83 | 0 | case 'r': |
84 | 0 | open_direction = O_RDONLY; |
85 | 0 | #if GNULIB_FOPEN_GNU |
86 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
87 | 0 | *q++ = *p; |
88 | 0 | #endif |
89 | 0 | continue; |
90 | 0 | case 'w': |
91 | 0 | open_direction = O_WRONLY; |
92 | 0 | open_flags |= O_CREAT | O_TRUNC; |
93 | 0 | #if GNULIB_FOPEN_GNU |
94 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
95 | 0 | *q++ = *p; |
96 | 0 | #endif |
97 | 0 | continue; |
98 | 0 | case 'a': |
99 | 0 | open_direction = O_WRONLY; |
100 | 0 | open_flags |= O_CREAT | O_APPEND; |
101 | 0 | #if GNULIB_FOPEN_GNU |
102 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
103 | 0 | *q++ = *p; |
104 | 0 | #endif |
105 | 0 | continue; |
106 | 0 | case 'b': |
107 | | /* While it is non-standard, O_BINARY is guaranteed by |
108 | | gnulib <fcntl.h>. We can also assume that orig_fopen |
109 | | supports the 'b' flag. */ |
110 | 0 | open_flags |= O_BINARY; |
111 | 0 | #if GNULIB_FOPEN_GNU |
112 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
113 | 0 | *q++ = *p; |
114 | 0 | #endif |
115 | 0 | continue; |
116 | 0 | case '+': |
117 | 0 | open_direction = O_RDWR; |
118 | 0 | #if GNULIB_FOPEN_GNU |
119 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
120 | 0 | *q++ = *p; |
121 | 0 | #endif |
122 | 0 | continue; |
123 | 0 | #if GNULIB_FOPEN_GNU |
124 | 0 | case 'x': |
125 | 0 | open_flags |= O_EXCL; |
126 | 0 | open_flags_gnu = true; |
127 | 0 | continue; |
128 | 0 | case 'e': |
129 | 0 | open_flags |= O_CLOEXEC; |
130 | 0 | open_flags_gnu = true; |
131 | 0 | continue; |
132 | 0 | #endif |
133 | 0 | default: |
134 | 0 | break; |
135 | 0 | } |
136 | 0 | #if GNULIB_FOPEN_GNU |
137 | | /* The rest of the mode string can be a platform-dependent extension. |
138 | | Copy it unmodified. */ |
139 | 0 | { |
140 | 0 | size_t len = strlen (p); |
141 | 0 | if (len > fdopen_mode_buf + BUF_SIZE - q) |
142 | 0 | len = fdopen_mode_buf + BUF_SIZE - q; |
143 | 0 | memcpy (q, p, len); |
144 | 0 | q += len; |
145 | 0 | } |
146 | 0 | #endif |
147 | 0 | break; |
148 | 0 | } |
149 | 0 | #if GNULIB_FOPEN_GNU |
150 | 0 | *q = '\0'; |
151 | 0 | #endif |
152 | 0 | } |
153 | | |
154 | | #if FOPEN_TRAILING_SLASH_BUG |
155 | | /* Fail if the mode requires write access and the filename ends in a slash, |
156 | | as POSIX says such a filename must name a directory |
157 | | <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>: |
158 | | "A pathname that contains at least one non-<slash> character and that |
159 | | ends with one or more trailing <slash> characters shall not be resolved |
160 | | successfully unless the last pathname component before the trailing |
161 | | <slash> characters names an existing directory" |
162 | | If the named file already exists as a directory, then if a mode that |
163 | | requires write access is specified, fopen() must fail because POSIX |
164 | | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html> |
165 | | says that it fails with errno = EISDIR in this case. |
166 | | If the named file does not exist or does not name a directory, then |
167 | | fopen() must fail since the file does not contain a '.' directory. */ |
168 | | { |
169 | | size_t len = strlen (filename); |
170 | | if (len > 0 && filename[len - 1] == '/') |
171 | | { |
172 | | int fd; |
173 | | struct stat statbuf; |
174 | | FILE *fp; |
175 | | |
176 | | if (open_direction != O_RDONLY) |
177 | | { |
178 | | errno = EISDIR; |
179 | | return NULL; |
180 | | } |
181 | | |
182 | | fd = open (filename, open_direction | open_flags, |
183 | | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
184 | | if (fd < 0) |
185 | | return NULL; |
186 | | |
187 | | if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) |
188 | | { |
189 | | close (fd); |
190 | | errno = ENOTDIR; |
191 | | return NULL; |
192 | | } |
193 | | |
194 | | # if GNULIB_FOPEN_GNU |
195 | | fp = fdopen (fd, fdopen_mode_buf); |
196 | | # else |
197 | | fp = fdopen (fd, mode); |
198 | | # endif |
199 | | if (fp == NULL) |
200 | | { |
201 | | int saved_errno = errno; |
202 | | close (fd); |
203 | | errno = saved_errno; |
204 | | } |
205 | | return fp; |
206 | | } |
207 | | } |
208 | | #endif |
209 | | |
210 | 0 | #if GNULIB_FOPEN_GNU |
211 | 0 | if (open_flags_gnu) |
212 | 0 | { |
213 | 0 | int fd; |
214 | 0 | FILE *fp; |
215 | |
|
216 | 0 | fd = open (filename, open_direction | open_flags, |
217 | 0 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
218 | 0 | if (fd < 0) |
219 | 0 | return NULL; |
220 | | |
221 | 0 | fp = fdopen (fd, fdopen_mode_buf); |
222 | 0 | if (fp == NULL) |
223 | 0 | { |
224 | 0 | int saved_errno = errno; |
225 | 0 | close (fd); |
226 | 0 | errno = saved_errno; |
227 | 0 | } |
228 | 0 | return fp; |
229 | 0 | } |
230 | 0 | #endif |
231 | | |
232 | | /* open_direction is sometimes used, sometimes unused. |
233 | | Silence gcc's warning about this situation. */ |
234 | 0 | (void) open_direction; |
235 | |
|
236 | 0 | return orig_fopen (filename, mode); |
237 | 0 | } |