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
22 #define __need_system_fcntl_h
25 /* Get the original definition of open. It might be defined as a macro. */
27 #include <sys/types.h>
28 #undef __need_system_fcntl_h
31 orig_open (const char *filename
, int flags
, mode_t mode
)
33 #if defined _WIN32 && !defined __CYGWIN__
34 return _open (filename
, flags
, mode
);
36 return open (filename
, flags
, mode
);
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. */
54 #include <sys/types.h>
58 #ifndef REPLACE_OPEN_DIRECTORY
59 # define REPLACE_OPEN_DIRECTORY 0
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;
69 static int have_cloexec
;
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
);
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. */
97 #if defined _WIN32 && ! defined __CYGWIN__
98 if (strcmp (filename
, "/dev/null") == 0)
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
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
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] == '/')
134 fd
= orig_open (filename
,
135 flags
& ~(have_cloexec
< 0 ? O_CLOEXEC
: 0), mode
);
137 if (flags
& O_CLOEXEC
)
143 else if (errno
== EINVAL
)
145 fd
= orig_open (filename
, flags
& ~O_CLOEXEC
, mode
);
149 if (have_cloexec
< 0 && 0 <= fd
)
150 set_cloexec_flag (fd
, true);
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
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
)))
166 if (stat (filename
, &statbuf
) == 0 && S_ISDIR (statbuf
.st_mode
))
168 /* Maximum recursion depth of 1. */
169 fd
= open ("/dev/null", flags
, mode
);
171 fd
= _gl_register_fd (fd
, filename
);
178 #if OPEN_TRAILING_SLASH_BUG
179 /* If the filename ends in a slash and fd does not refer to a directory,
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
191 /* We know len is positive, since open did not fail with ENOENT. */
192 size_t len
= strlen (filename
);
193 if (filename
[len
- 1] == '/')
197 if (fstat (fd
, &statbuf
) >= 0 && !S_ISDIR (statbuf
.st_mode
))
208 if (!REPLACE_OPEN_DIRECTORY
&& 0 <= fd
)
209 fd
= _gl_register_fd (fd
, filename
);