'\" te
.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
.\"  This notice shall appear on any product containing this material.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH OPEN 2 "Jun 16, 2008"
.SH NAME
open, openat \- open a file
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

\fBint\fR \fBopen\fR(\fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR, \fB/* mode_t\fR \fImode\fR */);
.fi

.LP
.nf
\fBint\fR \fBopenat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR,
     \fB/* mode_t\fR \fImode\fR */);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBopen()\fR function establishes the connection between a file and a file
descriptor. It creates an open file description that refers to a file and a
file descriptor that refers to that open file description. The file descriptor
is used by other I/O functions to refer to that file. The \fIpath\fR argument
points to a pathname naming the file.
.sp
.LP
The \fBopenat()\fR function is identical to the \fBopen()\fR function except
that the \fIpath\fR argument is interpreted relative to the starting point
implied by the \fIfildes\fR argument. If the \fIfildes\fR argument has the
special value \fBAT_FDCWD\fR, a relative path argument will be resolved
relative to the current working directory. If the \fIpath\fR argument is
absolute, the \fIfildes\fR argument is ignored.
.sp
.LP
The \fBopen()\fR function returns a file descriptor for the named file that is
the lowest file descriptor not currently open for that process. The open file
description is new, and therefore the file descriptor does not share it with
any other process in the system. The \fBFD_CLOEXEC\fR file descriptor flag
associated with the new file descriptor is cleared.
.sp
.LP
The file offset used to mark the current position within the file is set to the
beginning of the file.
.sp
.LP
The file status flags and file access modes of the open file description are
set according to the value of \fIoflag\fR. The \fImode\fR argument is used only
when \fBO_CREAT\fR is specified (see below.)
.sp
.LP
Values for \fIoflag\fR are constructed by a bitwise-inclusive-OR of flags from
the following list, defined in <\fBfcntl.h\fR>. Applications must specify
exactly one of the first three values (file access modes) below in the value of
\fIoflag\fR:
.sp
.ne 2
.na
\fB\fBO_RDONLY\fR\fR
.ad
.RS 12n
Open for reading only.
.RE

.sp
.ne 2
.na
\fB\fBO_WRONLY\fR\fR
.ad
.RS 12n
Open for writing only.
.RE

.sp
.ne 2
.na
\fB\fBO_RDWR\fR\fR
.ad
.RS 12n
Open for reading and writing. The result is undefined if this flag is applied
to a FIFO.
.RE

.sp
.LP
Any combination of the following may be used:
.sp
.ne 2
.na
\fB\fBO_APPEND\fR\fR
.ad
.sp .6
.RS 4n
If set, the file offset is set to the end of the file prior to each write.
.RE

.sp
.ne 2
.na
\fB\fBO_CREAT\fR\fR
.ad
.sp .6
.RS 4n
Create the file if it does not exist. This flag requires that the \fImode\fR
argument be specified.
.sp
If the file exists, this flag has no effect except as noted under \fBO_EXCL\fR
below.  Otherwise, the file is created with the user \fBID\fR of the file set
to the effective user \fBID\fR of the process. The group \fBID\fR of the file
is set to the effective group \fBIDs\fR of the process, or if the \fBS_ISGID\fR
bit is set in the directory in which the file is being created, the file's
group \fBID\fR is set to the group \fBID\fR of its parent directory.  If the
group \fBID\fR of the new file does not match the effective group \fBID\fR or
one of the supplementary groups IDs, the \fBS_ISGID\fR bit is cleared. The
access permission bits (see \fB<sys/stat.h>\fR) of the file mode are set to the
value of \fImode\fR, modified as follows (see \fBcreat\fR(2)): a bitwise-AND is
performed on the file-mode bits and the corresponding bits in the complement of
the process's file mode creation mask. Thus, all bits set in the process's file
mode creation mask (see \fBumask\fR(2)) are correspondingly cleared in the
file's permission mask. The "save text image after execution bit" of the mode
is cleared (see \fBchmod\fR(2)). \fBO_SYNC\fR Write I/O operations on the file
descriptor complete as defined by synchronized I/O file integrity completion
(see \fBfcntl.h\fR(3HEAD) definition of \fBO_SYNC\fR.) When bits other than the
file permission bits are set, the effect is unspecified. The \fImode\fR
argument does not affect whether the file is open for reading, writing or for
both.
.RE

.sp
.ne 2
.na
\fB\fBO_DSYNC\fR\fR
.ad
.sp .6
.RS 4n
Write I/O operations on the file descriptor complete as defined by synchronized
I/O data integrity completion.
.RE

.sp
.ne 2
.na
\fB\fBO_EXCL\fR\fR
.ad
.sp .6
.RS 4n
If \fBO_CREAT\fR and \fBO_EXCL\fR are set, \fBopen()\fR fails if the file
exists. The check for the existence of the file and the creation of the file if
it does not exist is atomic with respect to other threads executing
\fBopen()\fR naming the same filename in the same directory with \fBO_EXCL\fR
and \fBO_CREAT\fR set. If \fBO_EXCL\fR and \fBO_CREAT\fR are set, and path
names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to \fBEEXIST\fR,
regardless of the contents of the symbolic link. If \fBO_EXCL\fR is set and
\fBO_CREAT\fR is not set, the result is undefined.
.RE

.sp
.ne 2
.na
\fB\fBO_LARGEFILE\fR\fR
.ad
.sp .6
.RS 4n
If set, the offset maximum in the open file description is the largest value
that can be represented correctly in an object of type \fBoff64_t\fR.
.RE

.sp
.ne 2
.na
\fB\fBO_NOCTTY\fR\fR
.ad
.sp .6
.RS 4n
If set and \fIpath\fR identifies a terminal device, \fBopen()\fR does not cause
the terminal device to become the controlling terminal for the process.
.RE

.sp
.ne 2
.na
\fB\fBO_NOFOLLOW\fR\fR
.ad
.sp .6
.RS 4n
If the path names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to
\fBELOOP\fR.
.RE

.sp
.ne 2
.na
\fB\fBO_NOLINKS\fR\fR
.ad
.sp .6
.RS 4n
If the link count of the named file is greater than 1, \fBopen()\fR fails and
sets \fBerrno\fR to \fBEMLINK\fR.
.RE

.sp
.ne 2
.na
\fB\fBO_NONBLOCK\fR or \fBO_NDELAY\fR\fR
.ad
.sp .6
.RS 4n
These flags can affect subsequent reads and writes (see \fBread\fR(2) and
\fBwrite\fR(2)). If both \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are set,
\fBO_NONBLOCK\fR takes precedence.
.sp
When opening a \fBFIFO\fR with \fBO_RDONLY\fR or \fBO_WRONLY\fR set:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, an \fBopen()\fR for reading only
returns without delay.  An \fBopen()\fR for writing only returns an error if no
process currently has the file open for reading.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, an \fBopen()\fR for reading
only blocks until a thread opens the file for writing. An \fBopen()\fR for
writing only blocks the calling thread until a thread opens the file for
reading.
.RE
After both ends of a \fBFIFO\fR have been opened, there is no guarantee that
further calls to \fBopen()\fR \fBO_RDONLY\fR (\fBO_WRONLY\fR) will synchronize
with later calls to \fBopen()\fR \fBO_WRONLY\fR (\fBO_RDONLY\fR) until both
ends of the \fBFIFO\fR have been closed by all readers and writers.  Any data
written into a \fBFIFO\fR will be lost if both ends of the \fBFIFO\fR are
closed before the data is read.
.sp
When opening a block special or character special file that supports
non-blocking opens:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, the \fBopen()\fR function returns
without blocking for the device to be ready or available. Subsequent behavior
of the device is device-specific.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, the \fBopen()\fR function
blocks the calling thread until the device is ready or available before
returning.
.RE
Otherwise, the behavior of \fBO_NONBLOCK\fR and \fBO_NDELAY\fR is unspecified.
.RE

.sp
.ne 2
.na
\fB\fBO_RSYNC\fR\fR
.ad
.sp .6
.RS 4n
Read I/O operations on the file descriptor complete at the same level of
integrity as specified by the \fBO_DSYNC\fR and \fBO_SYNC\fR flags. If both
\fBO_DSYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all I/O operations on
the file descriptor complete as defined by synchronized I/O data integrity
completion.  If both \fBO_SYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all
I/O operations on the file descriptor complete as defined by synchronized I/O
file integrity completion.
.RE

.sp
.ne 2
.na
\fB\fBO_SYNC\fR\fR
.ad
.sp .6
.RS 4n
Write I/O operations on the file descriptor complete as defined by synchronized
I/O file integrity completion.
.RE

.sp
.ne 2
.na
\fB\fBO_TRUNC\fR\fR
.ad
.sp .6
.RS 4n
If the file exists and is a regular file, and the file is successfully opened
\fBO_RDWR\fR or \fBO_WRONLY\fR, its length is truncated to 0 and the mode and
owner are unchanged. It has no effect on \fBFIFO\fR special files or terminal
device files. Its effect on other file types is implementation-dependent. The
result of using \fBO_TRUNC\fR with \fBO_RDONLY\fR is undefined.
.RE

.sp
.ne 2
.na
\fB\fBO_XATTR\fR\fR
.ad
.sp .6
.RS 4n
If set in \fBopenat()\fR, a relative path argument is interpreted as a
reference to an extended attribute of the file associated with the supplied
file descriptor.  This flag therefore requires the presence of a legal
\fIfildes\fR argument. If set in \fBopen()\fR, the implied file descriptor is
that for the current working directory. Extended attributes must be referenced
with a relative path; providing an absolute path results in a normal file
reference.
.RE

.sp
.LP
If \fBO_CREAT\fR is set and the file did not previously exist, upon successful
completion, \fBopen()\fR marks for update the \fBst_atime\fR, \fBst_ctime\fR,
and \fBst_mtime\fR fields of the file and the \fBst_ctime\fR and \fBst_mtime\fR
fields of the parent directory.
.sp
.LP
If \fBO_TRUNC\fR is set and the file did previously exist, upon successful
completion, \fBopen()\fR marks for update the \fBst_ctime\fR and \fBst_mtime\fR
fields of the file.
.sp
.LP
If both the \fBO_SYNC\fR and \fBO_DSYNC\fR flags are set, the effect is as if
only the \fBO_SYNC\fR flag was set.
.sp
.LP
If \fIpath\fR refers to a \fBSTREAMS\fR file, \fIoflag\fR may be constructed
from \fBO_NONBLOCK\fR or \fBO_NODELAY\fR OR-ed with either \fBO_RDONLY\fR,
\fBO_WRONLY\fR, or \fBO_RDWR\fR. Other flag values are not applicable to
\fBSTREAMS\fR devices and have no effect on them.  The values \fBO_NONBLOCK\fR
and \fBO_NODELAY\fR affect the operation of \fBSTREAMS\fR drivers and certain
functions (see \fBread\fR(2), \fBgetmsg\fR(2), \fBputmsg\fR(2), and
\fBwrite\fR(2)) applied to file descriptors associated with \fBSTREAMS\fR
files.  For \fBSTREAMS\fR drivers, the implementation of \fBO_NONBLOCK\fR and
\fBO_NODELAY\fR is device-specific.
.sp
.LP
When \fBopen()\fR is invoked to open a named stream, and the \fBconnld\fR
module (see \fBconnld\fR(7M)) has been pushed on the pipe, \fBopen()\fR blocks
until the server process has issued an \fBI_RECVFD\fR \fBioctl()\fR (see
\fBstreamio\fR(7I)) to receive the file descriptor.
.sp
.LP
If \fIpath\fR names the master side of a pseudo-terminal device, then it is
unspecified whether \fBopen()\fR locks the slave side so that it cannot be
opened.  Portable applications must call \fBunlockpt\fR(3C) before opening the
slave side.
.sp
.LP
If the file is a regular file and the local file system is mounted with the
\fBnbmand\fR mount option, then a mandatory share reservation is automatically
obtained on the file. The share reservation is obtained as if \fBfcntl\fR(2)
were called with \fIcmd\fR \fBF_SHARE_NBMAND\fR and the \fBfshare_t\fR values
set as follows:
.sp
.ne 2
.na
\fB\fBf_access\fR\fR
.ad
.RS 12n
Set to the type of read/write access for which the file is opened.
.RE

.sp
.ne 2
.na
\fB\fBf_deny\fR\fR
.ad
.RS 12n
\fBF_NODNY\fR
.RE

.sp
.ne 2
.na
\fB\fBf_id\fR\fR
.ad
.RS 12n
The file descriptor value returned from \fBopen()\fR.
.RE

.sp
.LP
If \fIpath\fR is a symbolic link and \fBO_CREAT\fR and \fBO_EXCL\fR are set,
the link is not followed.
.sp
.LP
Certain flag values can be set following \fBopen()\fR as described in
\fBfcntl\fR(2).
.sp
.LP
The largest value that can be represented correctly in an object of type
\fBoff_t\fR is established as the offset maximum in the open file description.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, the \fBopen()\fR function opens the file and return
a non-negative integer representing the lowest numbered unused file descriptor.
Otherwise, \fB\(mi1\fR is returned, \fBerrno\fR is set to indicate the error,
and no files are created or modified.
.SH ERRORS
.sp
.LP
The \fBopen()\fR and \fBopenat()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
Search permission is denied on a component of the path prefix.
.sp
The file exists and the permissions specified by \fIoflag\fR are denied.
.sp
The file does not exist and write permission is denied for the parent directory
of the file to be created.
.sp
\fBO_TRUNC\fR is specified and write permission is denied.
.sp
The {\fBPRIV_FILE_DAC_SEARCH\fR} privilege allows processes to search
directories regardless of permission bits. The {\fBPRIV_FILE_DAC_WRITE\fR}
privilege allows processes to open files for writing regardless of permission
bits. See \fBprivileges\fR(5) for special considerations when opening files
owned by UID 0 for writing. The {\fBPRIV_FILE_DAC_READ\fR} privilege allows
processes to open files for reading regardless of permission bits.
.RE

.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 16n
A mandatory share reservation could not be obtained because the desired access
conflicts with an existing \fBf_deny\fR share reservation.
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 16n
The file descriptor provided to \fBopenat()\fR is invalid.
.RE

.sp
.ne 2
.na
\fB\fBEDQUOT\fR\fR
.ad
.RS 16n
The file does not exist, \fBO_CREAT\fR is specified, and either the directory
where the new file entry is being placed cannot be extended because the user's
quota of disk blocks on that file system has been exhausted, or the user's
quota of inodes on the file system where the file is being created has been
exhausted.
.RE

.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 16n
The \fBO_CREAT\fR and \fBO_EXCL\fR flags are set and the named file exists.
.RE

.sp
.ne 2
.na
\fB\fBEILSEQ\fR\fR
.ad
.RS 16n
The \fIpath\fR argument includes non-UTF8 characters and the file system
accepts only file names where all characters are part of the UTF-8 character
codeset.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
A signal was caught during \fBopen()\fR.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to an illegal address.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The system does not support synchronized I/O for this file, or the
\fBO_XATTR\fR flag was supplied and the underlying file system does not support
extended file attributes.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a \fBSTREAMS\fR file and a hangup or error
occurred during the \fBopen()\fR.
.RE

.sp
.ne 2
.na
\fB\fBEISDIR\fR\fR
.ad
.RS 16n
The named file is a directory and \fIoflag\fR includes \fBO_WRONLY\fR or
\fBO_RDWR\fR.
.RE

.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in resolving \fIpath\fR.
.sp
A loop exists in symbolic links encountered during resolution of the \fIpath\fR
argument.
.sp
The \fBO_NOFOLLOW\fR flag is set and the final component of path is a symbolic
link.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
There are currently {\fBOPEN_MAX\fR} file descriptors open in the calling
process.
.RE

.sp
.ne 2
.na
\fB\fBEMLINK\fR\fR
.ad
.RS 16n
The \fBO_NOLINKS\fR flag is set and the named file has a link count greater
than 1.
.RE

.sp
.ne 2
.na
\fB\fBEMULTIHOP\fR\fR
.ad
.RS 16n
Components of \fIpath\fR require hopping to multiple remote machines and the
file system does not allow it.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR} or a pathname
component is longer than {\fBNAME_MAX\fR}.
.RE

.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
The maximum allowable number of files is currently open in the system.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
The \fBO_CREAT\fR flag is not set and the named file does not exist; or the
\fBO_CREAT\fR flag is set and either the path prefix does not exist or the
\fIpath\fR argument points to an empty string.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine, and the link to that
machine is no longer active.
.RE

.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a STREAMS-based file and the system is unable to
allocate a STREAM.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 16n
The directory or file system that would contain the new file cannot be
expanded, the file does not exist, and \fBO_CREAT\fR is specified.
.RE

.sp
.ne 2
.na
\fB\fBENOSYS\fR\fR
.ad
.RS 16n
The device specified by \fIpath\fR does not support the open operation.
.RE

.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix is not a directory or a relative path was
supplied to \fBopenat()\fR, the \fBO_XATTR\fR flag was not supplied, and the
file descriptor does not refer to a directory.
.RE

.sp
.ne 2
.na
\fB\fBENXIO\fR\fR
.ad
.RS 16n
The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
flag is set, and no process has the file open for reading; or the named file is
a character special or block special file and the device associated with this
special file does not exist or has been retired by the fault management
framework .
.RE

.sp
.ne 2
.na
\fB\fBEOPNOTSUPP\fR\fR
.ad
.RS 16n
An attempt was made to open a path that corresponds to a \fBAF_UNIX\fR socket.
.RE

.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 16n
The named file is a regular file and either \fBO_LARGEFILE\fR is not set and
the size of the file cannot be represented correctly in an object of type
\fBoff_t\fR or \fBO_LARGEFILE\fR is set and the size of the file cannot be
represented correctly in an object of type \fBoff64_t\fR.
.RE

.sp
.ne 2
.na
\fB\fBEROFS\fR\fR
.ad
.RS 16n
The named file resides on a read-only file system and either \fBO_WRONLY\fR,
\fBO_RDWR\fR, \fBO_CREAT\fR (if file does not exist), or \fBO_TRUNC\fR is set
in the \fIoflag\fR argument.
.RE

.sp
.LP
The \fBopenat()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 9n
The \fIfildes\fR argument is not a valid open file descriptor or is not
\fBAT_FTCWD\fR.
.RE

.sp
.LP
The \fBopen()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names the slave side of a pseudo-terminal device that
is locked.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The value of the \fIoflag\fR argument is not valid.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {\fBPATH_MAX\fR}.
.RE

.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a \fBSTREAMS\fR file and the system is unable to
allocate resources.
.RE

.sp
.ne 2
.na
\fB\fBETXTBSY\fR\fR
.ad
.RS 16n
The file is a pure procedure (shared text) file that is being executed and
\fIoflag\fR is \fBO_WRONLY\fR or \fBO_RDWR\fR.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fROpen a file for writing by the owner.
.sp
.LP
The following example opens the file \fB/tmp/file\fR, either by creating it if
it does not already exist, or by truncating its length to 0 if it does exist.
If the call creates a new file, the access permission bits in the file mode of
the file are set to permit reading and writing by the owner, and to permit
reading only by group members and others.

.sp
.LP
If the call to \fBopen()\fR is successful, the file is opened for writing.

.sp
.in +2
.nf
#include <fcntl.h>
\&...
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *filename = "/tmp/file";
\&...
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
\&...
.fi
.in -2

.LP
\fBExample 2 \fROpen a file using an existence check.
.sp
.LP
The following example uses the \fBopen()\fR function to try to create the
\fBLOCKFILE\fR file and open it for writing. Since the \fBopen()\fR function
specifies the \fBO_EXCL\fR flag, the call fails if the file already exists. In
that case, the application assumes that someone else is updating the password
file and exits.

.sp
.in +2
.nf
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define LOCKFILE "/etc/ptmp"
\&...
int pfd; /* Integer for file descriptor returned by open() call. */
\&...
if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
{
        fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
        exit(1);
}
\&...
.fi
.in -2

.LP
\fBExample 3 \fROpen a file for writing.
.sp
.LP
The following example opens a file for writing, creating the file if it does
not already exist. If the file does exist, the system truncates the file to
zero bytes.

.sp
.in +2
.nf
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define LOCKFILE "/etc/ptmp"
\&...
int pfd;
char filename[PATH_MAX+1];
\&...
if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
{
        perror("Cannot open output file\en"); exit(1);
}
\&...
.fi
.in -2

.SH USAGE
.sp
.LP
The \fBopen()\fR function has a transitional interface for 64-bit file offsets.
See \fBlf64\fR(5). Note that using \fBopen64()\fR is equivalent to using
\fBopen()\fR with \fBO_LARGEFILE\fR set in \fIoflag\fR.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	Async-Signal-Safe
_
Standard	For \fBopen()\fR, see \fBstandards\fR(5).
.TE

.SH SEE ALSO
.sp
.LP
\fBIntro\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2), \fBdup\fR(2),
\fBexec\fR(2), \fBfcntl\fR(2), \fBgetmsg\fR(2), \fBgetrlimit\fR(2),
\fBlseek\fR(2), \fBputmsg\fR(2), \fBread\fR(2), \fBstat\fR(2), \fBumask\fR(2),
\fBwrite\fR(2), \fBattropen\fR(3C), \fBfcntl.h\fR(3HEAD), \fBstat.h\fR(3HEAD),
\fBunlockpt\fR(3C), \fBattributes\fR(5), \fBlf64\fR(5), \fBprivileges\fR(5),
\fBstandards\fR(5), \fBconnld\fR(7M), \fBstreamio\fR(7I)
.SH NOTES
.sp
.LP
Hierarchical Storage Management (HSM) file systems can sometimes cause long
delays when opening a file, since HSM files must be recalled from secondary
storage.