OPEN(2V) SYSTEM CALLS OPEN(2V) NAME open - open or create a file for reading or writing SYNOPSIS #include int open(path, flags [ , mode ] ) char *path; int flags, mode; DESCRIPTION path points to the pathname of a file. open opens the named file for reading and/or writing, as specified by the flags argument, and returns a descriptor for that file. The flags argument may indicate the file is to be created if it does not already exist (by specifying the O_CREAT flag), in which case the file is created with mode mode as described in chmod(2) and modified by the process' umask value (see umask(2)). If the path is a null string, the kernel maps this null pathname to ., the current directory. flags values are constructed by ORing 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 When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NDELAY 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_NDELAY 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_NDELAY is set: The open will return without waiting for car- rier. The first time the process attempts to Sun Release 3.2 Last change: 16 July 1986 1 OPEN(2V) SYSTEM CALLS OPEN(2V) perform I/O on the open file it will block (not currently implemented). 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. 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 group ID of the directory in which the file is 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(2). The ``save text image after execution'' bit of the mode 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 will fail if the file exists. This can be used to implement a simple exclusive access locking mechanism. If O_EXCL is set and the last component of the path- name is a symbolic link, the open will fail even if the symbolic link points to a non-existent name. The file pointer used to mark the current position within the file is set to the beginning of the file. The new descriptor is set to remain open across execve sys- tem calls; see close(2) and fcntl(2). There is a system enforced limit on the number of open file descriptors per process, whose value is returned by the getdtablesize(2) call. SYSTEM V DESCRIPTION If the O_NDELAY flag is set on an open, that flag is set for that file descriptor (see fcntl) and may affect subsequent reads and writes. See read(2V) and write(2V). RETURN VALUE The value -1 is returned if an error occurs, and external variable errno is set to indicate the cause of the error. Sun Release 3.2 Last change: 16 July 1986 2 OPEN(2V) SYSTEM CALLS OPEN(2V) Otherwise a non-negative numbered file descriptor for the new open file is returned. ERRORS Open fails if: ENOTDIR A component of the path prefix of path is not a directory. EINVAL path contains a character with the high-order bit set. ENAMETOOLONG The length of a component of path exceeds 255 characters, or the length of path exceeds 1023 characters. ENOENT O_CREAT is not set and the named file does not exist. ENOENT A component of the path prefix of path does not exist. ELOOP Too many symbolic links were encountered in translating path. EACCES Search permission is denied for a component of the path prefix of path. EACCES The required permissions (for reading and/or writing) are denied for the file named by path. EACCES The file referred to by path does not exist, O_CREAT is specified, and the directory in which it is to be created does not permit writing. EISDIR The named file is a directory, and the argu- ments specify it is to be opened for writing. ENXIO O_NDELAY is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading. EMFILE The system limit for open file descriptors per process has already been reached. ENFILE The system file table is full. ENOSPC The file does not exist, O_CREAT is speci- fied, and the directory in which the entry for the new file is being placed cannot be Sun Release 3.2 Last change: 16 July 1986 3 OPEN(2V) SYSTEM CALLS OPEN(2V) extended because there is no space left on the file system containing the directory. ENOSPC The file does not exist, O_CREAT is speci- fied, and there are no free inodes on the file system on which the file is being created. EDQUOT The file does not exist, O_CREAT is speci- fied, and the directory in which the entry for the new file is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted. EDQUOT The file does not exist, O_CREAT is speci- fied, and the user's quota of inodes on the file system on which the file is being created has been exhausted. EROFS The named file does not exist, O_CREAT is specified, and the file system on which it is to be created is a read-only file system. EROFS The named file resides on a read-only file system, and the file is to be opened for writing. ENXIO The file is a character special or block spe- cial file, and the associated device does not exist. EINTR A signal was caught during the open system call. ETXTBSY The file is a pure procedure (shared text) file that is being executed and the open call requests write access. EIO An I/O error occurred while reading from or writing to the file system. EFAULT path points outside the process's allocated address space. EEXIST O_EXCL and O_CREAT were both specified and the file exists. EOPNOTSUPP An attempt was made to open a socket (not currently implemented). Sun Release 3.2 Last change: 16 July 1986 4 OPEN(2V) SYSTEM CALLS OPEN(2V) SEE ALSO chmod(2), close(2), dup(2), fcntl(2), lseek(2), read(2V), write(2V), umask(2) Sun Release 3.2 Last change: 16 July 1986 5