8999 SMBIOS: cleanup 32-bit specific code
[unleashed.git] / usr / src / man / man3c / fopen.3c
blobfbc07a282f44029d52a462ad77ca2ab184bf1382
1 '\" te
2 .\" Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
3 .\" Copyright 1989 AT&T.
4 .\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
5 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
6 .\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.  All Rights Reserved.
7 .\" Portions Copyright (c) 2014, Joyent, Inc.  All Rights Reserved.
8 .\" 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
9 .\" http://www.opengroup.org/bookstore/.
10 .\" 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.
11 .\"  This notice shall appear on any product containing this material.
12 .\" 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.
13 .\" 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.
14 .\" 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]
15 .TH FOPEN 3C "Nov 24, 2014"
16 .SH NAME
17 fopen \- open a stream
18 .SH SYNOPSIS
19 .LP
20 .nf
21 #include <stdio.h>
23 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
24 .fi
26 .SH DESCRIPTION
27 .LP
28 The \fBfopen()\fR function opens the file whose pathname is the string pointed
29 to by \fIfilename\fR, and associates a stream with it.
30 .sp
31 .LP
32 The argument \fImode\fR points to a string beginning with one of the following
33 base sequences:
34 .sp
35 .ne 2
36 .na
37 \fB\fBr\fR\fR
38 .ad
39 .RS 20n
40 Open file for reading.
41 .RE
43 .sp
44 .ne 2
45 .na
46 \fB\fBw\fR\fR
47 .ad
48 .RS 20n
49 Truncate to  zero  length or create file for writing.
50 .RE
52 .sp
53 .ne 2
54 .na
55 \fB\fBa\fR\fR
56 .ad
57 .RS 20n
58 Append; open or create file for writing at end-of-file.
59 .RE
61 .sp
62 .ne 2
63 .na
64 \fB\fBr+\fR\fR
65 .ad
66 .RS 20n
67 Open file for update (reading and writing).
68 .RE
70 .sp
71 .ne 2
72 .na
73 \fB\fBw+\fR\fR
74 .ad
75 .RS 20n
76 Truncate to zero length or create file for update.
77 .RE
79 .sp
80 .ne 2
81 .na
82 \fB\fBa+\fR\fR
83 .ad
84 .RS 20n
85 Append; open or create file for update, writing at end-of-file.
86 .RE
88 .sp
89 .LP
90 In addition to the base sequences for the \fImode\fR argument above,
91 three additional flags are supported via the \fBb\fR character, the
92 \fBe\fR character, and the \fBx\fR character.  Order of these additional
93 flags (including the \fB+\fR) does not matter.
94 .sp
95 .LP
96 The character \fBb\fR has no effect, but is allowed for ISO C standard
97 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
98 the first character in the \fImode\fR argument) fails if the file does not
99 exist or cannot be read.
102 The character \fBe\fR will cause the underlying file descriptor to be
103 opened with the O_CLOEXEC flag as described in \fBopen\fR(2).
106 The character \fBx\fR will attempt to open the specified file
107 exclusively. This is the same behavior as opening the underlying file
108 with the O_EXCL flag as described in \fBopen\fR(2).
111 Opening a file with append mode (\fBa\fR as the first character in the
112 \fImode\fR argument) causes all subsequent writes to the file to be forced to
113 the then current end-of-file, regardless of intervening calls to
114 \fBfseek\fR(3C). If two separate processes open the same file for append, each
115 process may write freely to the file without fear of destroying output being
116 written by the other.  The output from the two processes will be intermixed in
117 the file in the order in which it is written.
120 When a file is opened with update mode (\fB+\fR as the second or third
121 character in the \fImode\fR argument), both input and output may be performed
122 on the associated stream. However, output must not be directly followed by
123 input without an intervening call to \fBfflush\fR(3C) or to a file positioning
124 function ( \fBfseek\fR(3C), \fBfsetpos\fR(3C) or \fBrewind\fR(3C)), and input
125 must not be directly followed by output without an intervening call to a file
126 positioning function, unless the input operation encounters end-of-file.
129 When opened, a stream is fully buffered if and only if it can be determined not
130 to refer to an interactive device. The error and end-of-file indicators for the
131 stream are cleared.
134 If \fImode\fR begins with \fBw\fR or \fBa\fR and the file did not previously
135 exist, upon successful completion, \fBfopen()\fR function will mark for update
136 the \fBst_atime\fR, \fBst_ctime\fR and \fBst_mtime\fR fields of the file and
137 the \fBst_ctime\fR and \fBst_mtime\fR fields of the parent directory.
140 If \fImode\fR begins with \fBw\fR and the file did previously exist, upon
141 successful completion, \fBfopen()\fR will mark for update the \fBst_ctime\fR
142 and \fBst_mtime\fR fields of the file.  The \fBfopen()\fR function will
143 allocate a file descriptor as \fBopen\fR(2) does.
146 Normally, 32-bit applications return an \fBEMFILE\fR error when attempting to
147 associate a stream with a file accessed by a file descriptor with a value
148 greater than 255. If the last character of \fImode\fR is \fBF\fR, 32-bit
149 applications will be allowed to associate a stream with a file accessed by a
150 file descriptor with a value greater than 255. A \fBFILE\fR pointer obtained in
151 this way must never be used by any code that might directly access fields in
152 the \fBFILE\fR structure. If the fields in the \fBFILE\fR structure are used
153 directly by 32-bit applications when the last character of mode is \fBF\fR,
154 data corruption could occur. See the USAGE section of this manual page and the
155 \fBenable_extended_FILE_stdio\fR(3C) manual page for other options for enabling
156 the extended FILE facility.
159 In 64-bit applications, the last character of \fImode\fR is silently ignored if
160 it is \fBF\fR.  64-bit applications are always allowed to associate a stream
161 with a file accessed by a file descriptor with any value.
164 The largest value that can be represented correctly in an object of type
165 \fBoff_t\fR will be established as the offset maximum in the open file
166 description.
167 .SH RETURN VALUES
169 Upon successful completion, \fBfopen()\fR returns a pointer to the object
170 controlling the stream.  Otherwise, a null pointer is returned and \fBerrno\fR
171 is set to indicate the error.
174 The \fBfopen()\fR function may fail and not set \fBerrno\fR if there are no
175 free \fBstdio\fR streams.
176 .SH ERRORS
178 The \fBfopen()\fR function will fail if:
180 .ne 2
182 \fB\fBEACCES\fR\fR
184 .RS 16n
185 Search permission is denied on a component of the path prefix, or the file
186 exists and the permissions specified by \fImode\fR are denied, or the file does
187 not exist and write permission is denied for the parent directory of the file
188 to be created.
192 .ne 2
194 \fB\fBEINTR\fR\fR
196 .RS 16n
197 A signal was caught during the execution of \fBfopen()\fR.
201 .ne 2
203 \fB\fBEISDIR\fR\fR
205 .RS 16n
206 The named file is a directory and \fImode\fR requires write access.
210 .ne 2
212 \fB\fBELOOP\fR\fR
214 .RS 16n
215 Too many symbolic links were encountered in resolving \fIpath\fR.
219 .ne 2
221 \fB\fBEMFILE\fR\fR
223 .RS 16n
224 There are {\fBOPEN_MAX\fR} file descriptors currently open in the calling
225 process.
229 .ne 2
231 \fB\fBENAMETOOLONG\fR\fR
233 .RS 16n
234 The length of the \fIfilename\fR exceeds \fIPATH_MAX\fR or a pathname component
235 is longer than \fINAME_MAX\fR.
239 .ne 2
241 \fB\fBENFILE\fR\fR
243 .RS 16n
244 The maximum allowable number of files is currently open in the system.
248 .ne 2
250 \fB\fBENOENT\fR\fR
252 .RS 16n
253 A component of \fIfilename\fR does not name an existing file or \fIfilename\fR
254 is an empty string.
258 .ne 2
260 \fB\fBENOSPC\fR\fR
262 .RS 16n
263 The directory or file system that would contain the new file cannot be
264 expanded, the file does not exist, and it was to be created.
268 .ne 2
270 \fB\fBENOTDIR\fR\fR
272 .RS 16n
273 A component of the path prefix is not a directory.
277 .ne 2
279 \fB\fBENXIO\fR\fR
281 .RS 16n
282 The named file is a character special or block special file, and the device
283 associated with this special file does not exist.
287 .ne 2
289 \fB\fBEOVERFLOW\fR\fR
291 .RS 16n
292 The current value of the file position cannot be represented correctly in an
293 object of type \fBfpos_t\fR.
297 .ne 2
299 \fB\fBEROFS\fR\fR
301 .RS 16n
302 The named file resides on a read-only file system and \fImode\fR requires write
303 access.
308 The \fBfopen()\fR function may fail if:
310 .ne 2
312 \fB\fBEINVAL\fR\fR
314 .RS 16n
315 The value of the \fImode\fR argument is not valid.
319 .ne 2
321 \fB\fBEMFILE\fR\fR
323 .RS 16n
324 {\fBFOPEN_MAX\fR} streams are currently open in the calling process.
326 {\fBSTREAM_MAX\fR} streams are currently open in the calling process.
330 .ne 2
332 \fB\fBENAMETOOLONG\fR\fR
334 .RS 16n
335 Pathname resolution of a symbolic link produced an intermediate result whose
336 length exceeds {\fBPATH_MAX\fR}.
340 .ne 2
342 \fB\fBENOMEM\fR\fR
344 .RS 16n
345 Insufficient storage space is available.
349 .ne 2
351 \fB\fBETXTBSY\fR\fR
353 .RS 16n
354 The file is a pure procedure (shared text) file that is being executed and
355 \fImode\fR requires write access.
358 .SH USAGE
360 A process is allowed to have at least {\fBFOPEN_MAX\fR} \fBstdio\fR streams
361 open at a time. For 32-bit applications, however, the underlying ABIs formerly
362 required that no file descriptor used to access the file underlying a
363 \fBstdio\fR stream have a value greater than 255. To maintain binary
364 compatibility with earlier Solaris releases, this limit still constrains 32-bit
365 applications. However, when a 32-bit application is aware that no code that has
366 access to the \fBFILE\fR pointer returned by \fBfopen()\fR will use the
367 \fBFILE\fR pointer to directly access any fields in the \fBFILE\fR structure,
368 the \fBF\fR character can be used as the last character in the \fImode\fR
369 argument to circumvent this limit. Because it could lead to data corruption,
370 the \fBF\fR character in \fImode\fR must never be used when the \fBFILE\fR
371 pointer might later be used by binary code unknown to the user. The \fBF\fR
372 character in \fImode\fR is intended to be used by library functions that need a
373 \fBFILE\fR pointer to access data to process a user request, but do not need to
374 pass the \fBFILE\fR pointer back to the user. 32-bit applications that have
375 been inspected can use the extended FILE facility to circumvent this limit if
376 the inspection shows that no \fBFILE\fR pointers will be used to directly
377 access \fBFILE\fR structure contents.
380 The \fBfopen()\fR function has a transitional interface for 64-bit file
381 offsets.  See \fBlf64\fR(5).
382 .SH ATTRIBUTES
384 See \fBattributes\fR(5) for descriptions of the following attributes:
389 box;
390 c | c
391 l | l .
392 ATTRIBUTE TYPE  ATTRIBUTE VALUE
394 Interface Stability     See below.
396 MT-Level        MT-Safe
401 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
402 respects this function is Standard.
403 .SH SEE ALSO
405 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C),
406 \fBfcloseall\fR(3C), \fBfdopen\fR(3C), \fBfflush\fR(3C),
407 \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C), \fBopen\fR(2),
408 \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)