Node:Access Modes, Next:, Up:File Status Flags



File Access Modes

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.

int O_RDONLY Macro
Open the file for read access.

int O_WRONLY Macro
Open the file for write access.

int O_RDWR Macro
Open the file for both reading and writing.

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.

int O_READ Macro
Open the file for reading. Same as O_RDWR; only defined on GNU.

int O_WRITE Macro
Open the file for reading. Same as O_WRONLY; only defined on GNU.

int O_EXEC Macro
Open the file for executing. Only defined on GNU.

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.

int O_ACCMODE Macro
This macro stands for a mask that can be bitwise-ANDed with the file status flag value to produce a value representing the file access mode. The mode will be O_RDONLY, O_WRONLY, or O_RDWR. (In the GNU system it could also be zero, and it never includes the O_EXEC bit.)