1 /* Open a stream to a file.
2 Copyright (C) 2007-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
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
22 #define _GL_ALREADY_INCLUDING_STDIO_H
25 /* Get the original definition of fopen. It might be defined as a macro. */
27 #undef _GL_ALREADY_INCLUDING_STDIO_H
30 orig_fopen (const char *filename, const char *mode)
32 return fopen (filename, mode);
36 /* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
37 this include because of the preliminary #include <stdio.h> above. */
45 #include <sys/types.h>
49 rpl_fopen (const char *filename, const char *mode)
56 char fdopen_mode_buf[BUF_SIZE + 1];
59 #if defined _WIN32 && ! defined __CYGWIN__
60 if (strcmp (filename, "/dev/null") == 0)
68 open_flags_gnu = false;
73 char *q = fdopen_mode_buf;
76 for (; *p != '\0'; p++)
81 open_direction = O_RDONLY;
83 if (q < fdopen_mode_buf + BUF_SIZE)
88 open_direction = O_WRONLY;
89 open_flags |= O_CREAT | O_TRUNC;
91 if (q < fdopen_mode_buf + BUF_SIZE)
96 open_direction = O_WRONLY;
97 open_flags |= O_CREAT | O_APPEND;
99 if (q < fdopen_mode_buf + BUF_SIZE)
104 /* While it is non-standard, O_BINARY is guaranteed by
105 gnulib <fcntl.h>. We can also assume that orig_fopen
106 supports the 'b' flag. */
107 open_flags |= O_BINARY;
109 if (q < fdopen_mode_buf + BUF_SIZE)
114 open_direction = O_RDWR;
116 if (q < fdopen_mode_buf + BUF_SIZE)
122 open_flags |= O_EXCL;
123 open_flags_gnu = true;
126 open_flags |= O_CLOEXEC;
127 open_flags_gnu = true;
134 /* The rest of the mode string can be a platform-dependent extension.
135 Copy it unmodified. */
137 size_t len = strlen (p);
138 if (len > fdopen_mode_buf + BUF_SIZE - q)
139 len = fdopen_mode_buf + BUF_SIZE - q;
151 #if FOPEN_TRAILING_SLASH_BUG
152 /* Fail if the mode requires write access and the filename ends in a slash,
153 as POSIX says such a filename must name a directory
154 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
155 "A pathname that contains at least one non-<slash> character and that
156 ends with one or more trailing <slash> characters shall not be resolved
157 successfully unless the last pathname component before the trailing
158 <slash> characters names an existing directory"
159 If the named file already exists as a directory, then if a mode that
160 requires write access is specified, fopen() must fail because POSIX
161 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html>
162 says that it fails with errno = EISDIR in this case.
163 If the named file does not exist or does not name a directory, then
164 fopen() must fail since the file does not contain a '.' directory. */
166 size_t len = strlen (filename);
167 if (len > 0 && filename[len - 1] == '/')
173 if (open_direction != O_RDONLY)
179 fd = open (filename, open_direction | open_flags,
180 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
184 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
191 # if GNULIB_FOPEN_GNU
192 fp = fdopen (fd, fdopen_mode_buf);
194 fp = fdopen (fd, mode);
198 int saved_errno = errno;
213 fd = open (filename, open_direction | open_flags,
214 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
218 fp = fdopen (fd, fdopen_mode_buf);
221 int saved_errno = errno;
229 return orig_fopen (filename, mode);