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