; Improve documentation of a recent change
[emacs.git] / lib / open.c
blobe690c9ea7798c4fcc24acf27fae1525b4f6f0689
1 /* Open a descriptor to a file.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 /* If the user's config.h happens to include <fcntl.h>, let it include only
20 the system's <fcntl.h> here, so that orig_open doesn't recurse to
21 rpl_open. */
22 #define __need_system_fcntl_h
23 #include <config.h>
25 /* Get the original definition of open. It might be defined as a macro. */
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #undef __need_system_fcntl_h
30 static int
31 orig_open (const char *filename, int flags, mode_t mode)
33 #if defined _WIN32 && !defined __CYGWIN__
34 return _open (filename, flags, mode);
35 #else
36 return open (filename, flags, mode);
37 #endif
40 /* Specification. */
41 #ifdef __osf__
42 /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
43 this include because of the preliminary #include <fcntl.h> above. */
44 # include "fcntl.h"
45 #else
46 # include <fcntl.h>
47 #endif
49 #include "cloexec.h"
51 #include <errno.h>
52 #include <stdarg.h>
53 #include <string.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
58 #ifndef REPLACE_OPEN_DIRECTORY
59 # define REPLACE_OPEN_DIRECTORY 0
60 #endif
62 int
63 open (const char *filename, int flags, ...)
65 /* 0 = unknown, 1 = yes, -1 = no. */
66 #if GNULIB_defined_O_CLOEXEC
67 int have_cloexec = -1;
68 #else
69 static int have_cloexec;
70 #endif
72 mode_t mode;
73 int fd;
75 mode = 0;
76 if (flags & O_CREAT)
78 va_list arg;
79 va_start (arg, flags);
81 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
82 creates crashing code when 'mode_t' is smaller than 'int'. */
83 mode = va_arg (arg, PROMOTED_MODE_T);
85 va_end (arg);
88 #if GNULIB_defined_O_NONBLOCK
89 /* The only known platform that lacks O_NONBLOCK is mingw, but it
90 also lacks named pipes and Unix sockets, which are the only two
91 file types that require non-blocking handling in open().
92 Therefore, it is safe to ignore O_NONBLOCK here. It is handy
93 that mingw also lacks openat(), so that is also covered here. */
94 flags &= ~O_NONBLOCK;
95 #endif
97 #if defined _WIN32 && ! defined __CYGWIN__
98 if (strcmp (filename, "/dev/null") == 0)
99 filename = "NUL";
100 #endif
102 #if OPEN_TRAILING_SLASH_BUG
103 /* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename
104 ends in a slash, as POSIX says such a filename must name a directory
105 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
106 "A pathname that contains at least one non-<slash> character and that
107 ends with one or more trailing <slash> characters shall not be resolved
108 successfully unless the last pathname component before the trailing
109 <slash> characters names an existing directory"
110 If the named file already exists as a directory, then
111 - if O_CREAT is specified, open() must fail because of the semantics
112 of O_CREAT,
113 - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
114 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html>
115 says that it fails with errno = EISDIR in this case.
116 If the named file does not exist or does not name a directory, then
117 - if O_CREAT is specified, open() must fail since open() cannot create
118 directories,
119 - if O_WRONLY or O_RDWR is specified, open() must fail because the
120 file does not contain a '.' directory. */
121 if ((flags & O_CREAT)
122 || (flags & O_ACCMODE) == O_RDWR
123 || (flags & O_ACCMODE) == O_WRONLY)
125 size_t len = strlen (filename);
126 if (len > 0 && filename[len - 1] == '/')
128 errno = EISDIR;
129 return -1;
132 #endif
134 fd = orig_open (filename,
135 flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
137 if (flags & O_CLOEXEC)
139 if (! have_cloexec)
141 if (0 <= fd)
142 have_cloexec = 1;
143 else if (errno == EINVAL)
145 fd = orig_open (filename, flags & ~O_CLOEXEC, mode);
146 have_cloexec = -1;
149 if (have_cloexec < 0 && 0 <= fd)
150 set_cloexec_flag (fd, true);
154 #if REPLACE_FCHDIR
155 /* Implementing fchdir and fdopendir requires the ability to open a
156 directory file descriptor. If open doesn't support that (as on
157 mingw), we use a dummy file that behaves the same as directories
158 on Linux (ie. always reports EOF on attempts to read()), and
159 override fstat() in fchdir.c to hide the fact that we have a
160 dummy. */
161 if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
162 && ((flags & O_ACCMODE) == O_RDONLY
163 || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
165 struct stat statbuf;
166 if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
168 /* Maximum recursion depth of 1. */
169 fd = open ("/dev/null", flags, mode);
170 if (0 <= fd)
171 fd = _gl_register_fd (fd, filename);
173 else
174 errno = EACCES;
176 #endif
178 #if OPEN_TRAILING_SLASH_BUG
179 /* If the filename ends in a slash and fd does not refer to a directory,
180 then fail.
181 Rationale: POSIX says such a filename must name a directory
182 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
183 "A pathname that contains at least one non-<slash> character and that
184 ends with one or more trailing <slash> characters shall not be resolved
185 successfully unless the last pathname component before the trailing
186 <slash> characters names an existing directory"
187 If the named file without the slash is not a directory, open() must fail
188 with ENOTDIR. */
189 if (fd >= 0)
191 /* We know len is positive, since open did not fail with ENOENT. */
192 size_t len = strlen (filename);
193 if (filename[len - 1] == '/')
195 struct stat statbuf;
197 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
199 close (fd);
200 errno = ENOTDIR;
201 return -1;
205 #endif
207 #if REPLACE_FCHDIR
208 if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
209 fd = _gl_register_fd (fd, filename);
210 #endif
212 return fd;