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