open(2P) INTERACTIVE UNIX System (POSIX) open(2P)
NAME
open - open for reading or writing
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open (path, oflag [, mode] )
char *path;
int oflag, mode;
DESCRIPTION
The path variable points to a path name naming a file. The
open system call opens a file descriptor for the named file
and sets the file status flags according to the value of
oflag. For non-STREAMS (see intro(2)) files, oflag values
are constructed by OR-ing flags from the following list
(only one of the first three flags below may be used):
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.
O_NONBLOCK
This flag may affect subsequent reads and writes
(see read(2P) and write(2P)).
When opening a FIFO with O_RDONLY or O_WRONLY set:
If O_NONBLOCK is set:
An open for reading-only will return without
delay. An open for writing-only will return
an error if no process currently has the file
open for reading.
If O_NONBLOCK is clear:
An open for reading-only will block until a
process opens the file for writing. An open
for writing-only will block until a process
opens the file for reading.
When opening a file associated with a communication
line:
If O_NONBLOCK is set:
The open will return without waiting for
carrier.
Rev. 1.1 Page 1
open(2P) INTERACTIVE UNIX System (POSIX) open(2P)
If O_NONBLOCK is clear:
The open will block until carrier is present.
O_APPEND If set, the file pointer will be set to the end of
the file prior to each write.
O_SYNC When opening a regular file, this flag affects sub-
sequent writes. If set, each write(2P) will wait
for both the file data and file status to be physi-
cally updated.
O_CREAT If the file exists, this flag has no effect. Oth-
erwise, the owner ID of the file is set to the
effective user ID of the process; the group ID of
the file is set to the effective group ID of the
process or to the group ID of the directory in
which the file is being created, and the low-order
12 bits of the file mode are set to the value of
mode, modified as follows (see creat(2)):
All bits set in the file mode creation mask of
the process are cleared (see umask(2P)).
The ``save text image after execution bit'' of
the mode is cleared (see chmod(2P)).
O_TRUNC If the file exists, its length is truncated to 0
and the mode and owner are unchanged.
O_EXCL If O_EXCL and O_CREAT are set, open will fail if
the file exists.
O_NOCTTY If set and path identifies a terminal device, the
open function shall not cause the terminal device
to become the controlling terminal for the process.
When opening a STREAMS file, oflag may be constructed from
O_NONBLOCK OR-ed with either O_RDONLY, O_WRONLY, or O_RDWR.
Other flag values are not applicable to STREAMS devices and
have no effect on them. The value of O_NONBLOCK affects the
operation of STREAMS drivers and certain system calls (see
read(2P), getmsg(2), putmsg(2), and write(2P)). For
drivers, the implementation of O_NONBLOCK is device-
specific. Each STREAMS device driver may treat this option
differently.
Certain flag values can be set following open as described
in fcntl(2).
The file pointer used to mark the current position within
the file is set to the beginning of the file.
Rev. 1.1 Page 2
open(2P) INTERACTIVE UNIX System (POSIX) open(2P)
The new file descriptor is set to remain open across exec
system calls (see fcntl(2)).
The named file is opened unless one or more of the following
is true:
[EACCES] Search permission is denied on a component of
the path prefix, or the file exists and the
permissions specified by oflag are denied, or
the file does not exist and write permission
is denied for the parent directory of the
file to be created, or O_TRUNC is specified
and write permission is denied.
[EAGAIN] The file exists, mandatory file/record lock-
ing is set, and there are outstanding record
locks on the file (see chmod(2P)).
[EEXIST] O_CREAT and O_EXCL are set, and the named
file exists.
[EFAULT] path points outside the allocated address
space of the process.
[EINTR] A signal was caught during the open system
call.
[EIO] A hangup or error occurred during a STREAMS
open.
[EISDIR] The named file is a directory and oflag is
write or read/write.
[EMFILE] NOFILES file descriptors are currently open.
[ENAMETOOLONG] The length of the path argument exceeds
{PATH_MAX}, or a path name component is
longer than {NAME_MAX} while
{_POSIX_NO_TRUNC} is in effect.
[EMULTIHOP] Components of path require hopping to multi-
ple remote machines.
[ENFILE] The system file table is full.
[ENOENT] O_CREAT is not set and the named file does
not exist.
[ENOLINK] The path variable points to a remote machine,
and the link to that machine is no longer
active.
[ENOMEM] The system is unable to allocate a send
Rev. 1.1 Page 3
open(2P) INTERACTIVE UNIX System (POSIX) open(2P)
descriptor.
[ENOSPC] O_CREAT and O_EXCL are set, and the file sys-
tem is out of inodes.
[ENOSR] Unable to allocate a stream.
[ENOTDIR] A component of the path prefix is not a
directory.
[ENXIO] The named file is a character special or
block special file, and the device associated
with this special file does not exist.
[ENXIO] O_NONBLOCK is set, the named file is a FIFO,
O_WRONLY is set, and no process has the file
open for reading.
[ENXIO] A STREAMS module or driver open routine
failed.
[EROFS] The named file resides on a read-only file
system and oflag is write or read/write.
[ETXTBSY] The file is a pure procedure (shared text)
file that is being executed and oflag is
write or read/write.
SEE ALSO
chmod(2P), close(2), creat(2), dup(2), fcntl(2), getmsg(2),
intro(2), lseek(2), putmsg(2), read(2P), umask(2P),
write(2P).
DIAGNOSTICS
Upon successful completion, the file descriptor is returned.
Otherwise, a value of -1 is returned, and errno is set to
indicate the error.
Rev. 1.1 Page 4