open(S) 6 January 1993 open(S) Name open - open for reading or writing Syntax cc . . . -lc #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (path, oflag [, mode] ) char *path; int oflag, mode; Description path 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 accord- ing to the value of oflag. For non-STREAMS files, oflag values are con- structed by OR-ing flags from the following list (only one of the first three flags below can be used): ORDONLY Open for reading only. OWRONLY Open for writing only. ORDWR Open for reading and writing. OAPPEND If set, the file pointer is set to the end of the file prior to each write. OCREAT If the file exists, this flag has no effect. Otherwise, the user 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 file's parent directory; and the low-order 12 bits of the file mode are set to the value of mode, modified as follows (see creat(S)): + All bits set in the file mode creation mask of the process are cleared (see umask(S)). + The ``save text image after execution bit'' of the mode is cleared (see chmod(S)). OEXCL Causes open to fail if the file exists. This can only be used if OCREAT is also set. ONOCTTY If this flag is set and path identifies a terminal, then that terminal is prevented from becoming the controlling terminal for the process OSYNC When opening a regular file, this flag affects subsequent writes. If set, each write(S) waits for both the file data and file status to be physically updated. OTRUNC If the file exists, is a regular file, and is opened for writing, its length is truncated to 0. The mode, owner and group are left unchanged. It has no effect on FIFO special files or directories. ONONBLOCK This flag may affect subsequent reads and writes (see read(S) and write(S)). When opening a FIFO with ORDONLY or OWRONLY set: If ONONBLOCK is set: An open for reading only returns without delay. An open for writing only returns an error if no process currently has the file open for reading. If ONONBLOCK is not set: An open for reading only will block 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: If ONONBLOCK is set: The open returns without waiting for carrier. If ONONBLOCK is not set: The open blocks until carrier is present. When opening a STREAMS file, oflag may be constructed from ONONBLOCK or-ed with either ORDONLY, OWRONLY or ORDWR. Other flag values are not applicable to STREAMS devices and have no effect on them. The value of ONONBLOCK affects the operation of STREAMS drivers and certain system calls (see read(S), getmsg(S), putmsg(S), and write(S)). For drivers, the implementation of ONONBLOCK is device-specific. Each STREAMS device driver may treat this option differently. Certain flag values can be set following open as described in fcntl(S). 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(S)). The named file is opened unless one or more of the following is true: [EACCES] + A component of the path prefix denies search permis- sion. + oflag permission is denied for the named file. + The file does not exist and write permission is not set for the parent directory of the file to be created. + OTRUNC is specified and write permission is not set for the file. [EAGAIN] The file exists, mandatory file/record locking is set, and there are outstanding record locks on the file (see chmod(S)) and OTRUNC is included in oflag. [EEXIST] OCREAT and OEXCL 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. [EINVAL] The value of oflag is not valid. [EIO] A hangup or error occurred during a STREAMS open. [EISDIR] The named file is a directory and oflag includes OWRONLY or ORDWR [EMFILE] OPENMAX descriptors are open in the current process. [EMULTIHOP] Components of path require hopping to multiple remote ma- chines. [ENAMETOOLONG] The length of path exceeds PATHMAX or the length of a pathname component exceeds NAMEMAX while POSIXNOTRUNC is in effect. [ENFILE] The system file table is full, SYSOPEN files are open in the system. [ENOENT] + OCREAT is not set and the named file does not exist. + OCREAT is set and either the path prefix does not exist or the path argument is a null string. [ENOLINK] path points to a remote machine, and the link to that ma- chine is no longer active. [ENOMEM] The system is unable to allocate a send descriptor. [ENOSPC] OCREAT and OEXCL are set, and the file system 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. + ONONBLOCK is set, the named file is a FIFO, OWRONLY is set, and no process has the file open for reading. + A STREAMS module or driver open routine failed. [EROFS] The named file resides on a read-only file system and oflag includes either OWRONLY, ORDWR, OCREAT (if the file does not exist) or OTRUNC. [ETXTBSY] The file is a pure procedure (shared text) file that is being executed and oflag includes either OWRONLY or ORDWR. Limitation A maximum of three XENIX or UNIX partitions may be opened on a hard disk. Even if any of these are subsequently closed, no more may be opened until reboot. Diagnostics Upon successful completion, the file descriptor is returned. Otherwise, a value of -1 is returned, and errno is set to indicate the error. See also chmod(S), close(S), creat(S), dup(S), fcntl(S), getmsg(S), lseek(S), putmsg(S), read(S), umask(S), write(S) Standards conformance open is conformant with: AT&T SVID Issue 2; X/Open Portability Guide, Issue 3, 1989; Intel386 Binary Compatibility Specification, Edition 2 (iBCSe2); IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1); and NIST FIPS 151-1.