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 .\" 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
6 .\" http://www.opengroup.org/bookstore/.
7 .\" 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.
8 .\" This notice shall appear on any product containing this material.
9 .\" 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.
10 .\" 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.
11 .\" 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]
12 .TH FOPEN 3C "Apr 18, 2006"
13 .SH NAME
14 fopen \- open a stream
15 .SH SYNOPSIS
16 .LP
17 .nf
18 #include <stdio.h>
19
20 \fBFILE *\fR\fBfopen\fR(\fBconst char *\fR\fIfilename\fR, \fBconst char *\fR\fImode\fR);
21 .fi
22
23 .SH DESCRIPTION
24 .sp
25 .LP
26 The \fBfopen()\fR function opens the file whose pathname is the string pointed
27 to by \fIfilename\fR, and associates a stream with it.
28 .sp
29 .LP
30 The argument \fImode\fR points to a string beginning with one of the following
31 sequences:
32 .sp
33 .ne 2
34 .na
35 \fB\fBr\fR or \fBrb\fR\fR
36 .ad
37 .RS 20n
38 Open file for reading.
39 .RE
40
41 .sp
42 .ne 2
43 .na
44 \fB\fBw\fR or \fBwb\fR\fR
45 .ad
46 .RS 20n
47 Truncate to zero length or create file for writing.
48 .RE
49
50 .sp
51 .ne 2
52 .na
53 \fB\fBa\fR or \fBab\fR\fR
54 .ad
55 .RS 20n
56 Append; open or create file for writing at end-of-file.
57 .RE
58
59 .sp
60 .ne 2
61 .na
62 \fB\fBr+\fR or \fBrb+\fR or \fBr+b\fR\fR
63 .ad
64 .RS 20n
65 Open file for update (reading and writing).
66 .RE
67
68 .sp
69 .ne 2
70 .na
71 \fB\fBw+\fR or \fBwb+\fR or \fBw+b\fR\fR
72 .ad
73 .RS 20n
74 Truncate to zero length or create file for update.
75 .RE
76
77 .sp
78 .ne 2
79 .na
80 \fB\fBa+\fR or \fBab+\fR or \fBa+b\fR\fR
81 .ad
82 .RS 20n
83 Append; open or create file for update, writing at end-of-file.
84 .RE
85
86 .sp
87 .LP
88 The character \fBb\fR has no effect, but is allowed for ISO C standard
89 conformance (see \fBstandards\fR(5)). Opening a file with read mode (\fBr\fR as
90 the first character in the \fImode\fR argument) fails if the file does not
91 exist or cannot be read.
92 .sp
93 .LP
94 Opening a file with append mode (\fBa\fR as the first character in the
95 \fImode\fR argument) causes all subsequent writes to the file to be forced to
96 the then current end-of-file, regardless of intervening calls to
97 \fBfseek\fR(3C). If two separate processes open the same file for append, each
98 process may write freely to the file without fear of destroying output being
99 written by the other. The output from the two processes will be intermixed in
100 the file in the order in which it is written.
101 .sp
102 .LP
103 When a file is opened with update mode (\fB+\fR as the second or third
104 character in the \fImode\fR argument), both input and output may be performed
105 on the associated stream. However, output must not be directly followed by
106 input without an intervening call to \fBfflush\fR(3C) or to a file positioning
107 function ( \fBfseek\fR(3C), \fBfsetpos\fR(3C) or \fBrewind\fR(3C)), and input
108 must not be directly followed by output without an intervening call to a file
109 positioning function, unless the input operation encounters end-of-file.
110 .sp
111 .LP
112 When opened, a stream is fully buffered if and only if it can be determined not
113 to refer to an interactive device. The error and end-of-file indicators for the
114 stream are cleared.
115 .sp
116 .LP
117 If \fImode\fR begins with \fIw\fR or \fBa\fR and the file did not previously
118 exist, upon successful completion, \fBfopen()\fR function will mark for update
119 the \fBst_atime\fR, \fBst_ctime\fR and \fBst_mtime\fR fields of the file and
120 the \fBst_ctime\fR and \fBst_mtime\fR fields of the parent directory.
121 .sp
122 .LP
123 If \fImode\fR begins with \fIw\fR and the file did previously exist, upon
124 successful completion, \fBfopen()\fR will mark for update the \fBst_ctime\fR
125 and \fBst_mtime\fR fields of the file. The \fBfopen()\fR function will
126 allocate a file descriptor as \fBopen\fR(2) does.
127 .sp
128 .LP
129 Normally, 32-bit applications return an \fBEMFILE\fR error when attempting to
130 associate a stream with a file accessed by a file descriptor with a value
131 greater than 255. If the last character of \fImode\fR is \fBF\fR, 32-bit
132 applications will be allowed to associate a stream with a file accessed by a
133 file descriptor with a value greater than 255. A \fBFILE\fR pointer obtained in
134 this way must never be used by any code that might directly access fields in
135 the \fBFILE\fR structure. If the fields in the \fBFILE\fR structure are used
136 directly by 32-bit applications when the last character of mode is \fBF\fR,
137 data corruption could occur. See the USAGE section of this manual page and the
138 \fBenable_extended_FILE_stdio\fR(3C) manual page for other options for enabling
139 the extended FILE facility.
140 .sp
141 .LP
142 In 64-bit applications, the last character of \fImode\fR is silently ignored if
143 it is \fBF\fR. 64-bit applications are always allowed to associate a stream
144 with a file accessed by a file descriptor with any value.
145 .sp
146 .LP
147 The largest value that can be represented correctly in an object of type
148 \fBoff_t\fR will be established as the offset maximum in the open file
149 description.
150 .SH RETURN VALUES
151 .sp
152 .LP
153 Upon successful completion, \fBfopen()\fR returns a pointer to the object
154 controlling the stream. Otherwise, a null pointer is returned and \fBerrno\fR
155 is set to indicate the error.
156 .sp
157 .LP
158 The \fBfopen()\fR function may fail and not set \fBerrno\fR if there are no
159 free \fBstdio\fR streams.
160 .SH ERRORS
161 .sp
162 .LP
163 The \fBfopen()\fR function will fail if:
164 .sp
165 .ne 2
166 .na
167 \fB\fBEACCES\fR\fR
168 .ad
169 .RS 16n
170 Search permission is denied on a component of the path prefix, or the file
171 exists and the permissions specified by \fImode\fR are denied, or the file does
172 not exist and write permission is denied for the parent directory of the file
173 to be created.
174 .RE
175
176 .sp
177 .ne 2
178 .na
179 \fB\fBEINTR\fR\fR
180 .ad
181 .RS 16n
182 A signal was caught during the execution of \fBfopen()\fR.
183 .RE
184
185 .sp
186 .ne 2
187 .na
188 \fB\fBEISDIR\fR\fR
189 .ad
190 .RS 16n
191 The named file is a directory and \fImode\fR requires write access.
192 .RE
193
194 .sp
195 .ne 2
196 .na
197 \fB\fBELOOP\fR\fR
198 .ad
199 .RS 16n
200 Too many symbolic links were encountered in resolving \fIpath\fR.
201 .RE
202
203 .sp
204 .ne 2
205 .na
206 \fB\fBEMFILE\fR\fR
207 .ad
208 .RS 16n
209 There are {\fBOPEN_MAX\fR} file descriptors currently open in the calling
210 process.
211 .RE
212
213 .sp
214 .ne 2
215 .na
216 \fB\fBENAMETOOLONG\fR\fR
217 .ad
218 .RS 16n
219 The length of the \fIfilename\fR exceeds \fIPATH_MAX\fR or a pathname component
220 is longer than \fINAME_MAX\fR.
221 .RE
222
223 .sp
224 .ne 2
225 .na
226 \fB\fBENFILE\fR\fR
227 .ad
228 .RS 16n
229 The maximum allowable number of files is currently open in the system.
230 .RE
231
232 .sp
233 .ne 2
234 .na
235 \fB\fBENOENT\fR\fR
236 .ad
237 .RS 16n
238 A component of \fIfilename\fR does not name an existing file or \fIfilename\fR
239 is an empty string.
240 .RE
241
242 .sp
243 .ne 2
244 .na
245 \fB\fBENOSPC\fR\fR
246 .ad
247 .RS 16n
248 The directory or file system that would contain the new file cannot be
249 expanded, the file does not exist, and it was to be created.
250 .RE
251
252 .sp
253 .ne 2
254 .na
255 \fB\fBENOTDIR\fR\fR
256 .ad
257 .RS 16n
258 A component of the path prefix is not a directory.
259 .RE
260
261 .sp
262 .ne 2
263 .na
264 \fB\fBENXIO\fR\fR
265 .ad
266 .RS 16n
267 The named file is a character special or block special file, and the device
268 associated with this special file does not exist.
269 .RE
270
271 .sp
272 .ne 2
273 .na
274 \fB\fBEOVERFLOW\fR\fR
275 .ad
276 .RS 16n
277 The current value of the file position cannot be represented correctly in an
278 object of type \fBfpos_t\fR.
279 .RE
280
281 .sp
282 .ne 2
283 .na
284 \fB\fBEROFS\fR\fR
285 .ad
286 .RS 16n
287 The named file resides on a read-only file system and \fImode\fR requires write
288 access.
289 .RE
290
291 .sp
292 .LP
293 The \fBfopen()\fR function may fail if:
294 .sp
295 .ne 2
296 .na
297 \fB\fBEINVAL\fR\fR
298 .ad
299 .RS 16n
300 The value of the \fImode\fR argument is not valid.
301 .RE
302
303 .sp
304 .ne 2
305 .na
306 \fB\fBEMFILE\fR\fR
307 .ad
308 .RS 16n
309 {\fBFOPEN_MAX\fR} streams are currently open in the calling process.
310 .sp
311 {\fBSTREAM_MAX\fR} streams are currently open in the calling process.
312 .RE
313
314 .sp
315 .ne 2
316 .na
317 \fB\fBENAMETOOLONG\fR\fR
318 .ad
319 .RS 16n
320 Pathname resolution of a symbolic link produced an intermediate result whose
321 length exceeds {\fBPATH_MAX\fR}.
322 .RE
323
324 .sp
325 .ne 2
326 .na
327 \fB\fBENOMEM\fR\fR
328 .ad
329 .RS 16n
330 Insufficient storage space is available.
331 .RE
332
333 .sp
334 .ne 2
335 .na
336 \fB\fBETXTBSY\fR\fR
337 .ad
338 .RS 16n
339 The file is a pure procedure (shared text) file that is being executed and
340 \fImode\fR requires write access.
341 .RE
342
343 .SH USAGE
344 .sp
345 .LP
346 A process is allowed to have at least {\fBFOPEN_MAX\fR} \fBstdio\fR streams
347 open at a time. For 32-bit applications, however, the underlying ABIs formerly
348 required that no file descriptor used to access the file underlying a
349 \fBstdio\fR stream have a value greater than 255. To maintain binary
350 compatibility with earlier Solaris releases, this limit still constrains 32-bit
351 applications. However, when a 32-bit application is aware that no code that has
352 access to the \fBFILE\fR pointer returned by \fBfopen()\fR will use the
353 \fBFILE\fR pointer to directly access any fields in the \fBFILE\fR structure,
354 the \fBF\fR character can be used as the last character in the \fImode\fR
355 argument to circumvent this limit. Because it could lead to data corruption,
356 the \fBF\fR character in \fImode\fR must never be used when the \fBFILE\fR
357 pointer might later be used by binary code unknown to the user. The \fBF\fR
358 character in \fImode\fR is intended to be used by library functions that need a
359 \fBFILE\fR pointer to access data to process a user request, but do not need to
360 pass the \fBFILE\fR pointer back to the user. 32-bit applications that have
361 been inspected can use the extended FILE facility to circumvent this limit if
362 the inspection shows that no \fBFILE\fR pointers will be used to directly
363 access \fBFILE\fR structure contents.
364 .sp
365 .LP
366 The \fBfopen()\fR function has a transitional interface for 64-bit file
367 offsets. See \fBlf64\fR(5).
368 .SH ATTRIBUTES
369 .sp
370 .LP
371 See \fBattributes\fR(5) for descriptions of the following attributes:
372 .sp
373
374 .sp
375 .TS
376 box;
377 c | c
378 l | l .
379 ATTRIBUTE TYPE ATTRIBUTE VALUE
380 _
381 Interface Stability See below.
382 _
383 MT-Level MT-Safe
384 .TE
385
386 .sp
387 .LP
388 The \fBF\fR character in the \fImode\fR argument is Evolving. In all other
389 respects this function is Standard.
390 .SH SEE ALSO
391 .sp
392 .LP
393 \fBenable_extended_FILE_stdio\fR(3C), \fBfclose\fR(3C), \fBfdopen\fR(3C),
394 \fBfflush\fR(3C), \fBfreopen\fR(3C), \fBfsetpos\fR(3C), \fBrewind\fR(3C),
395 \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5)