open(2)
NAME
open − open file for reading or writing
SYNOPSIS
#include <fcntl.h>
int open(
const char *path,
int oflag, ...
/* mode_t mode */
);
DESCRIPTION
open() opens a file descriptor for the named file and sets the file status flags according to the value of oflag. path points to a path name naming a file, and must not exceed PATH_MAX bytes in length. oflag values are constructed by OR-ing flags from the list below.
Exactly one of the flags O_RDONLY, O_WRONLY, or O_RDWR must be used in composing the value of oflag. If none or more than one is used, the behavior is undefined. Several other flags listed below can be changed by using fcntl() while the file is open. See fcntl(2) and fcntl(5) for details.
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.
O_NDELAY This flag might affect subsequent reads and writes. See read(2) and write(2).
When opening a FIFO with O_RDONLY or O_WRONLY set:
If O_NDELAY 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 O_NDELAY is clear:
An open() for reading-only does not return until a process opens the file for writing. An open() for writing-only does not return until a process opens the file for reading.
When opening a file associated with a communication line:
If O_NDELAY is set:
The open() returns without waiting for carrier.
If O_NDELAY is clear:
The open() does not return until carrier is present.
O_NONBLOCK Same effect as O_NDELAY for open(2), but slightly different effect in read(2) and write(2). Only one of O_NONBLOCK and O_NDELAY can be specified.
O_APPEND If set, the file offset is set to the end of the file prior to each write.
O_CREAT If the file exists, this flag has no effect, except as noted under O_EXCL below. 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 if the set-group- ID bit of the parent directory is not set, or to the group ID of the parent directory if the set-group- ID bit of the parent directory is set. The file access permission bits of the file mode are set to the value of mode modified as follows (see creat(2)):
• For each bit set in the file mode creation mask of the process, the corresponding bit in the new file’s mode is cleared (see umask(2)).
• The "save text image after execution" bit of the mode is cleared. See chmod(2).
• On systems with access control lists, three base ACL entries are created corresponding to the file access permissions (see acl(5)).
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, open() does not cause the terminal to become the controlling terminal for the process.
O_SYNC If a file is opened with O_SYNC or if that flag is set with the F_SETFL option of fcntl(), file system writes for the file are done through the cache to the disk as soon as possible, and the process blocks until the data is written to the buffer cache. This flag is ignored by all I/O calls except write(), and is ignored for files other than ordinary files and block special devices on those systems that permit I/O to block special devices.
The name O_SYNCIO is a synonym for O_SYNC, and is defined for backward compatibility in <fcntl.h>.
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).
EXAMPLES
The following call to open() opens file inputfile for reading only and returns a file descriptor for inputfile. For an example of reading from file inputfile, see the read(2) manual entry.
int myfd;
myfd = open ("inputfile", O_RDONLY);
The following call to open() opens file outputfile for writing and returns a file descriptor for outputfile. For an example of preallocating disk space for outputfile, see the prealloc(2) manual entry. For an example of writing to outputfile, see the write(2) manual entry.
int outfd; outfd = open ("outputfile", O_WRONLY);
RETURN VALUE
Upon successful completion, the file descriptor is returned. Otherwise, a value of −1 is returned and errno is set to indicate the error.
ERRORS
open() fails and the file is not opened if any of the following conditions are encountered:
[EACCES] oflag permission is denied for the named file.
[EACCES] A component of the path prefix denies search permission.
[EACCES] The file does not exist and the directory in which the file is to be created does not permit writing.
[EAGAIN] One or more segments of a pre-existing file have been locked with lockf or fcntl by some other process, and O_TRUNC is set.
[EAGAIN] The file exists, enforcement mode file/record locking is set, and there are outstanding record locks on the file (see chmod(2)).
[EDQUOT] User’s disk quota block or inode limit has been reached for this file system.
[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, and the system call was not restarted (see signal(5) and sigvector(2)).
[EINVAL] oflag specifies both O_WRONLY and O_RDWR.
[EINVAL] oflag specifies both O_NONBLOCK and O_NDELAY.
[EISDIR] The named file is a directory and oflag is write or read/write.
[ELOOP] Too many symbolic links are encountered in translating the path name.
[EMFILE] The maximum number of file descriptors allowed are currently open.
[ENAMETOOLONG]
The length of the specified path name exceeds PATH_MAX bytes, or the length of a component of the path name exceeds NAME_MAX bytes while _POSIX_NO_TRUNC is in effect.
[ENFILE] The system file table is full.
[ENOENT] The named file does not exist (for example, path is null or a component of path does not exist, or the file itself does not exist and O_CREAT is not set).
[ENOTDIR] A component of the path prefix is not a directory.
[ENXIO] O_NDELAY is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading.
[ENODEV] The named file is a character special or block special file, and the device associated with this special file either does not exist, or the driver for this device has not been configured into the kernel.
[EROFS] The named file resides on a read-only file system and oflag is write or read/write.
[ETXTBSY] The file is open for execution and oflag is write or read/write. Normal executable files are only open for a short time when they start execution. Other executable file types can be kept open for a long time, or indefinitely under some circumstances.
DEPENDENCIES
HP Clustered Environment:
Attempting to open a device file with a st_rcnode value that does not match the cnode ID of the machine on which the calling process is running (or 0) fails with an EOPNOTSUPP error.
WARNINGS
Access Control Lists
Access control list descriptions in this entry apply only to standard HP-UX operating systems. If HP-UX BLS software has been installed, access control lists are handled differently. Refer to HP-UX BLS documentation for information about access control lists in the HP-UX BLS environment.
AUTHOR
open() was developed by HP, AT&T, and the University of California, Berkeley.
SEE ALSO
chmod(2), close(2), creat(2), dup(2), fcntl(2), lockf(2), lseek(2), read(2), select(2), setacl(2), umask(2), write(2), acl(5), fcntl(5), signal(5).
STANDARDS CONFORMANCE
open(): AES, SVID2, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1
Hewlett-Packard Company — HP-UX Release 9.0: August 1992