SysIII/usr/src/man/man2/open.2

Compare this file to the similar file:
Show the results in this format:

.TH OPEN 2 
.SH NAME
open \- open for reading or writing
.SH SYNOPSIS
.B #include <fcntl.h>
.br
.B int open (path, oflag[, mode])
.br
.B char \(**path;
.br
.B int oflag, mode;
.SH DESCRIPTION
.I Path\^
points to a
path name
naming a file.
.I Open\^
opens a file descriptor for the named file
and sets the file status flags
according to the value of
.IR oflag .
.I Oflag\^
values are constructed by or-ing flags
from the following list (only one of the first three flags below
may be used):
.PP
.TP .88i
.SM
.B O_RDONLY
Open for reading only.
.TP
.SM
.B O_WRONLY
Open for writing only.
.TP
.SM
.B O_RDWR
Open for reading and writing.
.TP
.SM
.B O_NDELAY
This flag may affect subsequent reads and writes.
See
.IR read (2)
and
.IR write (2).
.IP
When opening a
.SM FIFO
with
.SM O_RDONLY
or
.SM O_WRONLY
set:
.IP
If
.SM O_NDELAY
is set:
.RS
.IP
An
.I open\^
for reading-only will return without delay.
An
.I open\^
for writing-only will return an error if no process
currently has the file open for reading.
.RE
.IP
If
.SM O_NDELAY
is clear:
.RS
.IP
An
.I open\^
for reading-only will block until a process
opens the file for writing.
An
.I open\^
for writing-only will block until a process
opens the file for reading.
.RE
.IP
When opening a file associated with a communication line:
.IP
If
.SM O_NDELAY
is set:
.RS
.IP
The open will return without waiting for carrier.
.RE
.IP
If
.SM O_NDELAY
is clear:
.RS
.IP
The open will block until carrier is present.
.RE
.TP
.SM
.B O_APPEND
If set, the file pointer will be set to the end of the file
prior to each write.
.TP
.SM
.B O_CREAT
If the file exists, this flag has no effect.
Otherwise,
the file's owner
.SM ID
is set to the process's effective
user
.SM ID\*S,
the file's group
.SM ID
is set to the process's effective group
.SM ID\*S,
and
the low-order 12 bits of the file mode are set to the value of
.I mode\^
modified as follows (see
.IR creat (2)):
.RS
.IP
All bits set in the process's file mode creation mask are cleared.
See
.IR umask (2).
.IP
The ``save text image after execution bit'' of the mode is cleared.
See
.IR chmod (2).
.RE
.TP
.SM
.B O_TRUNC
If the file exists, its length is truncated to 0 and the mode and owner
are unchanged.
.TP
.SM
.B O_EXCL
If
.SM O_EXCL
and
.SM O_CREAT
are set,
.I open\^
will fail if the file exists.
.PP
Upon successful completion a non-negative integer, the
file descriptor,
is returned.
.PP
The file pointer used to mark the current position within the file
is set to the beginning of the file.
.PP
The new file descriptor is set to remain open across
.I exec\^
system calls.
See
.IR fcntl (2).
.PP
No process may have more than
20
file descriptors open simultaneously.
.PP
The named file is opened unless one or more of the following are true:
.IP
A component of the
path prefix
is not a directory.
.SM
\%[ENOTDIR]
.IP
.SM O_CREAT
is not set and the named file does not exist.
.SM
\%[ENOENT]
.IP
A component of the
path prefix
denies search permission.
.SM
\%[EACCES]
.IP
.I Oflag\^
permission is denied for the named file.
.SM
\%[EACCES]
.IP
The named file is a directory and
.I oflag\^
is write or
read/write.
.SM
\%[EISDIR]
.IP
The named file resides on a read-only file system and
.I oflag\^
is write or read/write.
.SM
\%[EROFS]
.IP
Twenty (20)
file descriptors are currently open.
.SM
\%[EMFILE]
.IP
The named file is a character special or block special file,
and the device associated with this special file does not exist.
.SM
\%[ENXIO]
.IP
The file is a pure procedure (shared text) file that is being executed and
.I oflag\^
is write or read/write.
.SM
\%[ETXTBSY]
.IP
.I Path\^
points outside the process's allocated address space.
.SM
\%[EFAULT]
.IP
.SM O_CREAT
and
.SM O_EXCL
are set,
and the named file exists.
.SM
\%[EEXIST]
.IP
.SM O_NDELAY
is set, the named file is a
.SM FIFO\*S,
.SM O_WRONLY
is set, and no process has the file open for reading.
.SM
\%[ENXIO]
.SH "RETURN VALUE"
Upon successful completion,
a non-negative integer,
namely a file descriptor,
is returned.
Otherwise, a value of \-1 is returned and
.I errno\^
is set to indicate the error.
.SH "SEE ALSO"
close(2), creat(2), dup(2), fcntl(2), lseek(2), read(2), write(2).