teach manpages about largefile's demise
[unleashed.git] / share / man / man3c / fopen.3c
blob33252fecc4cb7912aff05b822ef334cd14830d63
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) 2006, Sun Microsystems, Inc.  All Rights Reserved.
46 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.
47 .\" All Rights Reserved.
48 .\" Portions Copyright (c) 2014, Joyent, Inc.  All Rights Reserved.
49 .\" Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
50 .\"
51 .TH FOPEN 3C "Nov 24, 2014"
52 .SH NAME
53 fopen \- open a stream
54 .SH SYNOPSIS
55 .LP
56 .nf
57 #include <stdio.h>
59 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
60 .fi
62 .SH DESCRIPTION
63 .LP
64 The \fBfopen()\fR function opens the file whose pathname is the string pointed
65 to by \fIfilename\fR, and associates a stream with it.
66 .sp
67 .LP
68 The argument \fImode\fR points to a string beginning with one of the following
69 base sequences:
70 .sp
71 .ne 2
72 .na
73 \fB\fBr\fR\fR
74 .ad
75 .RS 20n
76 Open file for reading.
77 .RE
79 .sp
80 .ne 2
81 .na
82 \fB\fBw\fR\fR
83 .ad
84 .RS 20n
85 Truncate to  zero  length or create file for writing.
86 .RE
88 .sp
89 .ne 2
90 .na
91 \fB\fBa\fR\fR
92 .ad
93 .RS 20n
94 Append; open or create file for writing at end-of-file.
95 .RE
97 .sp
98 .ne 2
99 .na
100 \fB\fBr+\fR\fR
102 .RS 20n
103 Open file for update (reading and writing).
107 .ne 2
109 \fB\fBw+\fR\fR
111 .RS 20n
112 Truncate to zero length or create file for update.
116 .ne 2
118 \fB\fBa+\fR\fR
120 .RS 20n
121 Append; open or create file for update, writing at end-of-file.
126 In addition to the base sequences for the \fImode\fR argument above,
127 three additional flags are supported via the \fBb\fR character, the
128 \fBe\fR character, and the \fBx\fR character.  Order of these additional
129 flags (including the \fB+\fR) does not matter.
132 The character \fBb\fR has no effect, but is allowed for ISO C standard
133 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
134 the first character in the \fImode\fR argument) fails if the file does not
135 exist or cannot be read.
138 The character \fBe\fR will cause the underlying file descriptor to be
139 opened with the O_CLOEXEC flag as described in \fBopen\fR(2).
142 The character \fBx\fR will attempt to open the specified file
143 exclusively. This is the same behavior as opening the underlying file
144 with the O_EXCL flag as described in \fBopen\fR(2).
147 Opening a file with append mode (\fBa\fR as the first character in the
148 \fImode\fR argument) causes all subsequent writes to the file to be forced to
149 the then current end-of-file, regardless of intervening calls to
150 \fBfseek\fR(3C). If two separate processes open the same file for append, each
151 process may write freely to the file without fear of destroying output being
152 written by the other.  The output from the two processes will be intermixed in
153 the file in the order in which it is written.
156 When a file is opened with update mode (\fB+\fR as the second or third
157 character in the \fImode\fR argument), both input and output may be performed
158 on the associated stream. However, output must not be directly followed by
159 input without an intervening call to \fBfflush\fR(3C) or to a file positioning
160 function ( \fBfseek\fR(3C), \fBfsetpos\fR(3C) or \fBrewind\fR(3C)), and input
161 must not be directly followed by output without an intervening call to a file
162 positioning function, unless the input operation encounters end-of-file.
165 When opened, a stream is fully buffered if and only if it can be determined not
166 to refer to an interactive device. The error and end-of-file indicators for the
167 stream are cleared.
170 If \fImode\fR begins with \fBw\fR or \fBa\fR and the file did not previously
171 exist, upon successful completion, \fBfopen()\fR function will mark for update
172 the \fBst_atime\fR, \fBst_ctime\fR and \fBst_mtime\fR fields of the file and
173 the \fBst_ctime\fR and \fBst_mtime\fR fields of the parent directory.
176 If \fImode\fR begins with \fBw\fR and the file did previously exist, upon
177 successful completion, \fBfopen()\fR will mark for update the \fBst_ctime\fR
178 and \fBst_mtime\fR fields of the file.  The \fBfopen()\fR function will
179 allocate a file descriptor as \fBopen\fR(2) does.
182 Normally, 32-bit applications return an \fBEMFILE\fR error when attempting to
183 associate a stream with a file accessed by a file descriptor with a value
184 greater than 255. If the last character of \fImode\fR is \fBF\fR, 32-bit
185 applications will be allowed to associate a stream with a file accessed by a
186 file descriptor with a value greater than 255. A \fBFILE\fR pointer obtained in
187 this way must never be used by any code that might directly access fields in
188 the \fBFILE\fR structure. If the fields in the \fBFILE\fR structure are used
189 directly by 32-bit applications when the last character of mode is \fBF\fR,
190 data corruption could occur. See the USAGE section of this manual page and the
191 \fBenable_extended_FILE_stdio\fR(3C) manual page for other options for enabling
192 the extended FILE facility.
195 In 64-bit applications, the last character of \fImode\fR is silently ignored if
196 it is \fBF\fR.  64-bit applications are always allowed to associate a stream
197 with a file accessed by a file descriptor with any value.
200 The largest value that can be represented correctly in an object of type
201 \fBoff_t\fR will be established as the offset maximum in the open file
202 description.
203 .SH RETURN VALUES
205 Upon successful completion, \fBfopen()\fR returns a pointer to the object
206 controlling the stream.  Otherwise, a null pointer is returned and \fBerrno\fR
207 is set to indicate the error.
210 The \fBfopen()\fR function may fail and not set \fBerrno\fR if there are no
211 free \fBstdio\fR streams.
212 .SH ERRORS
214 The \fBfopen()\fR function will fail if:
216 .ne 2
218 \fB\fBEACCES\fR\fR
220 .RS 16n
221 Search permission is denied on a component of the path prefix, or the file
222 exists and the permissions specified by \fImode\fR are denied, or the file does
223 not exist and write permission is denied for the parent directory of the file
224 to be created.
228 .ne 2
230 \fB\fBEINTR\fR\fR
232 .RS 16n
233 A signal was caught during the execution of \fBfopen()\fR.
237 .ne 2
239 \fB\fBEISDIR\fR\fR
241 .RS 16n
242 The named file is a directory and \fImode\fR requires write access.
246 .ne 2
248 \fB\fBELOOP\fR\fR
250 .RS 16n
251 Too many symbolic links were encountered in resolving \fIpath\fR.
255 .ne 2
257 \fB\fBEMFILE\fR\fR
259 .RS 16n
260 There are {\fBOPEN_MAX\fR} file descriptors currently open in the calling
261 process.
265 .ne 2
267 \fB\fBENAMETOOLONG\fR\fR
269 .RS 16n
270 The length of the \fIfilename\fR exceeds \fIPATH_MAX\fR or a pathname component
271 is longer than \fINAME_MAX\fR.
275 .ne 2
277 \fB\fBENFILE\fR\fR
279 .RS 16n
280 The maximum allowable number of files is currently open in the system.
284 .ne 2
286 \fB\fBENOENT\fR\fR
288 .RS 16n
289 A component of \fIfilename\fR does not name an existing file or \fIfilename\fR
290 is an empty string.
294 .ne 2
296 \fB\fBENOSPC\fR\fR
298 .RS 16n
299 The directory or file system that would contain the new file cannot be
300 expanded, the file does not exist, and it was to be created.
304 .ne 2
306 \fB\fBENOTDIR\fR\fR
308 .RS 16n
309 A component of the path prefix is not a directory.
313 .ne 2
315 \fB\fBENXIO\fR\fR
317 .RS 16n
318 The named file is a character special or block special file, and the device
319 associated with this special file does not exist.
323 .ne 2
325 \fB\fBEOVERFLOW\fR\fR
327 .RS 16n
328 The current value of the file position cannot be represented correctly in an
329 object of type \fBfpos_t\fR.
333 .ne 2
335 \fB\fBEROFS\fR\fR
337 .RS 16n
338 The named file resides on a read-only file system and \fImode\fR requires write
339 access.
344 The \fBfopen()\fR function may fail if:
346 .ne 2
348 \fB\fBEINVAL\fR\fR
350 .RS 16n
351 The value of the \fImode\fR argument is not valid.
355 .ne 2
357 \fB\fBEMFILE\fR\fR
359 .RS 16n
360 {\fBFOPEN_MAX\fR} streams are currently open in the calling process.
362 {\fBSTREAM_MAX\fR} streams are currently open in the calling process.
366 .ne 2
368 \fB\fBENAMETOOLONG\fR\fR
370 .RS 16n
371 Pathname resolution of a symbolic link produced an intermediate result whose
372 length exceeds {\fBPATH_MAX\fR}.
376 .ne 2
378 \fB\fBENOMEM\fR\fR
380 .RS 16n
381 Insufficient storage space is available.
385 .ne 2
387 \fB\fBETXTBSY\fR\fR
389 .RS 16n
390 The file is a pure procedure (shared text) file that is being executed and
391 \fImode\fR requires write access.
394 .SH USAGE
396 A process is allowed to have at least {\fBFOPEN_MAX\fR} \fBstdio\fR streams
397 open at a time. For 32-bit applications, however, the underlying ABIs formerly
398 required that no file descriptor used to access the file underlying a
399 \fBstdio\fR stream have a value greater than 255. To maintain binary
400 compatibility with earlier Solaris releases, this limit still constrains 32-bit
401 applications. However, when a 32-bit application is aware that no code that has
402 access to the \fBFILE\fR pointer returned by \fBfopen()\fR will use the
403 \fBFILE\fR pointer to directly access any fields in the \fBFILE\fR structure,
404 the \fBF\fR character can be used as the last character in the \fImode\fR
405 argument to circumvent this limit. Because it could lead to data corruption,
406 the \fBF\fR character in \fImode\fR must never be used when the \fBFILE\fR
407 pointer might later be used by binary code unknown to the user. The \fBF\fR
408 character in \fImode\fR is intended to be used by library functions that need a
409 \fBFILE\fR pointer to access data to process a user request, but do not need to
410 pass the \fBFILE\fR pointer back to the user. 32-bit applications that have
411 been inspected can use the extended FILE facility to circumvent this limit if
412 the inspection shows that no \fBFILE\fR pointers will be used to directly
413 access \fBFILE\fR structure contents.
414 .SH ATTRIBUTES
416 See \fBattributes\fR(5) for descriptions of the following attributes:
421 box;
422 c | c
423 l | l .
424 ATTRIBUTE TYPE  ATTRIBUTE VALUE
426 Interface Stability     See below.
428 MT-Level        MT-Safe
433 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
434 respects this function is Standard.
435 .SH SEE ALSO
437 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C),
438 \fBfcloseall\fR(3C), \fBfdopen\fR(3C), \fBfflush\fR(3C),
439 \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C), \fBopen\fR(2),
440 \fBattributes\fR(5), \fBstandards\fR(5)