1 '\" te
2 .\" Copyright 1989 AT&T.
3 .\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
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 .\" Portions Copyright (c) 2013, Joyent, Inc. All Rights Reserved.
7 .\" 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
8 .\" http://www.opengroup.org/bookstore/.
9 .\" 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.
10 .\" This notice shall appear on any product containing this material.
11 .\" 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.
12 .\" 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.
13 .\" 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]
14 .TH FOPEN 3C "Nov 06, 2013"
15 .SH NAME
16 fopen \- open a stream
17 .SH SYNOPSIS
18 .LP
19 .nf
20 #include <stdio.h>
21
22 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
23 .fi
24
25 .SH DESCRIPTION
26 .sp
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
42
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
51
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
60
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
69
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
78
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
87
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.
100 .sp
101 .LP
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).
104 .sp
105 .LP
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).
109 .sp
110 .LP
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.
118 .sp
119 .LP
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.
127 .sp
128 .LP
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.
132 .sp
133 .LP
134 If \fImode\fR begins with \fIw\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.
138 .sp
139 .LP
140 If \fImode\fR begins with \fIw\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.
144 .sp
145 .LP
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.
157 .sp
158 .LP
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.
162 .sp
163 .LP
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
168 .sp
169 .LP
170 Upon successful completion, \fBfopen()\fR returns a pointer to the object
171 controlling the stream. Otherwise, a null pointer is returned and \fBerrno\fR
172 is set to indicate the error.
173 .sp
174 .LP
175 The \fBfopen()\fR function may fail and not set \fBerrno\fR if there are no
176 free \fBstdio\fR streams.
177 .SH ERRORS
178 .sp
179 .LP
180 The \fBfopen()\fR function will fail if:
181 .sp
182 .ne 2
183 .na
184 \fB\fBEACCES\fR\fR
185 .ad
186 .RS 16n
187 Search permission is denied on a component of the path prefix, or the file
188 exists and the permissions specified by \fImode\fR are denied, or the file does
189 not exist and write permission is denied for the parent directory of the file
190 to be created.
191 .RE
192
193 .sp
194 .ne 2
195 .na
196 \fB\fBEINTR\fR\fR
197 .ad
198 .RS 16n
199 A signal was caught during the execution of \fBfopen()\fR.
200 .RE
201
202 .sp
203 .ne 2
204 .na
205 \fB\fBEISDIR\fR\fR
206 .ad
207 .RS 16n
208 The named file is a directory and \fImode\fR requires write access.
209 .RE
210
211 .sp
212 .ne 2
213 .na
214 \fB\fBELOOP\fR\fR
215 .ad
216 .RS 16n
217 Too many symbolic links were encountered in resolving \fIpath\fR.
218 .RE
219
220 .sp
221 .ne 2
222 .na
223 \fB\fBEMFILE\fR\fR
224 .ad
225 .RS 16n
226 There are {\fBOPEN_MAX\fR} file descriptors currently open in the calling
227 process.
228 .RE
229
230 .sp
231 .ne 2
232 .na
233 \fB\fBENAMETOOLONG\fR\fR
234 .ad
235 .RS 16n
236 The length of the \fIfilename\fR exceeds \fIPATH_MAX\fR or a pathname component
237 is longer than \fINAME_MAX\fR.
238 .RE
239
240 .sp
241 .ne 2
242 .na
243 \fB\fBENFILE\fR\fR
244 .ad
245 .RS 16n
246 The maximum allowable number of files is currently open in the system.
247 .RE
248
249 .sp
250 .ne 2
251 .na
252 \fB\fBENOENT\fR\fR
253 .ad
254 .RS 16n
255 A component of \fIfilename\fR does not name an existing file or \fIfilename\fR
256 is an empty string.
257 .RE
258
259 .sp
260 .ne 2
261 .na
262 \fB\fBENOSPC\fR\fR
263 .ad
264 .RS 16n
265 The directory or file system that would contain the new file cannot be
266 expanded, the file does not exist, and it was to be created.
267 .RE
268
269 .sp
270 .ne 2
271 .na
272 \fB\fBENOTDIR\fR\fR
273 .ad
274 .RS 16n
275 A component of the path prefix is not a directory.
276 .RE
277
278 .sp
279 .ne 2
280 .na
281 \fB\fBENXIO\fR\fR
282 .ad
283 .RS 16n
284 The named file is a character special or block special file, and the device
285 associated with this special file does not exist.
286 .RE
287
288 .sp
289 .ne 2
290 .na
291 \fB\fBEOVERFLOW\fR\fR
292 .ad
293 .RS 16n
294 The current value of the file position cannot be represented correctly in an
295 object of type \fBfpos_t\fR.
296 .RE
297
298 .sp
299 .ne 2
300 .na
301 \fB\fBEROFS\fR\fR
302 .ad
303 .RS 16n
304 The named file resides on a read-only file system and \fImode\fR requires write
305 access.
306 .RE
307
308 .sp
309 .LP
310 The \fBfopen()\fR function may fail if:
311 .sp
312 .ne 2
313 .na
314 \fB\fBEINVAL\fR\fR
315 .ad
316 .RS 16n
317 The value of the \fImode\fR argument is not valid.
318 .RE
319
320 .sp
321 .ne 2
322 .na
323 \fB\fBEMFILE\fR\fR
324 .ad
325 .RS 16n
326 {\fBFOPEN_MAX\fR} streams are currently open in the calling process.
327 .sp
328 {\fBSTREAM_MAX\fR} streams are currently open in the calling process.
329 .RE
330
331 .sp
332 .ne 2
333 .na
334 \fB\fBENAMETOOLONG\fR\fR
335 .ad
336 .RS 16n
337 Pathname resolution of a symbolic link produced an intermediate result whose
338 length exceeds {\fBPATH_MAX\fR}.
339 .RE
340
341 .sp
342 .ne 2
343 .na
344 \fB\fBENOMEM\fR\fR
345 .ad
346 .RS 16n
347 Insufficient storage space is available.
348 .RE
349
350 .sp
351 .ne 2
352 .na
353 \fB\fBETXTBSY\fR\fR
354 .ad
355 .RS 16n
356 The file is a pure procedure (shared text) file that is being executed and
357 \fImode\fR requires write access.
358 .RE
359
360 .SH USAGE
361 .sp
362 .LP
363 A process is allowed to have at least {\fBFOPEN_MAX\fR} \fBstdio\fR streams
364 open at a time. For 32-bit applications, however, the underlying ABIs formerly
365 required that no file descriptor used to access the file underlying a
366 \fBstdio\fR stream have a value greater than 255. To maintain binary
367 compatibility with earlier Solaris releases, this limit still constrains 32-bit
368 applications. However, when a 32-bit application is aware that no code that has
369 access to the \fBFILE\fR pointer returned by \fBfopen()\fR will use the
370 \fBFILE\fR pointer to directly access any fields in the \fBFILE\fR structure,
371 the \fBF\fR character can be used as the last character in the \fImode\fR
372 argument to circumvent this limit. Because it could lead to data corruption,
373 the \fBF\fR character in \fImode\fR must never be used when the \fBFILE\fR
374 pointer might later be used by binary code unknown to the user. The \fBF\fR
375 character in \fImode\fR is intended to be used by library functions that need a
376 \fBFILE\fR pointer to access data to process a user request, but do not need to
377 pass the \fBFILE\fR pointer back to the user. 32-bit applications that have
378 been inspected can use the extended FILE facility to circumvent this limit if
379 the inspection shows that no \fBFILE\fR pointers will be used to directly
380 access \fBFILE\fR structure contents.
381 .sp
382 .LP
383 The \fBfopen()\fR function has a transitional interface for 64-bit file
384 offsets. See \fBlf64\fR(5).
385 .SH ATTRIBUTES
386 .sp
387 .LP
388 See \fBattributes\fR(5) for descriptions of the following attributes:
389 .sp
390
391 .sp
392 .TS
393 box;
394 c | c
395 l | l .
396 ATTRIBUTE TYPE ATTRIBUTE VALUE
397 _
398 Interface Stability See below.
399 _
400 MT-Level MT-Safe
401 .TE
402
403 .sp
404 .LP
405 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
406 respects this function is Standard.
407 .SH SEE ALSO
408 .sp
409 .LP
410 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C), \fBfdopen\fR(3C),
411 \fBfflush\fR(3C), \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C),
412 \fBopen\fR(2), \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)