Line | Count | Source |
1 | | /* Open a stream to a file. |
2 | | Copyright (C) 2007-2026 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 | 38.4k | { |
32 | 38.4k | return fopen (filename, mode); |
33 | 38.4k | } |
34 | | |
35 | | /* Specification. */ |
36 | | #include <stdio.h> |
37 | | |
38 | | #include <errno.h> |
39 | | #include <fcntl.h> |
40 | | #include <string.h> |
41 | | #include <unistd.h> |
42 | | #include <sys/types.h> |
43 | | #include <sys/stat.h> |
44 | | |
45 | | FILE * |
46 | | rpl_fopen (const char *filename, const char *mode) |
47 | 38.4k | { |
48 | | #if defined _WIN32 && ! defined __CYGWIN__ |
49 | | if (streq (filename, "/dev/null")) |
50 | | filename = "NUL"; |
51 | | #endif |
52 | | |
53 | | /* Parse the mode. */ |
54 | 38.4k | int open_direction = 0; |
55 | 38.4k | int open_flags = 0; |
56 | 38.4k | #if GNULIB_FOPEN_GNU |
57 | 38.4k | bool open_flags_gnu = false; |
58 | 38.4k | # define BUF_SIZE 80 |
59 | 38.4k | char fdopen_mode_buf[BUF_SIZE + 1]; |
60 | 38.4k | #endif |
61 | 38.4k | { |
62 | 38.4k | const char *p = mode; |
63 | 38.4k | #if GNULIB_FOPEN_GNU |
64 | 38.4k | char *q = fdopen_mode_buf; |
65 | 38.4k | #endif |
66 | | |
67 | 76.9k | for (; *p != '\0'; p++) |
68 | 38.4k | { |
69 | 38.4k | switch (*p) |
70 | 38.4k | { |
71 | 38.4k | case 'r': |
72 | 38.4k | open_direction = O_RDONLY; |
73 | 38.4k | #if GNULIB_FOPEN_GNU |
74 | 38.4k | if (q < fdopen_mode_buf + BUF_SIZE) |
75 | 38.4k | *q++ = *p; |
76 | 38.4k | #endif |
77 | 38.4k | continue; |
78 | 0 | case 'w': |
79 | 0 | open_direction = O_WRONLY; |
80 | 0 | open_flags |= O_CREAT | O_TRUNC; |
81 | 0 | #if GNULIB_FOPEN_GNU |
82 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
83 | 0 | *q++ = *p; |
84 | 0 | #endif |
85 | 0 | continue; |
86 | 0 | case 'a': |
87 | 0 | open_direction = O_WRONLY; |
88 | 0 | open_flags |= O_CREAT | O_APPEND; |
89 | 0 | #if GNULIB_FOPEN_GNU |
90 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
91 | 0 | *q++ = *p; |
92 | 0 | #endif |
93 | 0 | continue; |
94 | 0 | case 'b': |
95 | | /* While it is non-standard, O_BINARY is guaranteed by |
96 | | gnulib <fcntl.h>. We can also assume that orig_fopen |
97 | | supports the 'b' flag. */ |
98 | 0 | open_flags |= O_BINARY; |
99 | 0 | #if GNULIB_FOPEN_GNU |
100 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
101 | 0 | *q++ = *p; |
102 | 0 | #endif |
103 | 0 | continue; |
104 | 0 | case '+': |
105 | 0 | open_direction = O_RDWR; |
106 | 0 | #if GNULIB_FOPEN_GNU |
107 | 0 | if (q < fdopen_mode_buf + BUF_SIZE) |
108 | 0 | *q++ = *p; |
109 | 0 | #endif |
110 | 0 | continue; |
111 | 0 | #if GNULIB_FOPEN_GNU |
112 | 0 | case 'x': |
113 | 0 | open_flags |= O_EXCL; |
114 | 0 | open_flags_gnu = true; |
115 | 0 | continue; |
116 | 0 | case 'e': |
117 | 0 | open_flags |= O_CLOEXEC; |
118 | 0 | open_flags_gnu = true; |
119 | 0 | continue; |
120 | 0 | #endif |
121 | 0 | default: |
122 | 0 | break; |
123 | 38.4k | } |
124 | 0 | #if GNULIB_FOPEN_GNU |
125 | | /* The rest of the mode string can be a platform-dependent extension. |
126 | | Copy it unmodified. */ |
127 | 0 | { |
128 | 0 | size_t len = strlen (p); |
129 | 0 | if (len > fdopen_mode_buf + BUF_SIZE - q) |
130 | 0 | len = fdopen_mode_buf + BUF_SIZE - q; |
131 | 0 | memcpy (q, p, len); |
132 | 0 | q += len; |
133 | 0 | } |
134 | 0 | #endif |
135 | 0 | break; |
136 | 38.4k | } |
137 | 38.4k | #if GNULIB_FOPEN_GNU |
138 | 38.4k | *q = '\0'; |
139 | 38.4k | #endif |
140 | 38.4k | } |
141 | | |
142 | | #if FOPEN_TRAILING_SLASH_BUG |
143 | | /* Fail if the mode requires write access and the filename ends in a slash, |
144 | | as POSIX says such a filename must name a directory |
145 | | <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>: |
146 | | "A pathname that contains at least one non-<slash> character and that |
147 | | ends with one or more trailing <slash> characters shall not be resolved |
148 | | successfully unless the last pathname component before the trailing |
149 | | <slash> characters names an existing directory" |
150 | | If the named file already exists as a directory, then if a mode that |
151 | | requires write access is specified, fopen() must fail because POSIX |
152 | | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html> |
153 | | says that it fails with errno = EISDIR in this case. |
154 | | If the named file does not exist or does not name a directory, then |
155 | | fopen() must fail since the file does not contain a '.' directory. */ |
156 | | { |
157 | | size_t len = strlen (filename); |
158 | | if (len > 0 && filename[len - 1] == '/') |
159 | | { |
160 | | if (open_direction != O_RDONLY) |
161 | | { |
162 | | errno = EISDIR; |
163 | | return NULL; |
164 | | } |
165 | | |
166 | | int fd = open (filename, open_direction | open_flags, |
167 | | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
168 | | if (fd < 0) |
169 | | return NULL; |
170 | | |
171 | | struct stat statbuf; |
172 | | if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) |
173 | | { |
174 | | close (fd); |
175 | | errno = ENOTDIR; |
176 | | return NULL; |
177 | | } |
178 | | |
179 | | FILE *fp; |
180 | | # if GNULIB_FOPEN_GNU |
181 | | fp = fdopen (fd, fdopen_mode_buf); |
182 | | # else |
183 | | fp = fdopen (fd, mode); |
184 | | # endif |
185 | | if (fp == NULL) |
186 | | { |
187 | | int saved_errno = errno; |
188 | | close (fd); |
189 | | errno = saved_errno; |
190 | | } |
191 | | return fp; |
192 | | } |
193 | | } |
194 | | #endif |
195 | | |
196 | 0 | #if GNULIB_FOPEN_GNU |
197 | 38.4k | if (open_flags_gnu) |
198 | 0 | { |
199 | 0 | int fd = open (filename, open_direction | open_flags, |
200 | 0 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
201 | 0 | if (fd < 0) |
202 | 0 | return NULL; |
203 | | |
204 | 0 | FILE *fp = fdopen (fd, fdopen_mode_buf); |
205 | 0 | if (fp == NULL) |
206 | 0 | { |
207 | 0 | int saved_errno = errno; |
208 | 0 | close (fd); |
209 | 0 | errno = saved_errno; |
210 | 0 | } |
211 | 0 | return fp; |
212 | 0 | } |
213 | 38.4k | #endif |
214 | | |
215 | | /* open_direction is sometimes used, sometimes unused. |
216 | | Silence gcc's warning about this situation. */ |
217 | 38.4k | (void) open_direction; |
218 | | |
219 | 38.4k | return orig_fopen (filename, mode); |
220 | 38.4k | } |