1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.Dd September 1, 2023 33.Dt FOPEN 3 34.Os 35.Sh NAME 36.Nm fopen , 37.Nm fdopen , 38.Nm freopen , 39.Nm fmemopen 40.Nd stream open functions 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In stdio.h 45.Ft FILE * 46.Fn fopen "const char * restrict path" "const char * restrict mode" 47.Ft FILE * 48.Fn fdopen "int fildes" "const char *mode" 49.Ft FILE * 50.Fn freopen "const char *path" "const char *mode" "FILE *stream" 51.Ft FILE * 52.Fn fmemopen "void * restrict buf" "size_t size" "const char * restrict mode" 53.Sh DESCRIPTION 54The 55.Fn fopen 56function 57opens the file whose name is the string pointed to by 58.Fa path 59and associates a stream with it. 60.Pp 61The argument 62.Fa mode 63points to a string beginning with one of the following letters: 64.Bl -tag -width indent 65.It Dq Li r 66Open for reading. 67The stream is positioned at the beginning of the file. 68Fail if the file does not exist. 69.It Dq Li w 70Open for writing. 71The stream is positioned at the beginning of the file. 72Truncate the file to zero length if it exists or create the file if it does not exist. 73.It Dq Li a 74Open for writing. 75The stream is positioned at the end of the file. 76Subsequent writes to the file will always end up at the then current 77end of file, irrespective of any intervening 78.Xr fseek 3 79or similar. 80Create the file if it does not exist. 81.El 82.Pp 83An optional 84.Dq Li + 85following 86.Dq Li r , 87.Dq Li w , 88or 89.Dq Li a 90opens the file for both reading and writing. 91An optional 92.Dq Li x 93following 94.Dq Li w 95or 96.Dq Li w+ 97causes the 98.Fn fopen 99call to fail if the file already exists. 100An optional 101.Dq Li e 102following the above 103causes the 104.Fn fopen 105call to set the 106.Dv FD_CLOEXEC 107flag on the underlying file descriptor. 108.Pp 109The 110.Fa mode 111string can also include the letter 112.Dq Li b 113after either the 114.Dq Li + 115or the first letter. 116This is strictly for compatibility with 117.St -isoC 118and has effect only for 119.Fn fmemopen ; 120otherwise 121.Dq Li b 122is ignored. 123.Pp 124Any created files will have mode 125.Do Dv S_IRUSR 126\&| 127.Dv S_IWUSR 128\&| 129.Dv S_IRGRP 130\&| 131.Dv S_IWGRP 132\&| 133.Dv S_IROTH 134\&| 135.Dv S_IWOTH Dc 136.Pq Li 0666 , 137as modified by the process' 138umask value (see 139.Xr umask 2 ) . 140.Pp 141Reads and writes may be intermixed on read/write streams in any order, 142and do not require an intermediate seek as in previous versions of 143.Em stdio . 144This is not portable to other systems, however; 145.St -isoC 146and 147.St -p1003.1 148both require that 149a file positioning function intervene between output and input, unless 150an input operation encounters end-of-file. 151.Pp 152The 153.Fn fdopen 154function associates a stream with the existing file descriptor, 155.Fa fildes . 156The mode 157of the stream must be compatible with the mode of the file descriptor. 158The 159.Dq Li x 160mode option is ignored. 161If the 162.Dq Li e 163mode option is present, the 164.Dv FD_CLOEXEC 165flag is set, otherwise it remains unchanged. 166When the stream is closed via 167.Xr fclose 3 , 168.Fa fildes 169is closed also. 170.Pp 171The 172.Fn freopen 173function 174opens the file whose name is the string pointed to by 175.Fa path 176and associates the stream pointed to by 177.Fa stream 178with it. 179The original stream (if it exists) is closed. 180The 181.Fa mode 182argument is used just as in the 183.Fn fopen 184function. 185.Pp 186If the 187.Fa path 188argument is 189.Dv NULL , 190.Fn freopen 191attempts to re-open the file associated with 192.Fa stream 193with a new mode. 194The new mode must be compatible with the mode that the stream was originally 195opened with: 196Streams open for reading can only be re-opened for reading, 197streams open for writing can only be re-opened for writing, 198and streams open for reading and writing can be re-opened in any mode. 199The 200.Dq Li x 201mode option is not meaningful in this context. 202.Pp 203The primary use of the 204.Fn freopen 205function 206is to change the file associated with a 207standard text stream 208.Dv ( stderr , stdin , 209or 210.Dv stdout ) . 211.Pp 212The 213.Fn fmemopen 214function 215associates the buffer given by the 216.Fa buf 217and 218.Fa size 219arguments with a stream. 220The 221.Fa buf 222argument is either a null pointer or point to a buffer that 223is at least 224.Fa size 225bytes long. 226If a null pointer is specified as the 227.Fa buf 228argument, 229.Fn fmemopen 230allocates 231.Fa size 232bytes of memory. 233This buffer is automatically freed when the stream is closed. 234Buffers can be opened in text-mode (default) or binary-mode 235(if 236.Dq Li b 237is present in the second or third position of the 238.Fa mode 239argument). 240Buffers opened in text-mode make sure that writes are terminated with a 241.Dv NULL 242byte, if the last write hasn't filled up the whole buffer. 243Buffers opened in binary-mode never append a 244.Dv NULL 245byte. 246.Sh RETURN VALUES 247Upon successful completion 248.Fn fopen , 249.Fn fdopen , 250.Fn freopen 251and 252.Fn fmemopen 253return a 254.Tn FILE 255pointer. 256Otherwise, 257.Dv NULL 258is returned and the global variable 259.Va errno 260is set to indicate the error. 261.Sh ERRORS 262.Bl -tag -width Er 263.It Bq Er EINVAL 264The 265.Fa mode 266argument 267to 268.Fn fopen , 269.Fn fdopen , 270.Fn freopen , 271or 272.Fn fmemopen 273was invalid. 274.El 275.Pp 276The 277.Fn fopen , 278.Fn fdopen , 279.Fn freopen 280and 281.Fn fmemopen 282functions 283may also fail and set 284.Va errno 285for any of the errors specified for the routine 286.Xr malloc 3 . 287.Pp 288The 289.Fn fopen 290function 291may also fail and set 292.Va errno 293for any of the errors specified for the routine 294.Xr open 2 . 295.Pp 296The 297.Fn fdopen 298function 299may also fail and set 300.Va errno 301for any of the errors specified for the routine 302.Xr fcntl 2 . 303.Pp 304The 305.Fn freopen 306function 307may also fail and set 308.Va errno 309for any of the errors specified for the routines 310.Xr open 2 , 311.Xr fclose 3 312and 313.Xr fflush 3 . 314.Pp 315The 316.Fn fmemopen 317function 318may also fail and set 319.Va errno 320if the 321.Fa size 322argument is 0. 323.Sh SEE ALSO 324.Xr open 2 , 325.Xr fclose 3 , 326.Xr fileno 3 , 327.Xr fseek 3 , 328.Xr funopen 3 329.Sh STANDARDS 330The 331.Fn fopen 332and 333.Fn freopen 334functions 335conform to 336.St -isoC , 337with the exception of the 338.Dq Li x 339mode option which conforms to 340.St -isoC-2011 . 341The 342.Fn fdopen 343function 344conforms to 345.St -p1003.1-88 . 346The 347.Dq Li e 348mode option does not conform to any standard 349but is also supported by glibc. 350The 351.Fn fmemopen 352function 353conforms to 354.St -p1003.1-2008 . 355The 356.Dq Li b 357mode does not conform to any standard 358but is also supported by glibc. 359.Sh HISTORY 360An 361.Fn fopen 362function appeared in 363.At v1 . 364