NAME [Toc] [Back]
open() - open file for reading or writing
SYNOPSIS [Toc] [Back]
int open(const char *path, int oflag, ... /* [mode_t mode] */ );
Remarks [Toc] [Back]
The ANSI C ", ..." construct specifies a variable length argument list
whose optional member is given in the associated comment (/* */).
DESCRIPTION [Toc] [Back]
The open() system call opens a file descriptor for the named file and
sets the file status flags according to the value of oflag.
The path argument points to a path name naming a file, and must not
exceed PATH_MAX bytes in length.
The oflag argument is a value that is the bitwise inclusive OR of
flags listed in "Read-Write Flags," "General Flags," and "Synchronized
I/O Flags" below.
The optional mode argument is only effective when the O_CREAT flag is
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).
Read-Write Flags [Toc] [Back]
Exactly one of the O_RDONLY, O_WRONLY, or O_RDWR flags must be used in
composing the value of oflag. If none or more than one is used, the
behavior is undefined.
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.
General Flags [Toc] [Back]
Several of the flags listed below can be changed with the fcntl()
system call while the file is open. See fcntl(2) and fcntl(5) for
O_APPEND If set, the file offset is set to the end of the
file prior to each write.
Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003
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 new 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 mode is cleared (see umask(2)).
+ The "save text image after execution" bit of
the new file mode is cleared. See chmod(2).
+ On HFS file systems with access control lists,
three base ACL entries are created
corresponding to the file access permissions
+ On JFS file systems that support access control
lists, optional ACL entries are created
corresponding to the parent directory's default
ACL entries (see aclv(5)).
O_EXCL If O_EXCL and O_CREAT are set and the file exists,
O_LARGEFILE This is a non-standard flag which may be used by
32-bit applications to access files larger than 2
GB. See creat64(2).
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:
A read-only open() returns without
A write-only open() returns an error if
no process currently has the file open
Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003
If O_NDELAY is clear:
A read-only open() does not return until
a process opens the file for writing.
A write-only open() does not return
until a process opens the file for
When opening a file associated with a
If O_NDELAY is set:
The open() returns without waiting for
If O_NDELAY is clear:
The open() does not return until carrier
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_NONBLOCK Same effect as O_NDELAY for open(2), but slightly
different effect in read(2) and write(2). If both
O_NONBLOCK and O_NDELAY are specified, O_NONBLOCK
O_TRUNC If the file exists, its length is truncated to 0
and the mode and owner are unchanged.
Synchronized I/O Flags
Together, the O_DSYNC, O_RSYNC, and O_SYNC flags constitute support
for Synchronized I/O. These flags are ignored for files other than
ordinary files and block special files on those systems that permit
I/O to block special devices (see pathconf(2)). If both the O_DSYNC
and O_SYNC flags are set, the effect is as if only the O_SYNC flag was
set. The O_RSYNC flag is ignored if it is not set along with the
O_DSYNC or O_SYNC flag.
O_DSYNC [Toc] [Back]
If a file is opened with O_DSYNC or that flag is set with
the F_SETFL option of fcntl(), writes to that file by the
process block until the data specified in the write request
and all file attributes required to retrieve the data are
written to the disk. File attributes that are not necessary
for data retrieval (access time, modification time, status
change time) are not necessarily written to the disk prior
Hewlett-Packard Company - 3 - HP-UX 11i Version 2: August 2003
to returning to the calling process.
O_SYNC [Toc] [Back]
Identical to O_DSYNC, with the addition that all file
attributes changed by the write operation (including access
time, modification time, and status change time) are also
written to the disk prior to returning to the calling
O_RSYNC|O_DSYNC (specified together)
Identical to O_DSYNC for file system writes.
For file system reads, the calling process blocks until the
data being read and all file attributes required to retrieve
the data are the same as their image on disk. Writes
pending on the data to be read are executed prior to
returning to the calling process.
O_RSYNC|O_SYNC (specified together)
Identical to O_SYNC for file system writes.
Identical to O_RSYNC|O_DSYNC for file system reads, with the
addition that all file attributes changed by the read
operation (including access time, modification time, and
status change time) too are the same as their image on disk.
RETURN VALUE [Toc] [Back]
open() returns the following values:
n Successful completion. n is a file descriptor for the
-1 Failure. errno is set to indicate the error.
ERRORS [Toc] [Back]
If open() fails, errno is set to one of the following values.
[EACCES] oflag permission is denied for the named file.
[EACCES] A component of the path prefix denies search
[EACCES] The file does not exist and the directory in which
the file is to be created does not permit writing.
[EACCES] O_TRUNC is specified and write permission is
[EAGAIN] The file exists, enforcement mode file/record
locking is set (see chmod(2)), there are
outstanding record locks on the file with the
lockf() or fcntl() system calls, and O_TRUNC is
Hewlett-Packard Company - 4 - HP-UX 11i Version 2: August 2003
[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
[EFAULT] path points outside the allocated address space of
[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.
[EISDIR] The named file is a directory and oflag is write
[ELOOP] Too many symbolic links are encountered in
translating the path name.
[EMFILE] The maximum number of file descriptors allowed are
[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.
[ENODEV] The named file is a character special or block
special file, and the driver associated with this
special file has not been configured into the
kernel or the DLKM driver associated with this
special file failed to load.
[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
[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
Hewlett-Packard Company - 5 - HP-UX 11i Version 2: August 2003
[ENXIO] The named file is a character special or block
special file, and the device associated with this
special file does not exist.
[ENOSPC] O_CREAT is specified, the file does not already
exist, and the directory that would contain the
file cannot be extended.
[EOVERFLOW] The named file is a regular file and the size of
the file cannot be represented correctly in an
object of size off_t.
[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
EXAMPLES [Toc] [Back]
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.
infd = 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)
outfd = open ("outputfile", O_WRONLY);
The following call opens file iofile for synchronized I/O file
integrity for reads and writes.
iofd = open ("iofile", O_RDWR|O_SYNC|O_RSYNC);
AUTHOR [Toc] [Back]
open() was developed by HP, AT&T, and the University of California,
SEE ALSO [Toc] [Back]
acl(2), chmod(2), close(2), creat(2), dup(2), fcntl(2), lockf(2),
Hewlett-Packard Company - 6 - HP-UX 11i Version 2: August 2003
lseek(2), creat64(2), pathconf(2), read(2), select(2), umask(2),
write(2), setacl(2), acl(5), aclv(5), fcntl(5), signal(5),
STANDARDS CONFORMANCE [Toc] [Back]
open(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1,
Hewlett-Packard Company - 7 - HP-UX 11i Version 2: August 2003 [ Back ]