open(2)
NAME
open − open for reading or writing
SYNTAX
#include <fcntl.h>
int open(name, oflag [ , mode ])
char *name;
int oflag, mode;
DESCRIPTION
The open system call opens for reading and writing the file specified by name. The specified name points to a path name naming a file. The open call opens a file descriptor for the named file and sets the file status flags according to oflag. The oflag values are constructed by a bitwise or of the following (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_NDELAY
This flag may affect subsequent reads and writes. For further information, see read(2) and write(2).
•When opening a FIFO with O_RDONLY or O_WRONLY set:
−If O_NDELAY is set, an open call for reading-only will return without delay. An open call for writing-only will return an error, if no process currently has the file open for reading.
−If O_NDELAY is clear, an open call for reading-only will block until a process opens the file for writing. An open call for writing-only will block until a process opens the file for reading.
•When opening a file associated with a communication line:
−If O_NDELAY is set, the open call will return without waiting for carrier.
−If O_NDELAY 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_CREAT
If the file exists, this flag has no effect. Otherwise, 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, and the low-order 12 bits of the file mode are set to the value of the mode (see creat(2)) and modified as follows:
•All bits set in the file mode creation mask of the process are cleared. For further information, see umask(2).
•The “save text image after execution bit” of the mode is cleared. For further information, see chmod(2).
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 call will fail if the file exists.
The file pointer used to mark the current position within the file is set to the beginning of the file.
The new file descriptor is set to remain open across exec calls. For further information, see fcntl(2).
DIAGNOSTICS
The open call will fail if:
[EACCES] A component of the path prefix denies search permission.
[EBUSY] The device specified is already open for exclusive use, and the caller is not the superuser.
[EFAULT] The specified name points outside the process’s allocated address space.
[EEXIST] O_CREAT and O_EXCL are set, and the named file exists.
[ENXIO] Either O_NDELAY is set, and the named file is a FIFO, or O_WRONLY is set, and no process has the file open for reading.
[EINTR] A signal was caught during the open call.
[ENOSPC] Either the directory to contain the file cannot be extended, or the file does not exist, and O_CREAT is specified.
[EISDIR] The named file is a directory, and the arguments specify it is to be opened for writing.
[EMFILE] The maximum number of file descriptors allowed (20) are already open.
[ENFILE] No more system file descriptors are available.
[ENFILE] There is insufficient system space to contain the inode.
[ENOENT] O_CREAT is not set, and the named file does not exist.
[ENOTDIR] A component of the path prefix is not a directory.
[ENXIO] Device special file is not for the current system (major device number is greater than either nchrdev or nblkdev).
[ENXIO] An attempt was made to open /dev/tty without a controlling terminal.
[ENXIO] An illegal device was specified, or the device is already open.
[EROFS] The named file resides on a read-only file system, and the file is to be modified.
[ETO] The specified tape drive is already open.
[ETOL] The specified tape drive is not on-line.
[ETPL] Tape position was lost for the specified tape drive.
[ETWL] The specified tape drive is physically write-locked.
[ETXTBSY] The file is a pure procedure (shared text) file that is being executed and the open call has requested write access.
ASSEMBLER
(open = 5.)
sys open; name; mode
(file descriptor in r0)
SEE ALSO
chmod(2), close(2), creat(2), dup(2), fcntl(2), read(2), umask(2), write(2)