fcntl.h(7POSIX)
π fcntl.h β file control options π
okay bestie, welcome to the fcntl.h page from the POSIX Programmer's Manual (7POSIX, she's iconic). this is THE header for file control options and honestly she does so much, let's get into it π«Ά
β¨ PROLOG β¨
real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. just so you know π
πΈ NAME πΈ
fcntl.h β file control options. that's her. that's the whole vibe.
π¦ SYNOPSIS π¦
#include <fcntl.h>
β¨ DESCRIPTION β¨
okay so the <fcntl.h> header shall define these symbolic constants for the cmd argument used by fcntl(). the values shall be unique and shall be suitable for use in #if preprocessing directives. no cap.
F_DUPFDβ Duplicate file descriptor.F_DUPFD_CLOEXECβ Duplicate file descriptor with the close-on-exec flagFD_CLOEXECset.F_GETFDβ Get file descriptor flags.F_SETFDβ Set file descriptor flags.F_GETFLβ Get file status flags and file access modes.F_SETFLβ Set file status flags.F_GETLKβ Get record locking information.F_SETLKβ Set record locking information.F_SETLKWβ Set record locking information; wait if blocked.F_GETOWNβ Get process or process group ID to receiveSIGURGsignals.F_SETOWNβ Set process or process group ID to receiveSIGURGsignals.
the <fcntl.h> header shall define the following symbolic constant used for the fcntl() file descriptor flags, which shall be suitable for use in #if preprocessing directives:
FD_CLOEXECβ Close the file descriptor upon execution of anexecfamily function.
the <fcntl.h> header shall also define these symbolic constants for the l_type argument used for record locking with fcntl(). values shall be unique and suitable for use in #if preprocessing directives π
F_RDLCKβ Shared or read lock.F_UNLCKβ Unlock.F_WRLCKβ Exclusive or write lock.
the <fcntl.h> header shall define the values used for l_whence, SEEK_SET, SEEK_CUR, and SEEK_END as described in <stdio.h>.
now for the file creation flags π these get used in the oflag value to open() and openat(). the values shall be bitwise-distinct and shall be suitable for use in #if preprocessing directives:
O_CLOEXECβ TheFD_CLOEXECflag associated with the new descriptor shall be set to close the file descriptor upon execution of anexecfamily function.O_CREATβ Create file if it does not exist.O_DIRECTORYβ Fail if file is a non-directory file.O_EXCLβ Exclusive use flag.O_NOCTTYβ Do not assign controlling terminal.O_NOFOLLOWβ Do not follow symbolic links.O_TRUNCβ Truncate flag.O_TTY_INITβ Set thetermiosstructure terminal parameters to a state that provides conforming behavior; see Section 11.2, Parameters that Can be Set.
heads up bestie: the O_TTY_INIT flag can have the value zero, and in this case it need not be bitwise-distinct from the other flags. yes, that's a whole thing.
next up, the file status flags β¨ for use with open(), openat(), and fcntl(). the values shall be suitable for use in #if preprocessing directives:
O_APPENDβ Set append mode.O_DSYNCβ Write according to synchronized I/O data integrity completion.O_NONBLOCKβ Non-blocking mode.O_RSYNCβ Synchronized read I/O operations.O_SYNCβ Write according to synchronized I/O file integrity completion.
and the mask for file access modes (the value shall be suitable for use in #if preprocessing directives):
O_ACCMODEβ Mask for file access modes.
okay now the file access modes π for use with open(), openat(), and fcntl(). the values shall be unique β except that O_EXEC and O_SEARCH may have equal values. and they shall be suitable for use in #if preprocessing directives:
O_EXECβ Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory. π«£O_RDONLYβ Open for reading only.O_RDWRβ Open for reading and writing.O_SEARCHβ Open directory for search only. The result is unspecified if this flag is applied to a non-directory file. π«£O_WRONLYβ Open for writing only.
the <fcntl.h> header shall define the symbolic constants for file modes for use as values of mode_t as described in <sys/stat.h>.
the <fcntl.h> header shall define the following symbolic constant as a special value used in place of a file descriptor for the *at() functions which take a directory file descriptor as a parameter:
AT_FDCWDβ Use the current working directory to determine the target of relative file paths.
the <fcntl.h> header shall define the following symbolic constant as a value for the flag used by faccessat():
AT_EACCESSβ Check access using effective user and group ID.
the <fcntl.h> header shall define the following symbolic constant as a value for the flag used by fstatat(), fchmodat(), fchownat(), and utimensat():
AT_SYMLINK_NOFOLLOWβ Do not follow symbolic links.
the <fcntl.h> header shall define the following symbolic constant as a value for the flag used by linkat():
AT_SYMLINK_FOLLOWβ Follow symbolic link.
the <fcntl.h> header shall define the following symbolic constant as a value for the flag used by unlinkat():
AT_REMOVEDIRβ Remove directory instead of file.
now for the advice argument used by posix_fadvise() π
the header shall define these:
POSIX_FADV_DONTNEEDβ The application expects that it will not access the specified data in the near future.POSIX_FADV_NOREUSEβ The application expects to access the specified data once and then not reuse it thereafter.POSIX_FADV_NORMALβ The application has no advice to give on its behavior with respect to the specified data. It is the default characteristic if no advice is given for an open file.POSIX_FADV_RANDOMβ The application expects to access the specified data in a random order.POSIX_FADV_SEQUENTIALβ The application expects to access the specified data sequentially from lower offsets to higher offsets.POSIX_FADV_WILLNEEDβ The application expects to access the specified data in the near future.
the <fcntl.h> header shall define the flock structure describing a file lock π it shall include the following members:
short l_type Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.
short l_whence Flag for starting offset.
off_t l_start Relative offset in bytes.
off_t l_len Size; if 0 then until EOF.
pid_t l_pid Process ID of the process holding the lock; returned with F_GETLK.
the <fcntl.h> header shall define the mode_t, off_t, and pid_t types as described in <sys/types.h>.
the following shall be declared as functions and may also be defined as macros. function prototypes shall be provided, bestie:
int creat(const char *, mode_t);
int fcntl(int, int, ...);
int open(const char *, int, ...);
int openat(int, const char *, int, ...);
int posix_fadvise(int, off_t, off_t, int);
int posix_fallocate(int, off_t, off_t);
inclusion of the <fcntl.h> header may also make visible all symbols from <sys/stat.h> and <unistd.h>. so many friends showing up uninvited, love that for them π§
the following sections are informative. iykyk.
π APPLICATION USAGE π
Although no existing implementation defines AT_SYMLINK_FOLLOW and AT_SYMLINK_NOFOLLOW as the same numeric value, POSIX.1β2008 does not prohibit that, since the two constants are not used with the same interfaces. so like, technically allowed, but nobody's doing it. π
π RATIONALE π
While many of the symbolic constants introduced in the <fcntl.h> header do not strictly need to be used in #if preprocessor directives, widespread historic practice has defined them as macros that are usable in such constructs, and examination of existing applications has shown that they are occasionally used in such a way. Therefore it was decided to retain this requirement on an implementation in POSIX.1β2008. tradition, basically π
β FUTURE DIRECTIONS β
None. that's it. nothing. moving on π
π SEE ALSO π
<stdio.h><sys_stat.h><sys_types.h><unistd.h>
The System Interfaces volume of POSIX.1β2017: creat(), exec, fcntl(), futimens(), open(), posix_fadvise(), posix_fallocate(), posix_madvise()
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's the whole page bestie, you ate it π
β¨ this one came to you from IEEE/The Open Group, 2017, and yes, fcntl.h(7POSIX) is officially a whole queen. go forth and control your files π«Άπ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms