open(2) open(2)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 open opens a file descriptor for the named file and sets the file status flags according to the value of oflag. The ar- gument path points to a pathname naming a file. The 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_NDELAY or O_NONBLOCK Either flag may affect subsequent reads and writes. See read(2) and write(2). When opening a FIFO with O_RDONLY or O_WRONLY set and if O_NDELAY or O_NONBLOCK is set, an open for reading-only returns without delay. An open for writing-only returns an error if no pro- cess currently has the file open for reading. if O_NDELAY and O_NONBLOCK are clear, an open for reading-only blocks until a process opens the file for writing. An open for writing- only blocks until a process opens the file for reading. When opening a file associated with a communication line and if O_NDELAY or O_NONBLOCK is set, the open returns without waiting for a carrier. if O_NDELAY and O_NONBLOCK are clear, an open blocks until a carrier is present. O_APPEND If set, the file pointer is set to the end of the file prior to each write. April, 1990 1
open(2) open(2)O_CREAT If the file exists, this flag has no effect. Oth- erwise, the owner ID of the file is set to the ef- fective 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 mode modified as fol- lows (see creat(2)): All bits set in the file-mode-creation mask of the process are cleared. See umask(2). Mode bit 01000 (save text image after execu- tion bit) is cleared. 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 fails if the file exists. O_NOCTTY If set, and path identifies a terminal device, the open does not cause the terminal device to become the controlling terminal for the process. O_NOCTTY has been added for compliance with POSIX. 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 system calls. See fcntl(2). RETURN VALUE On successful completion, the file descriptor is returned. Otherwise, a value of -1 is returned and errno is set to in- dicate the error. ERRORS The named file is opened unless one or more of the following are true: [ENOTDIR] A component of the path prefix is not a direc- tory. [ENAMETOOLONG] A component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX. [ELOOP] Too many symbolic links were encountered in translating a pathname. [ENOENT] O_CREAT is not set, and the named file does not 2 April, 1990
open(2) open(2)exist. [EACCES] A component of the path prefix denies search permission. [EACCES] An oflag permission is denied for the named file. [EISDIR] The named file is a directory, and oflag is write or read/write. [EROFS] The named file resides on a read-only file sys- tem, and oflag is write or read/write. [EMFILE] The per-process open file limit would be ex- ceeded. [ENXIO] The named file is a character-special or block-special file, and the device associated with this special file does not exist. [ETXTBSY] The file is a pure-procedure (shared text) file that is being executed, and oflag is write or read/write. Note: If you are running an network file system (NFS) and you are accessing a shared binary remotely, it is possible that you will not get this errno. [EFAULT] path points outside the allocated address space of the process. [EEXIST] O_CREAT and O_EXCL are set, and the named file exists. [ENXIO] O_NDELAY is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading. [EINTR] A signal was caught during the open system call. [ENFILE] The system file table is full. [ENOSPC] The directory or file system that would contain the new file cannot be extended. SEE ALSO chmod(2), close(2), creat(2), dup(2), fcntl(2), lseek(2), read(2), umask(2), write(2), fopen(3), ferror(3). April, 1990 3