openat: don’t close (-1)
[gnulib.git] / lib / fcntl.in.h
blobfc65d40bc0633b24a05c68dbb820f890423b80fe
1 /* Like <fcntl.h>, but with non-working flags defined to 0.
3 Copyright (C) 2006-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* written by Paul Eggert */
20 #if __GNUC__ >= 3
21 @PRAGMA_SYSTEM_HEADER@
22 #endif
23 @PRAGMA_COLUMNS@
25 #if defined __need_system_fcntl_h
26 /* Special invocation convention. */
28 /* Needed before <sys/stat.h>.
29 May also define off_t to a 64-bit type on native Windows. */
30 #include <sys/types.h>
31 /* On some systems other than glibc, <sys/stat.h> is a prerequisite of
32 <fcntl.h>. On glibc systems, we would like to avoid namespace pollution.
33 But on glibc systems, <fcntl.h> includes <sys/stat.h> inside an
34 extern "C" { ... } block, which leads to errors in C++ mode with the
35 overridden <sys/stat.h> from gnulib. These errors are known to be gone
36 with g++ version >= 4.3. */
37 #if !(defined __GLIBC__ || defined __UCLIBC__) || (defined __cplusplus && defined GNULIB_NAMESPACE && (defined __ICC || !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))))
38 # include <sys/stat.h>
39 #endif
40 #@INCLUDE_NEXT@ @NEXT_FCNTL_H@
42 /* Native Windows platforms declare open(), creat() in <io.h>. */
43 #if (@GNULIB_CREAT@ || @GNULIB_OPEN@ || defined GNULIB_POSIXCHECK) \
44 && (defined _WIN32 && ! defined __CYGWIN__)
45 # include <io.h>
46 #endif
48 #else
49 /* Normal invocation convention. */
51 #ifndef _@GUARD_PREFIX@_FCNTL_H
53 /* Needed before <sys/stat.h>.
54 May also define off_t to a 64-bit type on native Windows.
55 Also defines off64_t on macOS, NetBSD, OpenBSD, MSVC, Cygwin, Haiku. */
56 #include <sys/types.h>
57 /* On some systems other than glibc, <sys/stat.h> is a prerequisite of
58 <fcntl.h>. On glibc systems, we would like to avoid namespace pollution.
59 But on glibc systems, <fcntl.h> includes <sys/stat.h> inside an
60 extern "C" { ... } block, which leads to errors in C++ mode with the
61 overridden <sys/stat.h> from gnulib. These errors are known to be gone
62 with g++ version >= 4.3. */
63 #if !(defined __GLIBC__ || defined __UCLIBC__) || (defined __cplusplus && defined GNULIB_NAMESPACE && (defined __ICC || !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))))
64 # include <sys/stat.h>
65 #endif
66 /* The include_next requires a split double-inclusion guard. */
67 #@INCLUDE_NEXT@ @NEXT_FCNTL_H@
69 /* Native Windows platforms declare open(), creat() in <io.h>. */
70 #if (@GNULIB_CREAT@ || @GNULIB_OPEN@ || defined GNULIB_POSIXCHECK) \
71 && (defined _WIN32 && ! defined __CYGWIN__)
72 # include <io.h>
73 #endif
75 #ifndef _@GUARD_PREFIX@_FCNTL_H
76 #define _@GUARD_PREFIX@_FCNTL_H
78 /* This file uses GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */
79 #if !_GL_CONFIG_H_INCLUDED
80 #error "Please include config.h first."
81 #endif
83 #ifndef __GLIBC__ /* Avoid namespace pollution on glibc systems. */
84 # include <unistd.h>
85 #endif
88 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
90 /* The definition of _GL_ARG_NONNULL is copied here. */
92 /* The definition of _GL_WARN_ON_USE is copied here. */
95 /* Declare overridden functions. */
97 #if @GNULIB_CREAT@
98 # if @REPLACE_CREAT@
99 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
100 # undef creat
101 # define creat rpl_creat
102 # endif
103 _GL_FUNCDECL_RPL (creat, int, (const char *filename, mode_t mode),
104 _GL_ARG_NONNULL ((1)));
105 _GL_CXXALIAS_RPL (creat, int, (const char *filename, mode_t mode));
106 # elif defined _WIN32 && !defined __CYGWIN__
107 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
108 # undef creat
109 # define creat _creat
110 # endif
111 _GL_CXXALIAS_MDA (creat, int, (const char *filename, mode_t mode));
112 # else
113 _GL_CXXALIAS_SYS (creat, int, (const char *filename, mode_t mode));
114 # endif
115 _GL_CXXALIASWARN (creat);
116 #elif defined GNULIB_POSIXCHECK
117 # undef creat
118 /* Assume creat is always declared. */
119 _GL_WARN_ON_USE (creat, "creat is not always POSIX compliant - "
120 "use gnulib module creat for portability");
121 #elif @GNULIB_MDA_CREAT@
122 /* On native Windows, map 'creat' to '_creat', so that -loldnames is not
123 required. In C++ with GNULIB_NAMESPACE, avoid differences between
124 platforms by defining GNULIB_NAMESPACE::creat always. */
125 # if defined _WIN32 && !defined __CYGWIN__
126 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
127 # undef creat
128 # define creat _creat
129 # endif
130 /* Need to cast, because in mingw the last argument is 'int mode'. */
131 _GL_CXXALIAS_MDA_CAST (creat, int, (const char *filename, mode_t mode));
132 # else
133 _GL_CXXALIAS_SYS (creat, int, (const char *filename, mode_t mode));
134 # endif
135 _GL_CXXALIASWARN (creat);
136 #endif
138 #if @GNULIB_FCNTL@
139 # if @REPLACE_FCNTL@
140 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
141 # undef fcntl
142 # define fcntl rpl_fcntl
143 # endif
144 _GL_FUNCDECL_RPL (fcntl, int, (int fd, int action, ...), );
145 _GL_CXXALIAS_RPL (fcntl, int, (int fd, int action, ...));
146 # if !GNULIB_defined_rpl_fcntl
147 # define GNULIB_defined_rpl_fcntl 1
148 # endif
149 # else
150 # if !@HAVE_FCNTL@
151 _GL_FUNCDECL_SYS (fcntl, int, (int fd, int action, ...), );
152 # if !GNULIB_defined_fcntl
153 # define GNULIB_defined_fcntl 1
154 # endif
155 # endif
156 _GL_CXXALIAS_SYS (fcntl, int, (int fd, int action, ...));
157 # endif
158 _GL_CXXALIASWARN (fcntl);
159 #elif defined GNULIB_POSIXCHECK
160 # undef fcntl
161 # if HAVE_RAW_DECL_FCNTL
162 _GL_WARN_ON_USE (fcntl, "fcntl is not always POSIX compliant - "
163 "use gnulib module fcntl for portability");
164 # endif
165 #endif
167 #if @GNULIB_OPEN@
168 # if @REPLACE_OPEN@
169 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
170 # undef open
171 # define open rpl_open
172 # endif
173 _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...),
174 _GL_ARG_NONNULL ((1)));
175 _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
176 # elif defined _WIN32 && !defined __CYGWIN__
177 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
178 # undef open
179 # define open _open
180 # endif
181 _GL_CXXALIAS_MDA (open, int, (const char *filename, int flags, ...));
182 # else
183 _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
184 # endif
185 /* On HP-UX 11, in C++ mode, open() is defined as an inline function with a
186 default argument. _GL_CXXALIASWARN does not work in this case. */
187 # if !defined __hpux
188 _GL_CXXALIASWARN (open);
189 # endif
190 #elif defined GNULIB_POSIXCHECK
191 # undef open
192 /* Assume open is always declared. */
193 _GL_WARN_ON_USE (open, "open is not always POSIX compliant - "
194 "use gnulib module open for portability");
195 #elif @GNULIB_MDA_OPEN@
196 /* On native Windows, map 'open' to '_open', so that -loldnames is not
197 required. In C++ with GNULIB_NAMESPACE, avoid differences between
198 platforms by defining GNULIB_NAMESPACE::open always. */
199 # if defined _WIN32 && !defined __CYGWIN__
200 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
201 # undef open
202 # define open _open
203 # endif
204 _GL_CXXALIAS_MDA (open, int, (const char *filename, int flags, ...));
205 # else
206 _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
207 # endif
208 # if !defined __hpux
209 _GL_CXXALIASWARN (open);
210 # endif
211 #endif
213 #if @GNULIB_OPENAT@
214 # if @REPLACE_OPENAT@
215 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
216 # undef openat
217 # define openat rpl_openat
218 # endif
219 _GL_FUNCDECL_RPL (openat, int,
220 (int fd, char const *file, int flags, /* mode_t mode */ ...),
221 _GL_ARG_NONNULL ((2)));
222 _GL_CXXALIAS_RPL (openat, int,
223 (int fd, char const *file, int flags, /* mode_t mode */ ...));
224 # else
225 # if !@HAVE_OPENAT@
226 _GL_FUNCDECL_SYS (openat, int,
227 (int fd, char const *file, int flags, /* mode_t mode */ ...),
228 _GL_ARG_NONNULL ((2)));
229 # endif
230 _GL_CXXALIAS_SYS (openat, int,
231 (int fd, char const *file, int flags, /* mode_t mode */ ...));
232 # endif
233 _GL_CXXALIASWARN (openat);
234 #elif defined GNULIB_POSIXCHECK
235 # undef openat
236 # if HAVE_RAW_DECL_OPENAT
237 _GL_WARN_ON_USE (openat, "openat is not portable - "
238 "use gnulib module openat for portability");
239 # endif
240 #endif
243 /* Fix up the FD_* macros, only known to be missing on mingw. */
245 #ifndef FD_CLOEXEC
246 # define FD_CLOEXEC 1
247 #endif
249 /* Fix up the supported F_* macros. Intentionally leave other F_*
250 macros undefined. Only known to be missing on mingw. */
252 #ifndef F_DUPFD_CLOEXEC
253 # define F_DUPFD_CLOEXEC 0x40000000
254 /* Witness variable: 1 if gnulib defined F_DUPFD_CLOEXEC, 0 otherwise. */
255 # define GNULIB_defined_F_DUPFD_CLOEXEC 1
256 #else
257 # define GNULIB_defined_F_DUPFD_CLOEXEC 0
258 #endif
260 #ifndef F_DUPFD
261 # define F_DUPFD 1
262 #endif
264 #ifndef F_GETFD
265 # define F_GETFD 2
266 #endif
268 /* Fix up the O_* macros. */
270 /* AIX 7.1 with XL C 12.1 defines O_CLOEXEC, O_NOFOLLOW, and O_TTY_INIT
271 to values outside 'int' range, so omit these misdefinitions.
272 But avoid namespace pollution on non-AIX systems. */
273 #ifdef _AIX
274 # include <limits.h>
275 # if defined O_CLOEXEC && ! (INT_MIN <= O_CLOEXEC && O_CLOEXEC <= INT_MAX)
276 # undef O_CLOEXEC
277 # endif
278 # if defined O_NOFOLLOW && ! (INT_MIN <= O_NOFOLLOW && O_NOFOLLOW <= INT_MAX)
279 # undef O_NOFOLLOW
280 # endif
281 # if defined O_TTY_INIT && ! (INT_MIN <= O_TTY_INIT && O_TTY_INIT <= INT_MAX)
282 # undef O_TTY_INIT
283 # endif
284 #endif
286 #if !defined O_DIRECT && defined O_DIRECTIO
287 /* Tru64 spells it 'O_DIRECTIO'. */
288 # define O_DIRECT O_DIRECTIO
289 #endif
291 #if !defined O_CLOEXEC && defined O_NOINHERIT
292 /* Mingw spells it 'O_NOINHERIT'. */
293 # define O_CLOEXEC O_NOINHERIT
294 #endif
296 #ifndef O_CLOEXEC
297 # define O_CLOEXEC 0x40000000 /* Try to not collide with system O_* flags. */
298 # define GNULIB_defined_O_CLOEXEC 1
299 #else
300 # define GNULIB_defined_O_CLOEXEC 0
301 #endif
303 #ifndef O_DIRECT
304 # define O_DIRECT 0
305 #endif
307 #ifndef O_DIRECTORY
308 # define O_DIRECTORY 0
309 #endif
311 #ifndef O_DSYNC
312 # define O_DSYNC 0
313 #endif
315 #ifndef O_EXEC
316 # define O_EXEC O_RDONLY /* This is often close enough in older systems. */
317 #endif
319 #ifndef O_IGNORE_CTTY
320 # define O_IGNORE_CTTY 0
321 #endif
323 #ifndef O_NDELAY
324 # define O_NDELAY 0
325 #endif
327 #ifndef O_NOATIME
328 # define O_NOATIME 0
329 #endif
331 #ifndef O_NONBLOCK
332 # define O_NONBLOCK O_NDELAY
333 #endif
335 /* If the gnulib module 'nonblocking' is in use, guarantee a working non-zero
336 value of O_NONBLOCK. Otherwise, O_NONBLOCK is defined (above) to O_NDELAY
337 or to 0 as fallback. */
338 #if @GNULIB_NONBLOCKING@
339 # if O_NONBLOCK
340 # define GNULIB_defined_O_NONBLOCK 0
341 # else
342 # define GNULIB_defined_O_NONBLOCK 1
343 # undef O_NONBLOCK
344 # define O_NONBLOCK 0x40000000
345 # endif
346 #endif
348 #ifndef O_NOCTTY
349 # define O_NOCTTY 0
350 #endif
352 #ifndef O_NOFOLLOW
353 # define O_NOFOLLOW 0
354 #endif
356 #ifndef O_NOLINK
357 # define O_NOLINK 0
358 #endif
360 #ifndef O_NOLINKS
361 # define O_NOLINKS 0
362 #endif
364 #ifndef O_NOTRANS
365 # define O_NOTRANS 0
366 #endif
368 #ifndef O_RSYNC
369 # define O_RSYNC 0
370 #endif
372 #ifndef O_SEARCH
373 # define O_SEARCH O_RDONLY /* This is often close enough in older systems. */
374 #endif
376 #ifndef O_SYNC
377 # define O_SYNC 0
378 #endif
380 #ifndef O_TTY_INIT
381 # define O_TTY_INIT 0
382 #endif
384 #if ~O_ACCMODE & (O_RDONLY | O_WRONLY | O_RDWR | O_EXEC | O_SEARCH)
385 # undef O_ACCMODE
386 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR | O_EXEC | O_SEARCH)
387 #endif
389 /* For systems that distinguish between text and binary I/O.
390 O_BINARY is usually declared in fcntl.h */
391 #if !defined O_BINARY && defined _O_BINARY
392 /* For MSC-compatible compilers. */
393 # define O_BINARY _O_BINARY
394 # define O_TEXT _O_TEXT
395 #endif
397 #if defined __BEOS__ || defined __HAIKU__
398 /* BeOS 5 and Haiku have O_BINARY and O_TEXT, but they have no effect. */
399 # undef O_BINARY
400 # undef O_TEXT
401 #endif
403 #ifndef O_BINARY
404 # define O_BINARY 0
405 # define O_TEXT 0
406 #endif
408 /* Fix up the AT_* macros. */
410 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive. Its
411 value exceeds INT_MAX, so its use as an int doesn't conform to the
412 C standard, and GCC and Sun C complain in some cases. If the bug
413 is present, undef AT_FDCWD here, so it can be redefined below. */
414 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
415 # undef AT_FDCWD
416 #endif
418 /* Use the same bit pattern as Solaris 9, but with the proper
419 signedness. The bit pattern is important, in case this actually is
420 Solaris with the above workaround. */
421 #ifndef AT_FDCWD
422 # define AT_FDCWD (-3041965)
423 #endif
425 /* Use the same values as Solaris 9. This shouldn't matter, but
426 there's no real reason to differ. */
427 #ifndef AT_SYMLINK_NOFOLLOW
428 # define AT_SYMLINK_NOFOLLOW 4096
429 #endif
431 #ifndef AT_REMOVEDIR
432 # define AT_REMOVEDIR 1
433 #endif
435 /* Solaris 9 lacks these two, so just pick unique values. */
436 #ifndef AT_SYMLINK_FOLLOW
437 # define AT_SYMLINK_FOLLOW 2
438 #endif
440 #ifndef AT_EACCESS
441 # define AT_EACCESS 4
442 #endif
444 /* Ignore this flag if not supported. */
445 #ifndef AT_NO_AUTOMOUNT
446 # define AT_NO_AUTOMOUNT 0
447 #endif
449 #endif /* _@GUARD_PREFIX@_FCNTL_H */
450 #endif /* _@GUARD_PREFIX@_FCNTL_H */
451 #endif