@node fcntl.h
@section @file{fcntl.h}

POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/fcntl.h.html}

Gnulib module: fcntl-h
@mindex fcntl-h

Portability problems fixed by Gnulib:
@itemize
@item
The type @code{pid_t} is not defined on some platforms:
MSVC 14.

@item
The type @code{mode_t} is not defined on some platforms:
MSVC 14.

@item
The type @code{off64_t} is not defined on some platforms:
macOS 14, FreeBSD 10.4, NetBSD 10.0, OpenBSD 7.9, MSVC 14, Cygwin, Haiku, Minix 3.3.

@item
@samp{O_CLOEXEC} is not defined on some platforms:
Mac OS X 10.6, FreeBSD 8.4, NetBSD 5.1, OpenBSD 4.9, Minix 3.1.8, AIX 7.1, HP-UX 11.31, Solaris 10, Cygwin 1.7.1, mingw, MSVC 14.

@item
@samp{O_DSYNC}, @samp{O_NOCTTY},
@samp{O_NOFOLLOW}, @samp{O_RSYNC}, @samp{O_SYNC},
and @samp{O_TTY_INIT} are not defined on some platforms.
When not otherwise defined, Gnulib defines these macros to 0,
which is generally safe.

@item
@samp{O_NONBLOCK} is not defined on some platforms.
@mindex nonblocking
If the @samp{nonblocking} module is in use, gnulib guarantees a working
non-zero value; otherwise, the gnulib replacement is 0.

@item
@samp{O_EXEC} is not defined on some platforms:
glibc 2.42, macOS 12, FreeBSD 7.4, NetBSD 9.4, OpenBSD 7.9, HP-UX 11, Solaris 10, Cygwin 1.5.x, mingw, MSVC 14, Android.
However, glibc does define @samp{O_EXEC} on GNU/Hurd.
When not otherwise defined,
Gnulib defines this macro to @samp{O_RDONLY}, which is typically 0.

Note: @samp{O_EXEC} is not suitable for opening a file descriptor
that you want to use with @code{fchmod} later, because
musl libc defines @samp{O_EXEC} to @code{O_PATH} and
@c https://lists.gnu.org/archive/html/bug-gnulib/2012-11/msg00029.html
Linux @code{fchmod} fails with error @code{EBADF} when given such a file
descriptor.

@item
@samp{O_SEARCH} is not defined on some platforms:
glibc 2.42, macOS 12, FreeBSD 12.4, NetBSD 6.1, OpenBSD 7.9, AIX 6.1, HP-UX 11, Solaris 10, Cygwin 1.5.x, mingw, MSVC 14, Android.
When not otherwise defined,
Gnulib defines this macro to @samp{O_RDONLY}, which is typically 0.

@item
@samp{O_SEARCH} is defined to @code{O_PATH} on some platforms:
@c https://bugs.gnu.org/75405
musl libc.

@item
@samp{O_ACCMODE} is not defined on some platforms:
MSVC 14.

@item
The @samp{O_ACCMODE} mask mistakenly omits @samp{O_SEARCH} and
@samp{O_EXEC} on some platforms:
Cygwin.

@item
@samp{O_BINARY}, @samp{O_TEXT} (not specified by POSIX, but essential for
portability to native Windows platforms) are defined on some platforms but
not on others.
Gnulib defines these macros to 0 on GNU and other platforms that do not
distinguish between text and binary I/O@.

@item
@samp{O_CLOEXEC}, @samp{O_NOFOLLOW}, and @samp{O_TTY_INIT}
are defined to values that are too large for an @code{int} on some platforms:
AIX 7.1 with XL C 12.1.

@item
@samp{O_DIRECT}, @samp{O_IGNORE_CTTY}, @samp{O_NDELAY},
@samp{O_NOATIME}, @samp{O_NOLINK}, @samp{O_NOLINKS}, and
@samp{O_NOTRANS} (not specified by POSIX) are defined
on some platforms but not on others.
When not otherwise defined, Gnulib defines these macros to 0,
which is generally safe.

@item
@samp{O_DIRECTORY} is not defined on some platforms:
glibc 2.0, Mac OS X 10.4, FreeBSD 7.4, NetBSD 4.0.1, OpenBSD 4.9, HP-UX 11, Solaris 10, Cygwin 1.5.x, mingw, MSVC 14.

@item
@samp{FD_CLOEXEC}, @samp{F_DUPFD}, and @samp{F_GETFD} are not defined
on some platforms:
mingw, MSVC 14.

@item
@samp{F_DUPFD_CLOEXEC} is not defined on some platforms:
Mac OS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 7.9, AIX 5.1, HP-UX 11, Solaris 11 2010-11, Cygwin 1.7.1, mingw, MSVC 14.

@item
@samp{AT_FDCWD}, @samp{AT_EACCESS}, @samp{AT_SYMLINK_NOFOLLOW},
@samp{AT_SYMLINK_FOLLOW}, and @samp{AT_REMOVEDIR}
are not defined on many platforms:
glibc 2.3.6, Mac OS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 7.9, AIX 5.1, HP-UX 11, Cygwin 1.5.x, mingw, MSVC 14.

@item
@samp{AT_FDCWD} is defined with a value too large for an @code{int} on some
platforms:
Solaris 11.3.
@end itemize

Portability problems not fixed by Gnulib:
@itemize

@item
On some platforms, @samp{O_NOATIME} and @samp{O_NOFOLLOW}
are defined but do not work.
To help work around the resulting portability problems,
the @code{fcntl-h} module defines @samp{HAVE_WORKING_O_NOATIME} to 1
on platforms where @samp{O_NOATIME} is known to work, and to 0 otherwise;
and similarly for @samp{HAVE_WORKING_O_NOFOLLOW} and @samp{O_NOFOLLOW}:
Mac OS X 10.3.

@item
@samp{O_PATH} and @samp{AT_EMPTY_PATH} are not defined on some platforms:
glibc 2.13, macOS 26, FreeBSD 13.0, NetBSD 10.1, OpenBSD 7.9, Minix 3.3.0,
AIX 7.3, HP-UX 11.31, Solaris 11.4, Cygwin 3.0, mingw, MSVC 14.

@item
Although POSIX.1-2024 standardized @code{O_CLOFORK},
@code{FD_CLOFORK} and @code{F_DUPFD_CLOFORK},
they are not defined on most platforms:
glibc 2.42, musl libc, macOS 15.5, FreeBSD 14.2, NetBSD 10.1, OpenBSD 7.7, Minix 3.3.0, AIX 7.3, Solaris 11.3, Cygwin 3.6.2, mingw, MSVC 14.

@item
@samp{F_SETFD}, @samp{F_GETFL}, @samp{F_SETFL}, @samp{F_GETLK},
@samp{F_SETLK}, @samp{F_SETLKW}, @samp{F_GETOWN}, and @samp{F_SETOWN}
are not defined on some platforms:
mingw, MSVC 14.

@item
@samp{POSIX_FADV_DONTNEED}, @samp{POSIX_FADV_NOREUSE},
@samp{POSIX_FADV_NORMAL}, @samp{POSIX_FADV_RANDOM},
@samp{POSIX_FADV_SEQUENTIAL}, and @samp{POSIX_FADV_WILLNEED} are not
defined on some platforms.

@item
POSIX allows @samp{AT_FDCWD} to be @minus{}1, so it is unsafe to use
@samp{AT_FDCWD} as an invalid directory file descriptor argument to
functions like @samp{openat}.
Instead, you can use @samp{AT_FDCWD == -1 ? -2 : -1}.
@samp{AT_FDCWD} is @minus{}1 on some platforms:
Haiku R1/Beta4.

@item
POSIX.1-2024 allows, but does not require, this file to make visible
all symbols from the headers @code{<sys/stat.h>} and @code{<unistd.h>}.
Programs should not assume that these other symbols are visible merely
because this file is included, or that they are invisible because only
this file is included.
@end itemize
