OPEN(2) — HP-UX
NAME
open − open file for reading or writing
SYNOPSIS
#include <fcntl.h>
int open (path, oflag [ , mode ] )
char ∗path;
int oflag, mode;
DESCRIPTION
Path points to a path name naming a file; it may not exceed 1024 bytes in length. Open opens a file descriptor for the named file and sets the file status flags according to the value of oflag. 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 of the other flags listed below can be changed during the time the file is open using fcntl. 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 may 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 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 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 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", set-user-id and set-group-id bits 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.
O_SYNCIO If a file is opened with O_SYNCIO or is set with the F_SETFL option of fcntl, file system writes for that file will be done through the cache to the disk as soon as possible, and the process will block until this is completed. 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 which permit I/O to block special devices.
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 page.
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 page. For an example of writing to outputfile, see the write(2) manual page.
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 will fail and the file will not be opened if one of the following conditions is true. Errno will be set accordingly:
[ENOTDIR] A component of the path prefix is not a directory.
[ENOENT] O_CREAT is not set.
[ENOENT] The named file does not exist (for example, path is null or a component of path does not exist).
[EACCES] A component of the path prefix denies search permission.
[EACCES] Oflag permission is denied for the named file.
[EACCES] The file does not exist and the directory in which the file is to be created does not permit writing.
[EISDIR] The named file is a directory and oflag is write or read/write.
[EROFS] The named file resides on a read-only file system and oflag is write or read/write.
[EMFILE] The maximum number of file descriptors allowed are currently open.
[ENXIO] The named file is a character special or block special file, and the device associated with this special file does not exist.
[ENXIO] O_NDELAY is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading.
[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 may be kept open for a long time, or indefinitely under some circumstances.
[EEXIST] O_CREAT and O_EXCL are set, and the named file exists.
[EINTR] A signal was caught during the open system call, and the system call was not restarted (see signal(2) and sigvector(2)).
[ENFILE] The system file table is full.
[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)).
[EFAULT] Path points outside the allocated address space of the process.
[EINVAL] Oflag specifies both O_WRONLY and O_RDWR.
[ENAMETOOLONG] The path specified exceeds MAXPATHLEN characters.
[ELOOP] Too many symbolic links were encountered in translating the path name.
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), lseek(2), read(2), select(2), umask(2), write(2), lockf(2).
Hewlett-Packard Company — May 11, 2021