9103 opengroup acknowledgement should be properly formatted in man pages
[unleashed.git] / usr / src / man / man2 / open.2
blob3ad37a86c78f91b9f3824056209e74b01a5d9ed6
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
46 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.
47 .\" All Rights Reserved.
48 .\" Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
49 .\"
50 .TH OPEN 2 "Feb 14, 2015"
51 .SH NAME
52 open, openat \- open a file
53 .SH SYNOPSIS
54 .LP
55 .nf
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
60 \fBint\fR \fBopen\fR(\fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR, \fB/* mode_t\fR \fImode\fR */);
61 .fi
63 .LP
64 .nf
65 \fBint\fR \fBopenat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR,
66      \fB/* mode_t\fR \fImode\fR */);
67 .fi
69 .SH DESCRIPTION
70 .LP
71 The \fBopen()\fR function establishes the connection between a file and a file
72 descriptor. It creates an open file description that refers to a file and a
73 file descriptor that refers to that open file description. The file descriptor
74 is used by other I/O functions to refer to that file. The \fIpath\fR argument
75 points to a pathname naming the file.
76 .sp
77 .LP
78 The \fBopenat()\fR function is identical to the \fBopen()\fR function except
79 that the \fIpath\fR argument is interpreted relative to the starting point
80 implied by the \fIfildes\fR argument. If the \fIfildes\fR argument has the
81 special value \fBAT_FDCWD\fR, a relative path argument will be resolved
82 relative to the current working directory. If the \fIpath\fR argument is
83 absolute, the \fIfildes\fR argument is ignored.
84 .sp
85 .LP
86 The \fBopen()\fR function returns a file descriptor for the named file that is
87 the lowest file descriptor not currently open for that process. The open file
88 description is new, and therefore the file descriptor does not share it with
89 any other process in the system. The \fBFD_CLOEXEC\fR file descriptor flag
90 associated with the new file descriptor is cleared.
91 .sp
92 .LP
93 The file offset used to mark the current position within the file is set to the
94 beginning of the file.
95 .sp
96 .LP
97 The file status flags and file access modes of the open file description are
98 set according to the value of \fIoflag\fR. The \fImode\fR argument is used only
99 when \fBO_CREAT\fR is specified (see below.)
102 Values for \fIoflag\fR are constructed by a bitwise-inclusive-OR of flags from
103 the following list, defined in <\fBfcntl.h\fR>. Applications must specify
104 exactly one of the first three values (file access modes) below in the value of
105 \fIoflag\fR:
107 .ne 2
109 \fB\fBO_RDONLY\fR\fR
111 .RS 12n
112 Open for reading only.
116 .ne 2
118 \fB\fBO_WRONLY\fR\fR
120 .RS 12n
121 Open for writing only.
125 .ne 2
127 \fB\fBO_RDWR\fR\fR
129 .RS 12n
130 Open for reading and writing. The result is undefined if this flag is applied
131 to a FIFO.
136 Any combination of the following may be used:
138 .ne 2
140 \fB\fBO_APPEND\fR\fR
142 .sp .6
143 .RS 4n
144 If set, the file offset is set to the end of the file prior to each write.
148 .ne 2
150 \fB\fBO_CREAT\fR\fR
152 .sp .6
153 .RS 4n
154 Create the file if it does not exist. This flag requires that the \fImode\fR
155 argument be specified.
157 If the file exists, this flag has no effect except as noted under \fBO_EXCL\fR
158 below.  Otherwise, the file is created with the user \fBID\fR of the file set
159 to the effective user \fBID\fR of the process. The group \fBID\fR of the file
160 is set to the effective group \fBIDs\fR of the process, or if the \fBS_ISGID\fR
161 bit is set in the directory in which the file is being created, the file's
162 group \fBID\fR is set to the group \fBID\fR of its parent directory.  If the
163 group \fBID\fR of the new file does not match the effective group \fBID\fR or
164 one of the supplementary groups IDs, the \fBS_ISGID\fR bit is cleared. The
165 access permission bits (see \fB<sys/stat.h>\fR) of the file mode are set to the
166 value of \fImode\fR, modified as follows (see \fBcreat\fR(2)): a bitwise-AND is
167 performed on the file-mode bits and the corresponding bits in the complement of
168 the process's file mode creation mask. Thus, all bits set in the process's file
169 mode creation mask (see \fBumask\fR(2)) are correspondingly cleared in the
170 file's permission mask. The "save text image after execution bit" of the mode
171 is cleared (see \fBchmod\fR(2)). When bits other than the file permission bits
172 are set, the effect is unspecified. The \fImode\fR argument does not affect
173 whether the file is open for reading, writing or for both.
177 .ne 2
179 \fB\fBO_DSYNC\fR\fR
181 .sp .6
182 .RS 4n
183 Write I/O operations on the file descriptor complete as defined by synchronized
184 I/O data integrity completion.
188 .ne 2
190 \fB\fBO_EXCL\fR\fR
192 .sp .6
193 .RS 4n
194 If \fBO_CREAT\fR and \fBO_EXCL\fR are set, \fBopen()\fR fails if the file
195 exists. The check for the existence of the file and the creation of the file if
196 it does not exist is atomic with respect to other threads executing
197 \fBopen()\fR naming the same filename in the same directory with \fBO_EXCL\fR
198 and \fBO_CREAT\fR set. If \fBO_EXCL\fR and \fBO_CREAT\fR are set, and path
199 names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to \fBEEXIST\fR,
200 regardless of the contents of the symbolic link. If \fBO_EXCL\fR is set and
201 \fBO_CREAT\fR is not set, the result is undefined.
205 .ne 2
207 .B O_EXEC
210 .sp .6
211 .RS 4n
212 If set, indicates that the file should be opened for execute permission.
213 This option is only valid for regular files, an error will be returned
214 if it is not.
218 .ne 2
220 \fB\fBO_LARGEFILE\fR\fR
222 .sp .6
223 .RS 4n
224 If set, the offset maximum in the open file description is the largest value
225 that can be represented correctly in an object of type \fBoff64_t\fR.
229 .ne 2
231 \fB\fBO_NOCTTY\fR\fR
233 .sp .6
234 .RS 4n
235 If set and \fIpath\fR identifies a terminal device, \fBopen()\fR does not cause
236 the terminal device to become the controlling terminal for the process.
240 .ne 2
242 \fB\fBO_NOFOLLOW\fR\fR
244 .sp .6
245 .RS 4n
246 If the path names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to
247 \fBELOOP\fR.
251 .ne 2
253 \fB\fBO_NOLINKS\fR\fR
255 .sp .6
256 .RS 4n
257 If the link count of the named file is greater than 1, \fBopen()\fR fails and
258 sets \fBerrno\fR to \fBEMLINK\fR.
262 .ne 2
264 \fB\fBO_CLOEXEC\fR\fR
266 .sp .6
267 .RS 4n
268 If set, the file descriptor returned will be closed prior to any future
269 \fBexec()\fR calls.
273 .ne 2
275 \fB\fBO_NONBLOCK\fR or \fBO_NDELAY\fR\fR
277 .sp .6
278 .RS 4n
279 These flags can affect subsequent reads and writes (see \fBread\fR(2) and
280 \fBwrite\fR(2)). If both \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are set,
281 \fBO_NONBLOCK\fR takes precedence.
283 When opening a \fBFIFO\fR with \fBO_RDONLY\fR or \fBO_WRONLY\fR set:
284 .RS +4
286 .ie t \(bu
287 .el o
288 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, an \fBopen()\fR for reading only
289 returns without delay.  An \fBopen()\fR for writing only returns an error if no
290 process currently has the file open for reading.
292 .RS +4
294 .ie t \(bu
295 .el o
296 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, an \fBopen()\fR for reading
297 only blocks until a thread opens the file for writing. An \fBopen()\fR for
298 writing only blocks the calling thread until a thread opens the file for
299 reading.
301 After both ends of a \fBFIFO\fR have been opened, there is no guarantee that
302 further calls to \fBopen()\fR \fBO_RDONLY\fR (\fBO_WRONLY\fR) will synchronize
303 with later calls to \fBopen()\fR \fBO_WRONLY\fR (\fBO_RDONLY\fR) until both
304 ends of the \fBFIFO\fR have been closed by all readers and writers.  Any data
305 written into a \fBFIFO\fR will be lost if both ends of the \fBFIFO\fR are
306 closed before the data is read.
308 When opening a block special or character special file that supports
309 non-blocking opens:
310 .RS +4
312 .ie t \(bu
313 .el o
314 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, the \fBopen()\fR function returns
315 without blocking for the device to be ready or available. Subsequent behavior
316 of the device is device-specific.
318 .RS +4
320 .ie t \(bu
321 .el o
322 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, the \fBopen()\fR function
323 blocks the calling thread until the device is ready or available before
324 returning.
326 Otherwise, the behavior of \fBO_NONBLOCK\fR and \fBO_NDELAY\fR is unspecified.
330 .ne 2
332 \fB\fBO_RSYNC\fR\fR
334 .sp .6
335 .RS 4n
336 Read I/O operations on the file descriptor complete at the same level of
337 integrity as specified by the \fBO_DSYNC\fR and \fBO_SYNC\fR flags. If both
338 \fBO_DSYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all I/O operations on
339 the file descriptor complete as defined by synchronized I/O data integrity
340 completion.  If both \fBO_SYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all
341 I/O operations on the file descriptor complete as defined by synchronized I/O
342 file integrity completion.
346 .ne 2
348 .B O_SEARCH
350 .sp .6
351 .RS 4n
352 If set, indicates that the directory should be opened for searching.
353 This option is only valid for a directory, an error will be returned if
354 it is not.
358 .ne 2
360 \fB\fBO_SYNC\fR\fR
362 .sp .6
363 .RS 4n
364 Write I/O operations on the file descriptor complete as defined by synchronized
365 I/O file integrity completion (see \fBfcntl.h\fR(3HEAD) definition of
366 \fBO_SYNC\fR).
370 .ne 2
372 \fB\fBO_TRUNC\fR\fR
374 .sp .6
375 .RS 4n
376 If the file exists and is a regular file, and the file is successfully opened
377 \fBO_RDWR\fR or \fBO_WRONLY\fR, its length is truncated to 0 and the mode and
378 owner are unchanged. It has no effect on \fBFIFO\fR special files or terminal
379 device files. Its effect on other file types is implementation-dependent. The
380 result of using \fBO_TRUNC\fR with \fBO_RDONLY\fR is undefined.
384 .ne 2
386 \fB\fBO_XATTR\fR\fR
388 .sp .6
389 .RS 4n
390 If set in \fBopenat()\fR, a relative path argument is interpreted as a
391 reference to an extended attribute of the file associated with the supplied
392 file descriptor.  This flag therefore requires the presence of a legal
393 \fIfildes\fR argument. If set in \fBopen()\fR, the implied file descriptor is
394 that for the current working directory. Extended attributes must be referenced
395 with a relative path; providing an absolute path results in a normal file
396 reference.
401 If \fBO_CREAT\fR is set and the file did not previously exist, upon successful
402 completion, \fBopen()\fR marks for update the \fBst_atime\fR, \fBst_ctime\fR,
403 and \fBst_mtime\fR fields of the file and the \fBst_ctime\fR and \fBst_mtime\fR
404 fields of the parent directory.
407 If \fBO_TRUNC\fR is set and the file did previously exist, upon successful
408 completion, \fBopen()\fR marks for update the \fBst_ctime\fR and \fBst_mtime\fR
409 fields of the file.
412 If both the \fBO_SYNC\fR and \fBO_DSYNC\fR flags are set, the effect is as if
413 only the \fBO_SYNC\fR flag was set.
416 If \fIpath\fR refers to a \fBSTREAMS\fR file, \fIoflag\fR may be constructed
417 from \fBO_NONBLOCK\fR or \fBO_NODELAY\fR OR-ed with either \fBO_RDONLY\fR,
418 \fBO_WRONLY\fR, or \fBO_RDWR\fR. Other flag values are not applicable to
419 \fBSTREAMS\fR devices and have no effect on them.  The values \fBO_NONBLOCK\fR
420 and \fBO_NODELAY\fR affect the operation of \fBSTREAMS\fR drivers and certain
421 functions (see \fBread\fR(2), \fBgetmsg\fR(2), \fBputmsg\fR(2), and
422 \fBwrite\fR(2)) applied to file descriptors associated with \fBSTREAMS\fR
423 files.  For \fBSTREAMS\fR drivers, the implementation of \fBO_NONBLOCK\fR and
424 \fBO_NODELAY\fR is device-specific.
427 When \fBopen()\fR is invoked to open a named stream, and the \fBconnld\fR
428 module (see \fBconnld\fR(7M)) has been pushed on the pipe, \fBopen()\fR blocks
429 until the server process has issued an \fBI_RECVFD\fR \fBioctl()\fR (see
430 \fBstreamio\fR(7I)) to receive the file descriptor.
433 If \fIpath\fR names the master side of a pseudo-terminal device, then it is
434 unspecified whether \fBopen()\fR locks the slave side so that it cannot be
435 opened.  Portable applications must call \fBunlockpt\fR(3C) before opening the
436 slave side.
439 If the file is a regular file and the local file system is mounted with the
440 \fBnbmand\fR mount option, then a mandatory share reservation is automatically
441 obtained on the file. The share reservation is obtained as if \fBfcntl\fR(2)
442 were called with \fIcmd\fR \fBF_SHARE_NBMAND\fR and the \fBfshare_t\fR values
443 set as follows:
445 .ne 2
447 \fB\fBf_access\fR\fR
449 .RS 12n
450 Set to the type of read/write access for which the file is opened.
454 .ne 2
456 \fB\fBf_deny\fR\fR
458 .RS 12n
459 \fBF_NODNY\fR
463 .ne 2
465 \fB\fBf_id\fR\fR
467 .RS 12n
468 The file descriptor value returned from \fBopen()\fR.
473 If \fIpath\fR is a symbolic link and \fBO_CREAT\fR and \fBO_EXCL\fR are set,
474 the link is not followed.
477 Certain flag values can be set following \fBopen()\fR as described in
478 \fBfcntl\fR(2).
481 The largest value that can be represented correctly in an object of type
482 \fBoff_t\fR is established as the offset maximum in the open file description.
483 .SH RETURN VALUES
485 Upon successful completion, both \fBopen()\fR and \fBopenat()\fR functions open
486 the file and return a non-negative integer representing the lowest numbered
487 unused file descriptor.  Otherwise, \fB\(mi1\fR is returned, \fBerrno\fR is set
488 to indicate the error, and no files are created or modified.
489 .SH ERRORS
491 The \fBopen()\fR and \fBopenat()\fR functions will fail if:
493 .ne 2
495 \fB\fBEACCES\fR\fR
497 .RS 16n
498 Search permission is denied on a component of the path prefix.
500 The file exists and the permissions specified by \fIoflag\fR are denied.
502 The file does not exist and write permission is denied for the parent directory
503 of the file to be created.
505 \fBO_TRUNC\fR is specified and write permission is denied.
507 The {\fBPRIV_FILE_DAC_SEARCH\fR} privilege allows processes to search
508 directories regardless of permission bits. The {\fBPRIV_FILE_DAC_WRITE\fR}
509 privilege allows processes to open files for writing regardless of permission
510 bits. See \fBprivileges\fR(5) for special considerations when opening files
511 owned by UID 0 for writing. The {\fBPRIV_FILE_DAC_READ\fR} privilege allows
512 processes to open files for reading regardless of permission bits.
516 .ne 2
518 \fB\fBEAGAIN\fR\fR
520 .RS 16n
521 A mandatory share reservation could not be obtained because the desired access
522 conflicts with an existing \fBf_deny\fR share reservation.
526 .ne 2
528 \fB\fBEBADF\fR\fR
530 .RS 16n
531 The file descriptor provided to \fBopenat()\fR is invalid.
535 .ne 2
537 \fB\fBEDQUOT\fR\fR
539 .RS 16n
540 The file does not exist, \fBO_CREAT\fR is specified, and either the directory
541 where the new file entry is being placed cannot be extended because the user's
542 quota of disk blocks on that file system has been exhausted, or the user's
543 quota of inodes on the file system where the file is being created has been
544 exhausted.
548 .ne 2
550 \fB\fBEEXIST\fR\fR
552 .RS 16n
553 The \fBO_CREAT\fR and \fBO_EXCL\fR flags are set and the named file exists.
557 .ne 2
559 \fB\fBEILSEQ\fR\fR
561 .RS 16n
562 The \fIpath\fR argument includes non-UTF8 characters and the file system
563 accepts only file names where all characters are part of the UTF-8 character
564 codeset.
568 .ne 2
570 \fB\fBEINTR\fR\fR
572 .RS 16n
573 A signal was caught during \fBopen()\fR.
577 .ne 2
579 \fB\fBEFAULT\fR\fR
581 .RS 16n
582 The \fIpath\fR argument points to an illegal address.
586 .ne 2
588 \fB\fBEINVAL\fR\fR
590 .RS 16n
591 The system does not support synchronized I/O for this file, or the
592 \fBO_XATTR\fR flag was supplied and the underlying file system does not support
593 extended file attributes.
597 .ne 2
599 \fB\fBEIO\fR\fR
601 .RS 16n
602 The \fIpath\fR argument names a \fBSTREAMS\fR file and a hangup or error
603 occurred during the \fBopen()\fR.
607 .ne 2
609 \fB\fBEISDIR\fR\fR
611 .RS 16n
612 The named file is a directory and \fIoflag\fR includes \fBO_WRONLY\fR or
613 \fBO_RDWR\fR.
617 .ne 2
619 \fB\fBELOOP\fR\fR
621 .RS 16n
622 Too many symbolic links were encountered in resolving \fIpath\fR.
624 A loop exists in symbolic links encountered during resolution of the \fIpath\fR
625 argument.
627 The \fBO_NOFOLLOW\fR flag is set and the final component of path is a symbolic
628 link.
632 .ne 2
634 \fB\fBEMFILE\fR\fR
636 .RS 16n
637 There are currently {\fBOPEN_MAX\fR} file descriptors open in the calling
638 process.
642 .ne 2
644 \fB\fBEMLINK\fR\fR
646 .RS 16n
647 The \fBO_NOLINKS\fR flag is set and the named file has a link count greater
648 than 1.
652 .ne 2
654 \fB\fBEMULTIHOP\fR\fR
656 .RS 16n
657 Components of \fIpath\fR require hopping to multiple remote machines and the
658 file system does not allow it.
662 .ne 2
664 \fB\fBENAMETOOLONG\fR\fR
666 .RS 16n
667 The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR} or a pathname
668 component is longer than {\fBNAME_MAX\fR}.
672 .ne 2
674 \fB\fBENFILE\fR\fR
676 .RS 16n
677 The maximum allowable number of files is currently open in the system.
681 .ne 2
683 \fB\fBENOENT\fR\fR
685 .RS 16n
686 The \fBO_CREAT\fR flag is not set and the named file does not exist; or the
687 \fBO_CREAT\fR flag is set and either the path prefix does not exist or the
688 \fIpath\fR argument points to an empty string.
692 .ne 2
694 .B ENOEXEC
696 .RS 16n
697 The \fBO_EXEC\fR flag is set and \fIpath\fR does not point to a regular
698 file.
702 .ne 2
704 \fB\fBENOLINK\fR\fR
706 .RS 16n
707 The \fIpath\fR argument points to a remote machine, and the link to that
708 machine is no longer active.
712 .ne 2
714 \fB\fBENOSR\fR\fR
716 .RS 16n
717 The \fIpath\fR argument names a STREAMS-based file and the system is unable to
718 allocate a STREAM.
722 .ne 2
724 \fB\fBENOSPC\fR\fR
726 .RS 16n
727 The directory or file system that would contain the new file cannot be
728 expanded, the file does not exist, and \fBO_CREAT\fR is specified.
732 .ne 2
734 \fB\fBENOSYS\fR\fR
736 .RS 16n
737 The device specified by \fIpath\fR does not support the open operation.
741 .ne 2
743 \fB\fBENOTDIR\fR\fR
745 .RS 16n
746 A component of the path prefix is not a directory or a relative path was
747 supplied to \fBopenat()\fR, the \fBO_XATTR\fR flag was not supplied, and the
748 file descriptor does not refer to a directory. The \fBO_SEARCH\fR flag
749 was passed and \fIpath\fR does not refer to a directory.
753 .ne 2
755 \fB\fBENXIO\fR\fR
757 .RS 16n
758 The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
759 flag is set, and no process has the file open for reading; or the named file is
760 a character special or block special file and the device associated with this
761 special file does not exist or has been retired by the fault management
762 framework .
766 .ne 2
768 \fB\fBEOPNOTSUPP\fR\fR
770 .RS 16n
771 An attempt was made to open a path that corresponds to a \fBAF_UNIX\fR socket.
775 .ne 2
777 \fB\fBEOVERFLOW\fR\fR
779 .RS 16n
780 The named file is a regular file and either \fBO_LARGEFILE\fR is not set and
781 the size of the file cannot be represented correctly in an object of type
782 \fBoff_t\fR or \fBO_LARGEFILE\fR is set and the size of the file cannot be
783 represented correctly in an object of type \fBoff64_t\fR.
787 .ne 2
789 \fB\fBEROFS\fR\fR
791 .RS 16n
792 The named file resides on a read-only file system and either \fBO_WRONLY\fR,
793 \fBO_RDWR\fR, \fBO_CREAT\fR (if file does not exist), or \fBO_TRUNC\fR is set
794 in the \fIoflag\fR argument.
799 The \fBopenat()\fR function will fail if:
801 .ne 2
803 \fB\fBEBADF\fR\fR
805 .RS 9n
806 The \fIfildes\fR argument is not a valid open file descriptor or is not
807 \fBAT_FTCWD\fR.
812 The \fBopen()\fR function may fail if:
814 .ne 2
816 \fB\fBEAGAIN\fR\fR
818 .RS 16n
819 The \fIpath\fR argument names the slave side of a pseudo-terminal device that
820 is locked.
824 .ne 2
826 \fB\fBEINVAL\fR\fR
828 .RS 16n
829 The value of the \fIoflag\fR argument is not valid.
833 .ne 2
835 \fB\fBENAMETOOLONG\fR\fR
837 .RS 16n
838 Pathname resolution of a symbolic link produced an intermediate result whose
839 length exceeds {\fBPATH_MAX\fR}.
843 .ne 2
845 \fB\fBENOMEM\fR\fR
847 .RS 16n
848 The \fIpath\fR argument names a \fBSTREAMS\fR file and the system is unable to
849 allocate resources.
853 .ne 2
855 \fB\fBETXTBSY\fR\fR
857 .RS 16n
858 The file is a pure procedure (shared text) file that is being executed and
859 \fIoflag\fR is \fBO_WRONLY\fR or \fBO_RDWR\fR.
862 .SH EXAMPLES
864 \fBExample 1 \fROpen a file for writing by the owner.
867 The following example opens the file \fB/tmp/file\fR, either by creating it if
868 it does not already exist, or by truncating its length to 0 if it does exist.
869 If the call creates a new file, the access permission bits in the file mode of
870 the file are set to permit reading and writing by the owner, and to permit
871 reading only by group members and others.
875 If the call to \fBopen()\fR is successful, the file is opened for writing.
878 .in +2
880 #include <fcntl.h>
881 \&...
882 int fd;
883 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
884 char *filename = "/tmp/file";
885 \&...
886 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
887 \&...
889 .in -2
892 \fBExample 2 \fROpen a file using an existence check.
895 The following example uses the \fBopen()\fR function to try to create the
896 \fBLOCKFILE\fR file and open it for writing. Since the \fBopen()\fR function
897 specifies the \fBO_EXCL\fR flag, the call fails if the file already exists. In
898 that case, the application assumes that someone else is updating the password
899 file and exits.
902 .in +2
904 #include <fcntl.h>
905 #include <stdio.h>
906 #include <stdlib.h>
907 #define LOCKFILE "/etc/ptmp"
908 \&...
909 int pfd; /* Integer for file descriptor returned by open() call. */
910 \&...
911 if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
912         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
914         fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
915         exit(1);
917 \&...
919 .in -2
922 \fBExample 3 \fROpen a file for writing.
925 The following example opens a file for writing, creating the file if it does
926 not already exist. If the file does exist, the system truncates the file to
927 zero bytes.
930 .in +2
932 #include <fcntl.h>
933 #include <stdio.h>
934 #include <stdlib.h>
935 #define LOCKFILE "/etc/ptmp"
936 \&...
937 int pfd;
938 char filename[PATH_MAX+1];
939 \&...
940 if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
941         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
943         perror("Cannot open output file\en"); exit(1);
945 \&...
947 .in -2
949 .SH USAGE
951 The \fBopen()\fR function has a transitional interface for 64-bit file offsets.
952 See \fBlf64\fR(5). Note that using \fBopen64()\fR is equivalent to using
953 \fBopen()\fR with \fBO_LARGEFILE\fR set in \fIoflag\fR.
954 .SH ATTRIBUTES
956 See \fBattributes\fR(5) for descriptions of the following attributes:
961 box;
962 c | c
963 l | l .
964 ATTRIBUTE TYPE  ATTRIBUTE VALUE
966 Interface Stability     Committed
968 MT-Level        Async-Signal-Safe
970 Standard        For \fBopen()\fR, see \fBstandards\fR(5).
973 .SH SEE ALSO
975 \fBIntro\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2), \fBdup\fR(2),
976 \fBexec\fR(2), \fBfcntl\fR(2), \fBgetmsg\fR(2), \fBgetrlimit\fR(2),
977 \fBlseek\fR(2), \fBputmsg\fR(2), \fBread\fR(2), \fBstat\fR(2), \fBumask\fR(2),
978 \fBwrite\fR(2), \fBattropen\fR(3C), \fBfcntl.h\fR(3HEAD), \fBstat.h\fR(3HEAD),
979 \fBunlockpt\fR(3C), \fBattributes\fR(5), \fBlf64\fR(5), \fBprivileges\fR(5),
980 \fBstandards\fR(5), \fBconnld\fR(7M), \fBstreamio\fR(7I)
981 .SH NOTES
983 Hierarchical Storage Management (HSM) file systems can sometimes cause long
984 delays when opening a file, since HSM files must be recalled from secondary
985 storage.