open, creat - Open a file for reading or writing
#include <fcntl.h> #include <sys/stat.h> #include <sys/types.h>
int open (
const char *path,
int oflag [ ,
mode_t mode ] );
int creat (
const char *path,
mode_t mode );
Interfaces documented on this reference page conform to industry standards as follows:
creat(): POSIX.1, XPG4, XPG4-UNIX
open(): POSIX.1, XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about industry standards and associated tags.
Specifies the file to be opened or created. If the
path parameter refers to a symbolic link, the
open() function opens the file pointed to by the symbolic link.
Specifies the type of access, special open
processing, the type of update, and the initial state of the open
file. The parameter value is constructed by logically ORing special
open processing flags. These flags are defined in the fcntl.h
header file and are described below.
[DIGITAL] Specifies the read, write, and execute permissions
of the file to be created (requested by the O_CREAT flag in the
If the file already exists, this parameter is ignored. This parameter
is constructed by logically ORing values described in
the sys/mode.h header file.
The following two function calls are equivalent:
open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
The open() and creat() functions establish a connection between the file named by the path parameter and a file descriptor. The opened file descriptor is used by subsequent I/O functions, such as read() and write(), to access that file.
The returned file descriptor is the lowest file descriptor not previously open for that process.
Typically, no process can have more than OPEN_MAX file descriptors open simultaneously.
[DIGITAL] The per-process soft descriptor limit is checked. This number is configurable; the minimum value is 64. See getrlimit(2) and setrlimit(2).
The open() and creat() functions, which suspend the calling process until the request is completed, are redefined so that only the calling thread is suspended.
The file offset, marking 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 functions (see fcntl(2)).
The file status flags and file access flags are designated by the oflag parameter. The oflag parameter is constructed by bitwise-inclusive ORing exactly one of the file access flags (O_RDONLY, O_WRONLY, or O_RDWR) with one or more of the file status flags.
Exactly one of the file access values (O_RDONLY, O_WRONLY, or O_RDWR) must be specified. If none is set, O_RDONLY is assumed.
File status flags that specify special processing for subsequent reads and writes of the open file are as follows: If set, updates and writes to regular files and block devices are synchronous updates for data and file attribute information. On return from a function that performs an O_SYNC synchronous update (a write() system call when O_SYNC is set), the calling process is assured that all data and file attribute information for the file has been written to permanent storage, even if the file is also open for deferred update. If set, updates and writes to regular files and block devices are synchronous updates for data only. On return from a function that performs an O_DSYNC synchronous update (a write() system call when O_DSYNC is set), the calling process is assured that all data for the file has been written to permanent storage. Use of O_DSYNC does not guarantee that file-control information such as owner and modification time are updated to permanent storage. If set in combination with O_DSYNC, applies synchronized I/O data integrity completion to read operations. The calling process is assured that all pending writes of file data will have been written to permanent storage prior to a read, and that the data image has been successfully transferred to the calling process.
When opening a FIFO with O_RDONLY: If neither O_NDELAY nor O_NONBLOCK is set, the open() function blocks until another process opens the file for writing. If the file is already open for writing (even by the calling process), the open() function returns without delay. If O_NDELAY or O_NONBLOCK is set, the open() function returns immediately.
When opening a FIFO with O_WRONLY: If neither O_NDELAY nor O_NONBLOCK is set, the open() function blocks until another process opens the file for reading. If the file is already open for reading (even by the calling process), the open() function returns without delay. If O_NDELAY or O_NONBLOCK is set, the open() function returns an error if no process currently has the file open for reading.
When opening a block special or character special file that supports nonblocking opens, such as a terminal device: If neither O_NDELAY nor O_NONBLOCK is set, the open() function blocks until the device is ready or available. If O_NDELAY or O_NONBLOCK is set, the open() function returns without waiting for the device to be ready or available. Subsequent behavior of the device is device-specific.
When opening a STREAMS file, oflag may be constructed from O_NDELAY or O_NONBLOCK OR-ed with either O_RDONLY, O_WRONLY or O_RDWR. Other flag values are not applicable to STREAMS devices and have no effect on them. The value of O_NDELAY or NON_BLOCK affects the operation of STREAMS drivers and certain system calls. (See read(2), getmsg(2), putmsg(2), and write(2).) For drivers, the implementation of O_NDELAY or NON_BLOCK is device-specific. Each STREAMS device driver may treat this option differently.
Since a file newly created by creat() is write_only, an fdopen() call using the r+ parameter fails when following a creat() call. A solution to this problem is to create the file using a call that adheres to the following format:
open(path, O_RDWR | O_CREAT, 0666);
On successful completion, the open() and creat() functions return the file descriptor, a nonnegative integer. Otherwise, a value of -1 is returned and errno is set to indicate the error.
If the open() or creat() function fails, errno may be set to one of the following values: Search permission is denied on a component of the path prefix, or the type of access specified by the oflag parameter is denied for the named file, or the file does not exist and write permission is denied for the parent directory, or O_TRUNC is specified and write permission is denied. The O_TRUNC flag is set, the named file exists with enforced record locking enabled and there are record locks on the file. The named file is a block device file and the block device is in use by a mounted file system. The directory in which the entry for the new link is being placed cannot be extended because the quota of disk blocks or i-nodes defined for the user on the file system containing the directory has been exhausted. The O_CREAT and O_EXCL flags are set and the named file exists. The path parameter is an invalid address. A signal was caught by the open() function. The owner or group ID is not a value supported by this implementation. A hangup or error occurred during a STREAMS open(). The named file is a directory and write access is requested.
[DIGITAL] For NFS file access, if the open() or creat() function fails, errno may also be set to one of the following values: Indicates a server's attempt to handle an NFS request by generating a request to another NFS server, which is not allowed. Indicates a stale NFS file handle. An opened file was deleted by the server or another client; a client cannot open a file because the server has unmounted or unexported the remote directory; or the directory that contains an opened file was unmounted or unexported by the server. Indicates the connection timed out. For files that are mounted with the soft option, either the server is down or there is a network problem.
Functions: chmod(2), close(2), fcntl(2), getmsg(2), lseek(2), putmsg(2), read(2), stat(2), truncate(2), umask(2), write(2), lockf(3)
Standards: standards(5) delim off