The open-time flags specify options affecting how
open will behave.
These options are not preserved once the file is open. The exception to
O_NONBLOCK, which is also an I/O operating mode and so it
is saved. See section Opening and Closing Files, for how to call
There are two sorts of options specified by open-time flags.
openlooks up the file name to locate the file, and whether the file can be created.
openwill perform on the file once it is open.
Here are the file name translation flags.
O_EXCLare set, then
openfails if the specified file already exists. This is guaranteed to never clobber an existing file.
openfrom blocking for a "long time" to open the file. This is only meaningful for some kinds of files, usually devices such as serial ports; when it is not meaningful, it is harmless and ignored. Often opening a port to a modem blocks until the modem reports carrier detection; if
openwill return immediately without a carrier.
Note that the
O_NONBLOCK flag is overloaded as both an I/O operating
mode and a file name translation flag. This means that specifying
open also sets nonblocking I/O mode;
see section I/O Operating Modes. To open the file without blocking but do normal
I/O that blocks, you must call
O_NONBLOCK set and
fcntl to turn the bit off.
In the GNU system and 4.4 BSD, opening a file never makes it the
controlling terminal and
O_NOCTTY is zero. However, other
systems may use a nonzero value for
O_NOCTTY and set the
controlling terminal when you open a file that is a terminal device; so
to be portable, use
O_NOCTTY when it is important to avoid this.
The following three file name translation flags exist only in the GNU system.
fstaton the new file descriptor will return the information returned by
lstaton the link's name.)
The open-time action flags tell
open to do additional operations
which are not really related to opening the file. The reason to do them
as part of
open instead of in separate calls is that
can do them atomically.
O_TRUNC. In BSD and GNU you must have permission to write the file to truncate it, but you need not open for write access.
This is the only open-time action flag specified by POSIX.1. There is
no good reason for truncation to be done by
open, instead of by
ftruncate afterwards. The
O_TRUNC flag existed in
ftruncate was invented, and is retained for backward
flock. See section File Locks.
O_CREAT is specified, the locking is done atomically when
creating the file. You are guaranteed that no other process will get
the lock on the new file first.
flock. See section File Locks. This is atomic like
Go to the first, previous, next, last section, table of contents.