5479 Need missing *at family manual pages
[illumos-gate.git] / usr / src / man / man2 / open.2
blob0978f5a3e5260ffbfbe8c7d02212c5f7e13d1f9d
1 '\" te
2 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
5 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.  All Rights Reserved.
6 .\" 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
7 .\" http://www.opengroup.org/bookstore/.
8 .\" 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.
9 .\"  This notice shall appear on any product containing this material.
10 .\" 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.
11 .\" 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.
12 .\" 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]
13 .TH OPEN 2 "Dec 23, 2014"
14 .SH NAME
15 open, openat \- open a file
16 .SH SYNOPSIS
17 .LP
18 .nf
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
23 \fBint\fR \fBopen\fR(\fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR, \fB/* mode_t\fR \fImode\fR */);
24 .fi
26 .LP
27 .nf
28 \fBint\fR \fBopenat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR,
29      \fB/* mode_t\fR \fImode\fR */);
30 .fi
32 .SH DESCRIPTION
33 .LP
34 The \fBopen()\fR function establishes the connection between a file and a file
35 descriptor. It creates an open file description that refers to a file and a
36 file descriptor that refers to that open file description. The file descriptor
37 is used by other I/O functions to refer to that file. The \fIpath\fR argument
38 points to a pathname naming the file.
39 .sp
40 .LP
41 The \fBopenat()\fR function is identical to the \fBopen()\fR function except
42 that the \fIpath\fR argument is interpreted relative to the starting point
43 implied by the \fIfildes\fR argument. If the \fIfildes\fR argument has the
44 special value \fBAT_FDCWD\fR, a relative path argument will be resolved
45 relative to the current working directory. If the \fIpath\fR argument is
46 absolute, the \fIfildes\fR argument is ignored.
47 .sp
48 .LP
49 The \fBopen()\fR function returns a file descriptor for the named file that is
50 the lowest file descriptor not currently open for that process. The open file
51 description is new, and therefore the file descriptor does not share it with
52 any other process in the system. The \fBFD_CLOEXEC\fR file descriptor flag
53 associated with the new file descriptor is cleared.
54 .sp
55 .LP
56 The file offset used to mark the current position within the file is set to the
57 beginning of the file.
58 .sp
59 .LP
60 The file status flags and file access modes of the open file description are
61 set according to the value of \fIoflag\fR. The \fImode\fR argument is used only
62 when \fBO_CREAT\fR is specified (see below.)
63 .sp
64 .LP
65 Values for \fIoflag\fR are constructed by a bitwise-inclusive-OR of flags from
66 the following list, defined in <\fBfcntl.h\fR>. Applications must specify
67 exactly one of the first three values (file access modes) below in the value of
68 \fIoflag\fR:
69 .sp
70 .ne 2
71 .na
72 \fB\fBO_RDONLY\fR\fR
73 .ad
74 .RS 12n
75 Open for reading only.
76 .RE
78 .sp
79 .ne 2
80 .na
81 \fB\fBO_WRONLY\fR\fR
82 .ad
83 .RS 12n
84 Open for writing only.
85 .RE
87 .sp
88 .ne 2
89 .na
90 \fB\fBO_RDWR\fR\fR
91 .ad
92 .RS 12n
93 Open for reading and writing. The result is undefined if this flag is applied
94 to a FIFO.
95 .RE
97 .sp
98 .LP
99 Any combination of the following may be used:
101 .ne 2
103 \fB\fBO_APPEND\fR\fR
105 .sp .6
106 .RS 4n
107 If set, the file offset is set to the end of the file prior to each write.
111 .ne 2
113 \fB\fBO_CREAT\fR\fR
115 .sp .6
116 .RS 4n
117 Create the file if it does not exist. This flag requires that the \fImode\fR
118 argument be specified.
120 If the file exists, this flag has no effect except as noted under \fBO_EXCL\fR
121 below.  Otherwise, the file is created with the user \fBID\fR of the file set
122 to the effective user \fBID\fR of the process. The group \fBID\fR of the file
123 is set to the effective group \fBIDs\fR of the process, or if the \fBS_ISGID\fR
124 bit is set in the directory in which the file is being created, the file's
125 group \fBID\fR is set to the group \fBID\fR of its parent directory.  If the
126 group \fBID\fR of the new file does not match the effective group \fBID\fR or
127 one of the supplementary groups IDs, the \fBS_ISGID\fR bit is cleared. The
128 access permission bits (see \fB<sys/stat.h>\fR) of the file mode are set to the
129 value of \fImode\fR, modified as follows (see \fBcreat\fR(2)): a bitwise-AND is
130 performed on the file-mode bits and the corresponding bits in the complement of
131 the process's file mode creation mask. Thus, all bits set in the process's file
132 mode creation mask (see \fBumask\fR(2)) are correspondingly cleared in the
133 file's permission mask. The "save text image after execution bit" of the mode
134 is cleared (see \fBchmod\fR(2)). When bits other than the file permission bits
135 are set, the effect is unspecified. The \fImode\fR argument does not affect
136 whether the file is open for reading, writing or for both.
140 .ne 2
142 \fB\fBO_DSYNC\fR\fR
144 .sp .6
145 .RS 4n
146 Write I/O operations on the file descriptor complete as defined by synchronized
147 I/O data integrity completion.
151 .ne 2
153 \fB\fBO_EXCL\fR\fR
155 .sp .6
156 .RS 4n
157 If \fBO_CREAT\fR and \fBO_EXCL\fR are set, \fBopen()\fR fails if the file
158 exists. The check for the existence of the file and the creation of the file if
159 it does not exist is atomic with respect to other threads executing
160 \fBopen()\fR naming the same filename in the same directory with \fBO_EXCL\fR
161 and \fBO_CREAT\fR set. If \fBO_EXCL\fR and \fBO_CREAT\fR are set, and path
162 names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to \fBEEXIST\fR,
163 regardless of the contents of the symbolic link. If \fBO_EXCL\fR is set and
164 \fBO_CREAT\fR is not set, the result is undefined.
168 .ne 2
170 .B O_EXEC
173 .sp .6
174 .RS 4n
175 If set, indicates that the file should be opened for execute permission.
176 This option is only valid for regular files, an error will be returned
177 if it is not.
181 .ne 2
183 \fB\fBO_LARGEFILE\fR\fR
185 .sp .6
186 .RS 4n
187 If set, the offset maximum in the open file description is the largest value
188 that can be represented correctly in an object of type \fBoff64_t\fR.
192 .ne 2
194 \fB\fBO_NOCTTY\fR\fR
196 .sp .6
197 .RS 4n
198 If set and \fIpath\fR identifies a terminal device, \fBopen()\fR does not cause
199 the terminal device to become the controlling terminal for the process.
203 .ne 2
205 \fB\fBO_NOFOLLOW\fR\fR
207 .sp .6
208 .RS 4n
209 If the path names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to
210 \fBELOOP\fR.
214 .ne 2
216 \fB\fBO_NOLINKS\fR\fR
218 .sp .6
219 .RS 4n
220 If the link count of the named file is greater than 1, \fBopen()\fR fails and
221 sets \fBerrno\fR to \fBEMLINK\fR.
225 .ne 2
227 \fB\fBO_CLOEXEC\fR\fR
229 .sp .6
230 .RS 4n
231 If set, the file descriptor returned will be closed prior to any future
232 \fBexec()\fR calls.
236 .ne 2
238 \fB\fBO_NONBLOCK\fR or \fBO_NDELAY\fR\fR
240 .sp .6
241 .RS 4n
242 These flags can affect subsequent reads and writes (see \fBread\fR(2) and
243 \fBwrite\fR(2)). If both \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are set,
244 \fBO_NONBLOCK\fR takes precedence.
246 When opening a \fBFIFO\fR with \fBO_RDONLY\fR or \fBO_WRONLY\fR set:
247 .RS +4
249 .ie t \(bu
250 .el o
251 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, an \fBopen()\fR for reading only
252 returns without delay.  An \fBopen()\fR for writing only returns an error if no
253 process currently has the file open for reading.
255 .RS +4
257 .ie t \(bu
258 .el o
259 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, an \fBopen()\fR for reading
260 only blocks until a thread opens the file for writing. An \fBopen()\fR for
261 writing only blocks the calling thread until a thread opens the file for
262 reading.
264 After both ends of a \fBFIFO\fR have been opened, there is no guarantee that
265 further calls to \fBopen()\fR \fBO_RDONLY\fR (\fBO_WRONLY\fR) will synchronize
266 with later calls to \fBopen()\fR \fBO_WRONLY\fR (\fBO_RDONLY\fR) until both
267 ends of the \fBFIFO\fR have been closed by all readers and writers.  Any data
268 written into a \fBFIFO\fR will be lost if both ends of the \fBFIFO\fR are
269 closed before the data is read.
271 When opening a block special or character special file that supports
272 non-blocking opens:
273 .RS +4
275 .ie t \(bu
276 .el o
277 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, the \fBopen()\fR function returns
278 without blocking for the device to be ready or available. Subsequent behavior
279 of the device is device-specific.
281 .RS +4
283 .ie t \(bu
284 .el o
285 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, the \fBopen()\fR function
286 blocks the calling thread until the device is ready or available before
287 returning.
289 Otherwise, the behavior of \fBO_NONBLOCK\fR and \fBO_NDELAY\fR is unspecified.
293 .ne 2
295 \fB\fBO_RSYNC\fR\fR
297 .sp .6
298 .RS 4n
299 Read I/O operations on the file descriptor complete at the same level of
300 integrity as specified by the \fBO_DSYNC\fR and \fBO_SYNC\fR flags. If both
301 \fBO_DSYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all I/O operations on
302 the file descriptor complete as defined by synchronized I/O data integrity
303 completion.  If both \fBO_SYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all
304 I/O operations on the file descriptor complete as defined by synchronized I/O
305 file integrity completion.
309 .ne 2
311 .B O_SEARCH
313 .sp .6
314 .RS 4n
315 If set, indicates that the directory should be opened for searching.
316 This option is only valid for a directory, an error will be returned if
317 it is not.
321 .ne 2
323 \fB\fBO_SYNC\fR\fR
325 .sp .6
326 .RS 4n
327 Write I/O operations on the file descriptor complete as defined by synchronized
328 I/O file integrity completion (see \fBfcntl.h\fR(3HEAD) definition of
329 \fBO_SYNC\fR).
333 .ne 2
335 \fB\fBO_TRUNC\fR\fR
337 .sp .6
338 .RS 4n
339 If the file exists and is a regular file, and the file is successfully opened
340 \fBO_RDWR\fR or \fBO_WRONLY\fR, its length is truncated to 0 and the mode and
341 owner are unchanged. It has no effect on \fBFIFO\fR special files or terminal
342 device files. Its effect on other file types is implementation-dependent. The
343 result of using \fBO_TRUNC\fR with \fBO_RDONLY\fR is undefined.
347 .ne 2
349 \fB\fBO_XATTR\fR\fR
351 .sp .6
352 .RS 4n
353 If set in \fBopenat()\fR, a relative path argument is interpreted as a
354 reference to an extended attribute of the file associated with the supplied
355 file descriptor.  This flag therefore requires the presence of a legal
356 \fIfildes\fR argument. If set in \fBopen()\fR, the implied file descriptor is
357 that for the current working directory. Extended attributes must be referenced
358 with a relative path; providing an absolute path results in a normal file
359 reference.
364 If \fBO_CREAT\fR is set and the file did not previously exist, upon successful
365 completion, \fBopen()\fR marks for update the \fBst_atime\fR, \fBst_ctime\fR,
366 and \fBst_mtime\fR fields of the file and the \fBst_ctime\fR and \fBst_mtime\fR
367 fields of the parent directory.
370 If \fBO_TRUNC\fR is set and the file did previously exist, upon successful
371 completion, \fBopen()\fR marks for update the \fBst_ctime\fR and \fBst_mtime\fR
372 fields of the file.
375 If both the \fBO_SYNC\fR and \fBO_DSYNC\fR flags are set, the effect is as if
376 only the \fBO_SYNC\fR flag was set.
379 If \fIpath\fR refers to a \fBSTREAMS\fR file, \fIoflag\fR may be constructed
380 from \fBO_NONBLOCK\fR or \fBO_NODELAY\fR OR-ed with either \fBO_RDONLY\fR,
381 \fBO_WRONLY\fR, or \fBO_RDWR\fR. Other flag values are not applicable to
382 \fBSTREAMS\fR devices and have no effect on them.  The values \fBO_NONBLOCK\fR
383 and \fBO_NODELAY\fR affect the operation of \fBSTREAMS\fR drivers and certain
384 functions (see \fBread\fR(2), \fBgetmsg\fR(2), \fBputmsg\fR(2), and
385 \fBwrite\fR(2)) applied to file descriptors associated with \fBSTREAMS\fR
386 files.  For \fBSTREAMS\fR drivers, the implementation of \fBO_NONBLOCK\fR and
387 \fBO_NODELAY\fR is device-specific.
390 When \fBopen()\fR is invoked to open a named stream, and the \fBconnld\fR
391 module (see \fBconnld\fR(7M)) has been pushed on the pipe, \fBopen()\fR blocks
392 until the server process has issued an \fBI_RECVFD\fR \fBioctl()\fR (see
393 \fBstreamio\fR(7I)) to receive the file descriptor.
396 If \fIpath\fR names the master side of a pseudo-terminal device, then it is
397 unspecified whether \fBopen()\fR locks the slave side so that it cannot be
398 opened.  Portable applications must call \fBunlockpt\fR(3C) before opening the
399 slave side.
402 If the file is a regular file and the local file system is mounted with the
403 \fBnbmand\fR mount option, then a mandatory share reservation is automatically
404 obtained on the file. The share reservation is obtained as if \fBfcntl\fR(2)
405 were called with \fIcmd\fR \fBF_SHARE_NBMAND\fR and the \fBfshare_t\fR values
406 set as follows:
408 .ne 2
410 \fB\fBf_access\fR\fR
412 .RS 12n
413 Set to the type of read/write access for which the file is opened.
417 .ne 2
419 \fB\fBf_deny\fR\fR
421 .RS 12n
422 \fBF_NODNY\fR
426 .ne 2
428 \fB\fBf_id\fR\fR
430 .RS 12n
431 The file descriptor value returned from \fBopen()\fR.
436 If \fIpath\fR is a symbolic link and \fBO_CREAT\fR and \fBO_EXCL\fR are set,
437 the link is not followed.
440 Certain flag values can be set following \fBopen()\fR as described in
441 \fBfcntl\fR(2).
444 The largest value that can be represented correctly in an object of type
445 \fBoff_t\fR is established as the offset maximum in the open file description.
446 .SH RETURN VALUES
448 Upon successful completion, the \fBopen()\fR function opens the file and return
449 a non-negative integer representing the lowest numbered unused file descriptor.
450 Otherwise, \fB\(mi1\fR is returned, \fBerrno\fR is set to indicate the error,
451 and no files are created or modified.
452 .SH ERRORS
454 The \fBopen()\fR and \fBopenat()\fR functions will fail if:
456 .ne 2
458 \fB\fBEACCES\fR\fR
460 .RS 16n
461 Search permission is denied on a component of the path prefix.
463 The file exists and the permissions specified by \fIoflag\fR are denied.
465 The file does not exist and write permission is denied for the parent directory
466 of the file to be created.
468 \fBO_TRUNC\fR is specified and write permission is denied.
470 The {\fBPRIV_FILE_DAC_SEARCH\fR} privilege allows processes to search
471 directories regardless of permission bits. The {\fBPRIV_FILE_DAC_WRITE\fR}
472 privilege allows processes to open files for writing regardless of permission
473 bits. See \fBprivileges\fR(5) for special considerations when opening files
474 owned by UID 0 for writing. The {\fBPRIV_FILE_DAC_READ\fR} privilege allows
475 processes to open files for reading regardless of permission bits.
479 .ne 2
481 \fB\fBEAGAIN\fR\fR
483 .RS 16n
484 A mandatory share reservation could not be obtained because the desired access
485 conflicts with an existing \fBf_deny\fR share reservation.
489 .ne 2
491 \fB\fBEBADF\fR\fR
493 .RS 16n
494 The file descriptor provided to \fBopenat()\fR is invalid.
498 .ne 2
500 \fB\fBEDQUOT\fR\fR
502 .RS 16n
503 The file does not exist, \fBO_CREAT\fR is specified, and either the directory
504 where the new file entry is being placed cannot be extended because the user's
505 quota of disk blocks on that file system has been exhausted, or the user's
506 quota of inodes on the file system where the file is being created has been
507 exhausted.
511 .ne 2
513 \fB\fBEEXIST\fR\fR
515 .RS 16n
516 The \fBO_CREAT\fR and \fBO_EXCL\fR flags are set and the named file exists.
520 .ne 2
522 \fB\fBEILSEQ\fR\fR
524 .RS 16n
525 The \fIpath\fR argument includes non-UTF8 characters and the file system
526 accepts only file names where all characters are part of the UTF-8 character
527 codeset.
531 .ne 2
533 \fB\fBEINTR\fR\fR
535 .RS 16n
536 A signal was caught during \fBopen()\fR.
540 .ne 2
542 \fB\fBEFAULT\fR\fR
544 .RS 16n
545 The \fIpath\fR argument points to an illegal address.
549 .ne 2
551 \fB\fBEINVAL\fR\fR
553 .RS 16n
554 The system does not support synchronized I/O for this file, or the
555 \fBO_XATTR\fR flag was supplied and the underlying file system does not support
556 extended file attributes.
560 .ne 2
562 \fB\fBEIO\fR\fR
564 .RS 16n
565 The \fIpath\fR argument names a \fBSTREAMS\fR file and a hangup or error
566 occurred during the \fBopen()\fR.
570 .ne 2
572 \fB\fBEISDIR\fR\fR
574 .RS 16n
575 The named file is a directory and \fIoflag\fR includes \fBO_WRONLY\fR or
576 \fBO_RDWR\fR.
580 .ne 2
582 \fB\fBELOOP\fR\fR
584 .RS 16n
585 Too many symbolic links were encountered in resolving \fIpath\fR.
587 A loop exists in symbolic links encountered during resolution of the \fIpath\fR
588 argument.
590 The \fBO_NOFOLLOW\fR flag is set and the final component of path is a symbolic
591 link.
595 .ne 2
597 \fB\fBEMFILE\fR\fR
599 .RS 16n
600 There are currently {\fBOPEN_MAX\fR} file descriptors open in the calling
601 process.
605 .ne 2
607 \fB\fBEMLINK\fR\fR
609 .RS 16n
610 The \fBO_NOLINKS\fR flag is set and the named file has a link count greater
611 than 1.
615 .ne 2
617 \fB\fBEMULTIHOP\fR\fR
619 .RS 16n
620 Components of \fIpath\fR require hopping to multiple remote machines and the
621 file system does not allow it.
625 .ne 2
627 \fB\fBENAMETOOLONG\fR\fR
629 .RS 16n
630 The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR} or a pathname
631 component is longer than {\fBNAME_MAX\fR}.
635 .ne 2
637 \fB\fBENFILE\fR\fR
639 .RS 16n
640 The maximum allowable number of files is currently open in the system.
644 .ne 2
646 \fB\fBENOENT\fR\fR
648 .RS 16n
649 The \fBO_CREAT\fR flag is not set and the named file does not exist; or the
650 \fBO_CREAT\fR flag is set and either the path prefix does not exist or the
651 \fIpath\fR argument points to an empty string.
655 .ne 2
657 .B ENOEXEC
659 .RS 16n
660 The \fBO_EXEC\fR flag is set and \fIpath\fR does not point to a regular
661 file.
665 .ne 2
667 \fB\fBENOLINK\fR\fR
669 .RS 16n
670 The \fIpath\fR argument points to a remote machine, and the link to that
671 machine is no longer active.
675 .ne 2
677 \fB\fBENOSR\fR\fR
679 .RS 16n
680 The \fIpath\fR argument names a STREAMS-based file and the system is unable to
681 allocate a STREAM.
685 .ne 2
687 \fB\fBENOSPC\fR\fR
689 .RS 16n
690 The directory or file system that would contain the new file cannot be
691 expanded, the file does not exist, and \fBO_CREAT\fR is specified.
695 .ne 2
697 \fB\fBENOSYS\fR\fR
699 .RS 16n
700 The device specified by \fIpath\fR does not support the open operation.
704 .ne 2
706 \fB\fBENOTDIR\fR\fR
708 .RS 16n
709 A component of the path prefix is not a directory or a relative path was
710 supplied to \fBopenat()\fR, the \fBO_XATTR\fR flag was not supplied, and the
711 file descriptor does not refer to a directory. The \fBO_SEARCH\fR flag
712 was passed and \fIpath\fR does not refer to a directory.
716 .ne 2
718 \fB\fBENXIO\fR\fR
720 .RS 16n
721 The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
722 flag is set, and no process has the file open for reading; or the named file is
723 a character special or block special file and the device associated with this
724 special file does not exist or has been retired by the fault management
725 framework .
729 .ne 2
731 \fB\fBEOPNOTSUPP\fR\fR
733 .RS 16n
734 An attempt was made to open a path that corresponds to a \fBAF_UNIX\fR socket.
738 .ne 2
740 \fB\fBEOVERFLOW\fR\fR
742 .RS 16n
743 The named file is a regular file and either \fBO_LARGEFILE\fR is not set and
744 the size of the file cannot be represented correctly in an object of type
745 \fBoff_t\fR or \fBO_LARGEFILE\fR is set and the size of the file cannot be
746 represented correctly in an object of type \fBoff64_t\fR.
750 .ne 2
752 \fB\fBEROFS\fR\fR
754 .RS 16n
755 The named file resides on a read-only file system and either \fBO_WRONLY\fR,
756 \fBO_RDWR\fR, \fBO_CREAT\fR (if file does not exist), or \fBO_TRUNC\fR is set
757 in the \fIoflag\fR argument.
762 The \fBopenat()\fR function will fail if:
764 .ne 2
766 \fB\fBEBADF\fR\fR
768 .RS 9n
769 The \fIfildes\fR argument is not a valid open file descriptor or is not
770 \fBAT_FTCWD\fR.
775 The \fBopen()\fR function may fail if:
777 .ne 2
779 \fB\fBEAGAIN\fR\fR
781 .RS 16n
782 The \fIpath\fR argument names the slave side of a pseudo-terminal device that
783 is locked.
787 .ne 2
789 \fB\fBEINVAL\fR\fR
791 .RS 16n
792 The value of the \fIoflag\fR argument is not valid.
796 .ne 2
798 \fB\fBENAMETOOLONG\fR\fR
800 .RS 16n
801 Pathname resolution of a symbolic link produced an intermediate result whose
802 length exceeds {\fBPATH_MAX\fR}.
806 .ne 2
808 \fB\fBENOMEM\fR\fR
810 .RS 16n
811 The \fIpath\fR argument names a \fBSTREAMS\fR file and the system is unable to
812 allocate resources.
816 .ne 2
818 \fB\fBETXTBSY\fR\fR
820 .RS 16n
821 The file is a pure procedure (shared text) file that is being executed and
822 \fIoflag\fR is \fBO_WRONLY\fR or \fBO_RDWR\fR.
825 .SH EXAMPLES
827 \fBExample 1 \fROpen a file for writing by the owner.
830 The following example opens the file \fB/tmp/file\fR, either by creating it if
831 it does not already exist, or by truncating its length to 0 if it does exist.
832 If the call creates a new file, the access permission bits in the file mode of
833 the file are set to permit reading and writing by the owner, and to permit
834 reading only by group members and others.
838 If the call to \fBopen()\fR is successful, the file is opened for writing.
841 .in +2
843 #include <fcntl.h>
844 \&...
845 int fd;
846 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
847 char *filename = "/tmp/file";
848 \&...
849 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
850 \&...
852 .in -2
855 \fBExample 2 \fROpen a file using an existence check.
858 The following example uses the \fBopen()\fR function to try to create the
859 \fBLOCKFILE\fR file and open it for writing. Since the \fBopen()\fR function
860 specifies the \fBO_EXCL\fR flag, the call fails if the file already exists. In
861 that case, the application assumes that someone else is updating the password
862 file and exits.
865 .in +2
867 #include <fcntl.h>
868 #include <stdio.h>
869 #include <stdlib.h>
870 #define LOCKFILE "/etc/ptmp"
871 \&...
872 int pfd; /* Integer for file descriptor returned by open() call. */
873 \&...
874 if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
875         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
877         fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
878         exit(1);
880 \&...
882 .in -2
885 \fBExample 3 \fROpen a file for writing.
888 The following example opens a file for writing, creating the file if it does
889 not already exist. If the file does exist, the system truncates the file to
890 zero bytes.
893 .in +2
895 #include <fcntl.h>
896 #include <stdio.h>
897 #include <stdlib.h>
898 #define LOCKFILE "/etc/ptmp"
899 \&...
900 int pfd;
901 char filename[PATH_MAX+1];
902 \&...
903 if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
904         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
906         perror("Cannot open output file\en"); exit(1);
908 \&...
910 .in -2
912 .SH USAGE
914 The \fBopen()\fR function has a transitional interface for 64-bit file offsets.
915 See \fBlf64\fR(5). Note that using \fBopen64()\fR is equivalent to using
916 \fBopen()\fR with \fBO_LARGEFILE\fR set in \fIoflag\fR.
917 .SH ATTRIBUTES
919 See \fBattributes\fR(5) for descriptions of the following attributes:
924 box;
925 c | c
926 l | l .
927 ATTRIBUTE TYPE  ATTRIBUTE VALUE
929 Interface Stability     Committed
931 MT-Level        Async-Signal-Safe
933 Standard        For \fBopen()\fR, see \fBstandards\fR(5).
936 .SH SEE ALSO
938 \fBIntro\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2), \fBdup\fR(2),
939 \fBexec\fR(2), \fBfcntl\fR(2), \fBgetmsg\fR(2), \fBgetrlimit\fR(2),
940 \fBlseek\fR(2), \fBputmsg\fR(2), \fBread\fR(2), \fBstat\fR(2), \fBumask\fR(2),
941 \fBwrite\fR(2), \fBattropen\fR(3C), \fBfcntl.h\fR(3HEAD), \fBstat.h\fR(3HEAD),
942 \fBunlockpt\fR(3C), \fBattributes\fR(5), \fBlf64\fR(5), \fBprivileges\fR(5),
943 \fBstandards\fR(5), \fBconnld\fR(7M), \fBstreamio\fR(7I)
944 .SH NOTES
946 Hierarchical Storage Management (HSM) file systems can sometimes cause long
947 delays when opening a file, since HSM files must be recalled from secondary
948 storage.