Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

chmod(2)

close(2)

dup(2)

fcntl(2)

lseek(2)

read(2)

write(2)

umask(2)

tty(4)

open(2)

NAME

open − open a file for reading or writing, or create a new file

SYNTAX

#include <sys/file.h>
#include <limits.h>     /* definition of OPEN_MAX */

open(path, flags, mode)
char *path;
int flags, mode;

DESCRIPTION

The open system call opens the file specified and returns a descriptor for that file. The file pointer used to mark the current position within the file is set to the beginning of the file.

The file descriptor remains open across execve() system calls. The close() system call closes the file descriptor.

No process may have more than OPEN_MAX file descriptors open simultaneously. 

ARGUMENTS

pathis the address of a string of ASCII characters representing a path name, terminated by a null character.  The path name identifies the file to be opened. 

modeOnly used with the O_CREAT flag.  The file is created with the specified mode, as described in chmod() and modified by the process’s umask value (see umask().)).

flagsdefine how the file is to be opened.  This argument is formed by or’ing the following values:

O_RDONLYopen for reading only

O_WRONLYopen for writing only

O_RDWRopen for reading and writing

O_NDELAYdo not block on open

When opening a port (named pipe) with O_RDONLY or O_WRONLY:

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.

O_APPENDappend on each write

O_CREATcreate file if it does not exist

O_TRUNCtruncate size to 0

O_EXCLerror if create and file exists

O_BLKINUSEblock if file is in use

O_BLKANDSET
block if file is in use then set in use

O_FSYNCdo file writes synchronously

Opening a file with O_APPEND set causes each write on the file to be appended to the end. 

If O_TRUNC is specified and the file exists, the file is truncated to zero length. 

If O_EXCL is set with O_CREAT, then if the file already exists, the open returns an error.  This can be used to implement a simple exclusive access locking mechanism. 

If the O_NDELAY flag is specified and the open call would result in the process being blocked for some reason, the open returns immediately.  For example, if the process were waiting for carrier on a dialup line, an open with the O_NDELAY flag would return immediately.  The first time the process attempts to perform I/O on the open file, it will block. 

If the O_FSYNC flag is specified, each subsequent write (see write())) for the file is synchronous, instead of the default asynchronous writes.   Use this flag to be sure that the write is complete when the system call returns.   With asynchronous writes, the call returns when data is written to the buffer cache. There is no guarantee that the data was actually written out to the device.  With synchronous writes, the call returns when the data is written from the buffer cache to the device.

O_BLKINUSE and O_BLKANDSET provide a test and set operation similar to a semaphore.  O_BLKINUSE will cause the open to block if another process has marked the file as in use.  The open blocks in the system at a point where no references to the file are established.

There are two ways to mark a file as in use:

1.Use the ioctl() system call with the request argument set to FIOSINUSE or TIOCSINUSE.  (See tty(.)).)

2.Use the O_BLKANDSET flag to open(.).

O_BLKANDSET will cause the open to block if another process has marked the file in use.   When the open resumes, the file is marked in use by the current process.

If O_NDELAY is used in conjunction with either O_BLKINUSE or O_BLKANDSET, the open will fail if the file is in use. The external variable errno is set to EWOULDBLOCK in this case.

NOTE

The in use flag cannot be inherited by a child process nor can it be replicated by dup(.).

When the in use flag is cleared, all processes that are blocked for that reason will resume. The open will continue to block if another process marks the file as in use again.

The in use flag can be cleared in three ways:

1.When the file descriptor marked as in use is closed

2.When the process that set the in use flag exits

3.When an ioctl() system call is issued with the request argument as FIOCINUSE or TIOCCINUSE. 

ENVIRONMENT

When your program is compiled using the System V environment, and O_NDELAY is specified, subsequent reads and writes are also affected. 

DIAGNOSTICS

The named file is opened unless one or more of the following are true:

[EINVAL] The pathname contains a character with the high-order bit set. 

[EINVAL] An attempt was made to open a file with the O_RDONLY and O_FSYNC flags set. 

[ENOTDIR] A component of the path prefix is not a directory. 

[ENOENT] O_CREAT is not set and the named file does not exist. 

[EACCES] The required permissions for reading and/or writing are denied for the named flag. 

[EISDIR] The named file is a directory, and the arguments specify it is to be opened for writing. 

[EROFS] The named file resides on a read-only file system, and the file is to be modified. 

[EMFILE] {OPEN_MAX} file descriptors 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. 

[ETXTBSY] The file is a pure procedure (shared text) file that is being executed and the open call requests write access.

[EFAULT] Path points outside the process’s allocated address space. 

[ELOOP] Too many symbolic links were encountered in translating the pathname. 

[EEXIST] O_CREAT and O_EXCL were specified and the file exists. 

[ENXIO] The O_NDELAY flag is given, and the file is a communications device on which there is no carrier present. 

[EOPNOTSUPP]
An attempt was made to open a socket that is not set active.

[EWOULDBLOCK]
The open would have blocked if the O_NDELAY was not used. The probable cause for the block is that the file was marked in use. 

[ENAMETOOLONG]
A component of a pathname exceeded 255 characters, or an entire pathname exceeded 1023 characters.

[ENOENT] A component of the path name that must exist does not exist. 

[EACCES] Search permission is denied for a component of the path prefix. 

[EACCES] O_CREAT is specified, the file does not exist, and the directory in which it is to be created does not permit writing. 

[ENFILE] The system file table is full. 

[ENOSPC] O_CREAT is specified, the file does not exist, and the directory in which the entry for the new file is being placed cannot be extended because there is no space left on the file system containing the directory. 

[ENOSPC] O_CREAT is specified, the file does not exist, and there are no free inodes on the file system on which the file is being created. 

[EDQUOT] O_CREAT is specified, the file does not exist, 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] O_CREAT is specified, the file does not exist, and the user’s quota of inodes on the file system on which the file is being created has been exhausted. 

[EIO] An I/O error occurred while making the directory entry or allocating the inode for O_CREAT. 

[ESTALE] The file handle given in the argument is invalid.  The file referred to by that file handle no longer exists or has been revoked. 

[ETIMEDOUT] A "connect" request or remote file operation failed because the connected party did not properly respond after a period of time which is dependent on the communications protocol. 

SEE ALSO

chmod(2), close(2), dup(2), fcntl(2), lseek(2), read(2), write(2), umask(2), tty(4)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026