The file access modes allow a file descriptor to be used for reading, writing, or both. (In the GNU system, they can also allow none of these, and allow execution of the file as a program.) The access modes are chosen when the file is opened, and never change.
In the GNU system (and not in other systems), O_RDONLY
and
O_WRONLY
are independent bits that can be bitwise-ORed together,
and it is valid for either bit to be set or clear. This means that
O_RDWR
is the same as O_RDONLY|O_WRONLY
. A file access
mode of zero is permissible; it allows no operations that do input or
output to the file, but does allow other operations such as
fchmod
. On the GNU system, since “read-only” or “write-only”
is a misnomer, fcntl.h defines additional names for the file
access modes. These names are preferred when writing GNU-specific code.
But most programs will want to be portable to other POSIX.1 systems and
should use the POSIX.1 names above instead.
To determine the file access mode with fcntl
, you must extract
the access mode bits from the retrieved file status flags. In the GNU
system, you can just test the O_READ
and O_WRITE
bits in
the flags word. But in other POSIX.1 systems, reading and writing
access modes are not stored as distinct bit flags. The portable way to
extract the file access mode bits is with O_ACCMODE
.