1.\" $NetBSD: fopen.3,v 1.31 2015/07/15 19:08:43 christos Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Chris Torek and the American National Standards Committee X3, 8.\" on Information Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd November 14, 2012 37.Dt FOPEN 3 38.Os 39.Sh NAME 40.Nm fopen , 41.Nm fdopen , 42.Nm freopen 43.Nd stream open functions 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.In stdio.h 48.Ft FILE * 49.Fn fopen "const char * restrict path" "const char * restrict mode" 50.Ft FILE * 51.Fn fdopen "int fildes" "const char *mode" 52.Ft FILE * 53.Fn freopen "const char * restrict path" "const char * restrict mode" "FILE * restrict stream" 54.Sh DESCRIPTION 55The 56.Fn fopen 57function 58opens the file whose name is the string pointed to by 59.Fa path 60and associates a stream with it. 61.Pp 62The argument 63.Fa mode 64points to a string beginning with one of the following 65sequences (Additional characters may follow these sequences.): 66.Bl -tag -width 4n 67.It Dq Li a 68Append; open for writing. 69The file is created if it does not exist. 70.It Dq Li a+ 71Append; open for reading and writing. 72The file is created if it does not exist. 73.It Dq Li r 74Open for reading. 75.It Dq Li r+ 76Open for reading and writing. 77.It Dq Li w 78Open for writing. 79Truncate file to zero length or create file. 80.It Dq Li w+ 81Open for reading and writing. 82Truncate file to zero length or create file. 83.El 84.Pp 85Additionally, the 86.Fa mode 87string can also include one of the following letters: 88.Bl -tag -width 4n 89.It Sq b 90The letter 91.Sq b 92may appear either as a last character or as a character between the 93characters in any of the two-character strings described above. 94This is strictly for compatibility with 95.St -ansiC 96and has no effect; the 97.Sq b 98is ignored. 99.It Sq e 100The letter 101.Sq e 102in the mode string sets the close-on-exec flag in the file descriptors of 103the newly opened file files; if the operation fails, 104.Fn fopen 105will fail. 106This is a non 107.St -ansiC 108extension. 109.It Sq f 110The letter 111.Sq f 112in the mode string restricts 113.Fn fopen 114to regular files; if the file opened is not a regular file, 115.Fn fopen 116will fail. 117This is a non 118.St -ansiC 119extension. 120.It Sq x 121The letter 122.Sq x 123in the mode turns on exclusive open mode to the file 124.Pq Dv O_EXCL 125which means that the file will not be created if it already exists. 126.El 127.Pp 128Any created files will have mode 129.Pf \*q Dv S_IRUSR 130\&| 131.Dv S_IWUSR 132\&| 133.Dv S_IRGRP 134\&| 135.Dv S_IWGRP 136\&| 137.Dv S_IROTH 138\&| 139.Dv S_IWOTH Ns \*q 140.Pq Li 0666 , 141as modified by the process' 142.Xr umask 2 143value. 144.Pp 145Opening a file with append mode causes all subsequent writes to it 146to be forced to the then current end of file, regardless of intervening 147repositioning of the stream. 148.Pp 149The 150.Fn fopen 151and 152.Fn freopen 153functions initially position the stream at the start of the file 154unless the file is opened with append mode, 155in which case the stream is initially positioned at the end of the file. 156.\" PR 6072 claims this paragraph is not correct. 157.\" .Pp 158.\" Reads and writes may be intermixed on read/write streams in any order, 159.\" and do not require an intermediate seek as in previous versions of 160.\" .Em stdio . 161.\" This is not portable to other systems, however; 162.\" .Tn ANSI C 163.\" requires that 164.\" a file positioning function intervene between output and input, unless 165.\" an input operation encounters end-of-file. 166.Pp 167The 168.Fn fdopen 169function associates a stream with the existing file descriptor, 170.Fa fildes . 171The 172.Fa mode 173of the stream must be compatible with the mode of the file descriptor. 174The stream is positioned at the file offset of the file descriptor. 175.Pp 176The 177.Fn freopen 178function 179opens the file whose name is the string pointed to by 180.Fa path 181and associates the stream pointed to by 182.Fa stream 183with it. 184The original stream (if it exists) is closed. 185The 186.Fa mode 187argument is used just as in the 188.Fn fopen 189function. 190The primary use of the 191.Fn freopen 192function 193is to change the file associated with a 194standard text stream 195.Pf ( Em stderr , 196.Em stdin , 197or 198.Em stdout ) . 199.Pp 200Input and output against the opened stream will be fully buffered, unless 201it refers to an interactive terminal device, or a different kind of buffering 202is specified in the environment. 203See 204.Xr setvbuf 3 205for additional details. 206.Sh RETURN VALUES 207Upon successful completion 208.Fn fopen , 209.Fn fdopen 210and 211.Fn freopen 212return a 213.Tn FILE 214pointer. 215Otherwise, 216.Dv NULL 217is returned and the global variable 218.Va errno 219is set to indicate the error. 220.Sh ERRORS 221The functions may fail if: 222.Bl -tag -width Er 223.It Bq Er EFTYPE 224The file is not a regular file and the character ``f'' is specified 225in the mode. 226.It Bq Er EINVAL 227The specified 228.Fa mode 229was invalid. 230.El 231.Pp 232The 233.Fn fopen , 234.Fn fdopen 235and 236.Fn freopen 237functions 238may also fail and set 239.Va errno 240for any of the errors specified for the routine 241.Xr malloc 3 . 242.Pp 243The 244.Fn fopen 245function 246may also fail and set 247.Va errno 248for any of the errors specified for the routine 249.Xr open 2 . 250.Pp 251The 252.Fn fdopen 253function 254may also fail and set 255.Va errno 256for any of the errors specified for the routine 257.Xr fcntl 2 . 258.Pp 259The 260.Fn freopen 261function 262may also fail and set 263.Va errno 264for any of the errors specified for the routines 265.Xr open 2 , 266.Xr fclose 3 267and 268.Xr fflush 3 . 269.Sh SEE ALSO 270.Xr open 2 , 271.Xr fclose 3 , 272.Xr fileno 3 , 273.Xr fseek 3 , 274.Xr funopen 3 275.Sh STANDARDS 276The 277.Fn fopen 278and 279.Fn freopen 280functions conform to 281.St -ansiC . 282All three functions are specified in 283.St -p1003.1-2008 . 284.Sh CAVEATS 285Proper code using 286.Fn fdopen 287with error checking should 288.Xr close 2 289.Fa fildes 290in case of failure, and 291.Xr fclose 3 292the resulting FILE * in case of success. 293.Bd -literal 294 FILE *file; 295 int fd; 296 297 if ((file = fdopen(fd, "r")) != NULL) { 298 /* perform operations on the FILE * */ 299 fclose(file); 300 } else { 301 /* failure, report the error */ 302 close(fd); 303 } 304.Ed 305