Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > O

open(2)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

open() — open file for reading or writing

SYNOPSIS

#include <fcntl.h>

int open(const char *path, int oflag, ... /* [mode_t mode] */ );

Remarks

The ANSI C ", ..." construct specifies a variable length argument list whose optional member is given in the associated comment (/* */).

DESCRIPTION

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," "Synchronized I/O Flags," and "Direct I/O Flags" below.

The optional mode argument is only effective when the O_CREAT flag is specified.

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

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

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 details.

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 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 (see acl(5)).

  • 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, open() fails.

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 delay.

    • A write-only open() returns an error if no process currently has the file open for reading.

  • 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 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_NOCTTY

If set, and path identifies a terminal device, open() does not cause the terminal to become the controlling terminal for the process.

O_NOFOLLOW

If the last component of path refers to a symbolic link, the open() will fail. This is true regardless of whether the target of the link exists or not.

NOTE: If the symbolic link in path is followed by a trailing '/', the link will be traversed, regardless of the setting of O_NOFOLLOW.

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 takes precedence.

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

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 to returning to the calling process.

O_SYNC

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 process.

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

open() returns the following values:

n

Successful completion. n is a file descriptor for the opened file.

-1

Failure. errno is set to indicate the error.

ERRORS

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 permission.

[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 denied.

[EAGAIN]

The file exists, enforcement mode file/record locking is set (see chmod(2)), there are outstanding record locks on the file with the flock(), lockf(), or fcntl() system calls, and O_TRUNC is set.

[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)).

[EINVAL]

oflag specifies both O_WRONLY and O_RDWR.

[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.

[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 set).

[ENOTDIR]

A component of the path prefix is not a directory.

[ENOTSUP]

The last component of path is a sybolic link and O_NOFOLLOW was set.

[ENXIO]

O_NDELAY is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading.

[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 circumstances.

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 infd; 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) manual entry.

int outfd; outfd = open ("outputfile", O_WRONLY);

The following call opens file iofile for synchronized I/O file integrity for reads and writes.

int iofd; iofd = open ("iofile", O_RDWR|O_SYNC|O_RSYNC);

AUTHOR

open() was developed by HP, AT&T, and the University of California, Berkeley.

STANDARDS CONFORMANCE

open(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, POSIX.4

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.