Debian Codegraph @

lib/fopen.c

raw
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 <[email protected]>, 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 {
32 return fopen (filename, mode);
33 }
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 {
48 #if defined _WIN32 && ! defined __CYGWIN__
49 if (streq (filename, "/dev/null"))
50 filename = "NUL";
51 #endif
52
53 /* Parse the mode. */
54 int open_direction = 0;
55 int open_flags = 0;
56 #if GNULIB_FOPEN_GNU
57 bool open_flags_gnu = false;
58 # define BUF_SIZE 80
59 char fdopen_mode_buf[BUF_SIZE + 1];
60 #endif
61 {
62 const char *p = mode;
63 #if GNULIB_FOPEN_GNU
64 char *q = fdopen_mode_buf;
65 #endif
66
67 for (; *p != '\0'; p++)
68 {
69 switch (*p)
70 {
71 case 'r':
72 open_direction = O_RDONLY;
73 #if GNULIB_FOPEN_GNU
74 if (q < fdopen_mode_buf + BUF_SIZE)
75 *q++ = *p;
76 #endif
77 continue;
78 case 'w':
79 open_direction = O_WRONLY;
80 open_flags |= O_CREAT | O_TRUNC;
81 #if GNULIB_FOPEN_GNU
82 if (q < fdopen_mode_buf + BUF_SIZE)
83 *q++ = *p;
84 #endif
85 continue;
86 case 'a':
87 open_direction = O_WRONLY;
88 open_flags |= O_CREAT | O_APPEND;
89 #if GNULIB_FOPEN_GNU
90 if (q < fdopen_mode_buf + BUF_SIZE)
91 *q++ = *p;
92 #endif
93 continue;
94 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 open_flags |= O_BINARY;
99 #if GNULIB_FOPEN_GNU
100 if (q < fdopen_mode_buf + BUF_SIZE)
101 *q++ = *p;
102 #endif
103 continue;
104 case '+':
105 open_direction = O_RDWR;
106 #if GNULIB_FOPEN_GNU
107 if (q < fdopen_mode_buf + BUF_SIZE)
108 *q++ = *p;
109 #endif
110 continue;
111 #if GNULIB_FOPEN_GNU
112 case 'x':
113 open_flags |= O_EXCL;
114 open_flags_gnu = true;
115 continue;
116 case 'e':
117 open_flags |= O_CLOEXEC;
118 open_flags_gnu = true;
119 continue;
120 #endif
121 default:
122 break;
123 }
124 #if GNULIB_FOPEN_GNU
125 /* The rest of the mode string can be a platform-dependent extension.
126 Copy it unmodified. */
127 {
128 size_t len = strlen (p);
129 if (len > fdopen_mode_buf + BUF_SIZE - q)
130 len = fdopen_mode_buf + BUF_SIZE - q;
131 memcpy (q, p, len);
132 q += len;
133 }
134 #endif
135 break;
136 }
137 #if GNULIB_FOPEN_GNU
138 *q = '\0';
139 #endif
140 }
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 #if GNULIB_FOPEN_GNU
197 if (open_flags_gnu)
198 {
199 int fd = open (filename, open_direction | open_flags,
200 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
201 if (fd < 0)
202 return NULL;
203
204 FILE *fp = fdopen (fd, fdopen_mode_buf);
205 if (fp == NULL)
206 {
207 int saved_errno = errno;
208 close (fd);
209 errno = saved_errno;
210 }
211 return fp;
212 }
213 #endif
214
215 /* open_direction is sometimes used, sometimes unused.
216 Silence gcc's warning about this situation. */
217 (void) open_direction;
218
219 return orig_fopen (filename, mode);
220 }