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