1*045ae074Ssevan.\" $NetBSD: fopen.3,v 1.36 2019/09/02 00:32:55 sevan Exp $ 2389c46c5Sjtc.\" 3389c46c5Sjtc.\" Copyright (c) 1990, 1991, 1993 4389c46c5Sjtc.\" The Regents of the University of California. All rights reserved. 561f28255Scgd.\" 661f28255Scgd.\" This code is derived from software contributed to Berkeley by 761f28255Scgd.\" Chris Torek and the American National Standards Committee X3, 861f28255Scgd.\" on Information Processing Systems. 961f28255Scgd.\" 1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without 1161f28255Scgd.\" modification, are permitted provided that the following conditions 1261f28255Scgd.\" are met: 1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright 1461f28255Scgd.\" notice, this list of conditions and the following disclaimer. 1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright 1661f28255Scgd.\" notice, this list of conditions and the following disclaimer in the 1761f28255Scgd.\" documentation and/or other materials provided with the distribution. 18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors 1961f28255Scgd.\" may be used to endorse or promote products derived from this software 2061f28255Scgd.\" without specific prior written permission. 2161f28255Scgd.\" 2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2561f28255Scgd.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3261f28255Scgd.\" SUCH DAMAGE. 3361f28255Scgd.\" 34389c46c5Sjtc.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 3561f28255Scgd.\" 36*045ae074Ssevan.Dd September 2, 2019 3761f28255Scgd.Dt FOPEN 3 3861f28255Scgd.Os 3961f28255Scgd.Sh NAME 4061f28255Scgd.Nm fopen , 4161f28255Scgd.Nm fdopen , 4261f28255Scgd.Nm freopen 4361f28255Scgd.Nd stream open functions 44312aca53Sperry.Sh LIBRARY 45312aca53Sperry.Lb libc 4661f28255Scgd.Sh SYNOPSIS 47472351e1Swiz.In stdio.h 4861f28255Scgd.Ft FILE * 4913034da9Skleink.Fn fopen "const char * restrict path" "const char * restrict mode" 5061f28255Scgd.Ft FILE * 513b7ac44eSkleink.Fn fdopen "int fildes" "const char *mode" 5261f28255Scgd.Ft FILE * 5313034da9Skleink.Fn freopen "const char * restrict path" "const char * restrict mode" "FILE * restrict stream" 5461f28255Scgd.Sh DESCRIPTION 5561f28255ScgdThe 5661f28255Scgd.Fn fopen 5761f28255Scgdfunction 5861f28255Scgdopens the file whose name is the string pointed to by 5961f28255Scgd.Fa path 6061f28255Scgdand associates a stream with it. 6161f28255Scgd.Pp 6261f28255ScgdThe argument 6361f28255Scgd.Fa mode 6461f28255Scgdpoints to a string beginning with one of the following 651a3a8b34Skresequences, which may be followed by additional modifiers 661a3a8b34Skreas indicated below: 6776576e42Swiz.Bl -tag -width 4n 6876576e42Swiz.It Dq Li a 691a3a8b34SkreAppend; open an existing or new file for writing in append mode. 7076576e42SwizThe file is created if it does not exist. 7176576e42Swiz.It Dq Li a+ 721a3a8b34SkreOpen for reading and writing in append mode. 7376576e42SwizThe file is created if it does not exist. 7461f28255Scgd.It Dq Li r 751a3a8b34SkreRead; open an existing file for reading. 7661f28255Scgd.It Dq Li r+ 771a3a8b34SkreOpen an existing file for reading and writing. 7861f28255Scgd.It Dq Li w 791a3a8b34SkreWrite; open an empty file for writing. 801a3a8b34SkreTruncate an existing file to zero length or create a new file. 813c891ba5Smycroft.It Dq Li w+ 821a3a8b34SkreOpen an empty file for reading and writing. 830e9bf305SpookaTruncate file to zero length or create file. 8461f28255Scgd.El 8561f28255Scgd.Pp 861a3a8b34SkreAfter one of those, the 8761f28255Scgd.Fa mode 881a3a8b34Skrestring can also include one or more of the following modifier letters: 8976576e42Swiz.Bl -tag -width 4n 9076576e42Swiz.It Sq b 9176576e42SwizThe letter 9276576e42Swiz.Sq b 9376576e42Swizmay appear either as a last character or as a character between the 9476576e42Swizcharacters in any of the two-character strings described above. 9561f28255ScgdThis is strictly for compatibility with 961a3a8b34Skre.St -ansiC , 971a3a8b34Skrewhere it means open in 981a3a8b34Skre.Dq binary 991a3a8b34Skremode which is identical to text mode here, 1001a3a8b34Skreso has no effect; the 10176576e42Swiz.Sq b 102e2bebf71Schristosis ignored. 10376576e42Swiz.It Sq e 104e2bebf71SchristosThe letter 10576576e42Swiz.Sq e 106b4e5b9faSchristosin the mode string sets the close-on-exec 107b4e5b9faSchristos.Pq Dv O_CLOEXEC 108b4e5b9faSchristosflag of the file descriptor, which means that it will not be available 109b4e5b9faSchristosafter an 1100858eb46Swiz.Xr exec 3 111b4e5b9faSchristossystem call. 1126569c5c5SwizThis is a non 113fc1a8318Schristos.St -ansiC 114fc1a8318Schristosextension. 11576576e42Swiz.It Sq f 116846092f0SwizThe letter 11776576e42Swiz.Sq f 11876576e42Swizin the mode string restricts 11976576e42Swiz.Fn fopen 12076576e42Swizto regular files; if the file opened is not a regular file, 121e2bebf71Schristos.Fn fopen 1221a3a8b34Skrewill close it, and fail. 123e2bebf71SchristosThis is a non 124e2bebf71Schristos.St -ansiC 125e2bebf71Schristosextension. 126b4e5b9faSchristos.It Sq l 127b4e5b9faSchristosThe letter 128b4e5b9faSchristos.Sq l 129b4e5b9faSchristosin the mode string turns the don't-follow-symlinks 130b4e5b9faSchristos.Pq Dv O_NOFOLLOW 131b4e5b9faSchristosflag of the file descriptor, which means that if the last path component 132b4e5b9faSchristosis a symbolic link, it will not be followed. 133b4e5b9faSchristosThis is a non 134b4e5b9faSchristos.St -ansiC 135b4e5b9faSchristosextension. 13642837a5bSchristos.It Sq x 13742837a5bSchristosThe letter 13842837a5bSchristos.Sq x 139b4ed92beSriastradhin the mode turns on exclusive open mode to the file 140b4ed92beSriastradh.Pq Dv O_EXCL 14142837a5bSchristoswhich means that the file will not be created if it already exists. 1426c755bb2SkreIn that case 1431a3a8b34Skre.Fn fopen 1441a3a8b34Skrewill fail. 1454c0e40e4Sjruoho.El 146e2bebf71Schristos.Pp 14761f28255ScgdAny created files will have mode 148727da110Sjoerg.Pf \*q Dv S_IRUSR 14961f28255Scgd\&| 15061f28255Scgd.Dv S_IWUSR 15161f28255Scgd\&| 15261f28255Scgd.Dv S_IRGRP 15361f28255Scgd\&| 15461f28255Scgd.Dv S_IWGRP 15561f28255Scgd\&| 15661f28255Scgd.Dv S_IROTH 15761f28255Scgd\&| 158727da110Sjoerg.Dv S_IWOTH Ns \*q 15961f28255Scgd.Pq Li 0666 , 16061f28255Scgdas modified by the process' 1614c0e40e4Sjruoho.Xr umask 2 1624c0e40e4Sjruohovalue. 16361f28255Scgd.Pp 1643b7ac44eSkleinkOpening a file with append mode causes all subsequent writes to it 1653b7ac44eSkleinkto be forced to the then current end of file, regardless of intervening 1663b7ac44eSkleinkrepositioning of the stream. 1673b7ac44eSkleink.Pp 1683b7ac44eSkleinkThe 1693b7ac44eSkleink.Fn fopen 1703b7ac44eSkleinkand 1713b7ac44eSkleink.Fn freopen 1723b7ac44eSkleinkfunctions initially position the stream at the start of the file 1733b7ac44eSkleinkunless the file is opened with append mode, 1743b7ac44eSkleinkin which case the stream is initially positioned at the end of the file. 175dc17a3ffSfair.\" PR 6072 claims this paragraph is not correct. 176dc17a3ffSfair.\" .Pp 177dc17a3ffSfair.\" Reads and writes may be intermixed on read/write streams in any order, 178dc17a3ffSfair.\" and do not require an intermediate seek as in previous versions of 179dc17a3ffSfair.\" .Em stdio . 180dc17a3ffSfair.\" This is not portable to other systems, however; 181dc17a3ffSfair.\" .Tn ANSI C 182dc17a3ffSfair.\" requires that 183dc17a3ffSfair.\" a file positioning function intervene between output and input, unless 184dc17a3ffSfair.\" an input operation encounters end-of-file. 18561f28255Scgd.Pp 18661f28255ScgdThe 18761f28255Scgd.Fn fdopen 18861f28255Scgdfunction associates a stream with the existing file descriptor, 18961f28255Scgd.Fa fildes . 19061f28255ScgdThe 19161f28255Scgd.Fa mode 19261f28255Scgdof the stream must be compatible with the mode of the file descriptor. 1933b7ac44eSkleinkThe stream is positioned at the file offset of the file descriptor. 19461f28255Scgd.Pp 19561f28255ScgdThe 19661f28255Scgd.Fn freopen 19761f28255Scgdfunction 19861f28255Scgdopens the file whose name is the string pointed to by 19961f28255Scgd.Fa path 20061f28255Scgdand associates the stream pointed to by 20161f28255Scgd.Fa stream 20261f28255Scgdwith it. 20361f28255ScgdThe original stream (if it exists) is closed. 20461f28255ScgdThe 20561f28255Scgd.Fa mode 20661f28255Scgdargument is used just as in the 2078e1d7f3dSfair.Fn fopen 20861f28255Scgdfunction. 20961f28255ScgdThe primary use of the 21061f28255Scgd.Fn freopen 21161f28255Scgdfunction 21261f28255Scgdis to change the file associated with a 21361f28255Scgdstandard text stream 21461f28255Scgd.Pf ( Em stderr , 21561f28255Scgd.Em stdin , 21661f28255Scgdor 21761f28255Scgd.Em stdout ) . 21890b9d61dSchristos.Pp 21990b9d61dSchristosInput and output against the opened stream will be fully buffered, unless 22090b9d61dSchristosit refers to an interactive terminal device, or a different kind of buffering 22190b9d61dSchristosis specified in the environment. 22290b9d61dSchristosSee 22390b9d61dSchristos.Xr setvbuf 3 22490b9d61dSchristosfor additional details. 22561f28255Scgd.Sh RETURN VALUES 22661f28255ScgdUpon successful completion 22761f28255Scgd.Fn fopen , 22861f28255Scgd.Fn fdopen 22961f28255Scgdand 23061f28255Scgd.Fn freopen 2310858eb46Swizreturn a FILE pointer. 23261f28255ScgdOtherwise, 23361f28255Scgd.Dv NULL 23461f28255Scgdis returned and the global variable 23561f28255Scgd.Va errno 23661f28255Scgdis set to indicate the error. 23761f28255Scgd.Sh ERRORS 2384c0e40e4SjruohoThe functions may fail if: 239a66bb899Sjtc.Bl -tag -width Er 240846092f0Swiz.It Bq Er EFTYPE 241846092f0SwizThe file is not a regular file and the character ``f'' is specified 242846092f0Swizin the mode. 24361f28255Scgd.It Bq Er EINVAL 2444c0e40e4SjruohoThe specified 24561f28255Scgd.Fa mode 24661f28255Scgdwas invalid. 24761f28255Scgd.El 24861f28255Scgd.Pp 24961f28255ScgdThe 25061f28255Scgd.Fn fopen , 25161f28255Scgd.Fn fdopen 25261f28255Scgdand 25361f28255Scgd.Fn freopen 25461f28255Scgdfunctions 25561f28255Scgdmay also fail and set 25661f28255Scgd.Va errno 25761f28255Scgdfor any of the errors specified for the routine 25861f28255Scgd.Xr malloc 3 . 25961f28255Scgd.Pp 26061f28255ScgdThe 26161f28255Scgd.Fn fopen 26261f28255Scgdfunction 26361f28255Scgdmay also fail and set 26461f28255Scgd.Va errno 26561f28255Scgdfor any of the errors specified for the routine 26661f28255Scgd.Xr open 2 . 26761f28255Scgd.Pp 26861f28255ScgdThe 26961f28255Scgd.Fn fdopen 27061f28255Scgdfunction 27161f28255Scgdmay also fail and set 27261f28255Scgd.Va errno 27361f28255Scgdfor any of the errors specified for the routine 27461f28255Scgd.Xr fcntl 2 . 27561f28255Scgd.Pp 27661f28255ScgdThe 27761f28255Scgd.Fn freopen 27861f28255Scgdfunction 27961f28255Scgdmay also fail and set 28061f28255Scgd.Va errno 28161f28255Scgdfor any of the errors specified for the routines 28261f28255Scgd.Xr open 2 , 28361f28255Scgd.Xr fclose 3 28461f28255Scgdand 28561f28255Scgd.Xr fflush 3 . 28661f28255Scgd.Sh SEE ALSO 28761f28255Scgd.Xr open 2 , 28861f28255Scgd.Xr fclose 3 , 28953f8a68cSwiz.Xr fileno 3 , 29061f28255Scgd.Xr fseek 3 , 29161f28255Scgd.Xr funopen 3 29261f28255Scgd.Sh STANDARDS 29361f28255ScgdThe 29461f28255Scgd.Fn fopen 29561f28255Scgdand 29661f28255Scgd.Fn freopen 2974c0e40e4Sjruohofunctions conform to 29861f28255Scgd.St -ansiC . 2994c0e40e4SjruohoAll three functions are specified in 3004c0e40e4Sjruoho.St -p1003.1-2008 . 301*045ae074Ssevan.Sh HISTORY 302*045ae074SsevanAn 303*045ae074Ssevan.Fn fopen 304*045ae074Ssevanfunction appeared in 305*045ae074Ssevan.At v1 . 306673cbedcSyamt.Sh CAVEATS 307673cbedcSyamtProper code using 308673cbedcSyamt.Fn fdopen 309673cbedcSyamtwith error checking should 310673cbedcSyamt.Xr close 2 311673cbedcSyamt.Fa fildes 312673cbedcSyamtin case of failure, and 313673cbedcSyamt.Xr fclose 3 314673cbedcSyamtthe resulting FILE * in case of success. 315673cbedcSyamt.Bd -literal 316673cbedcSyamt FILE *file; 317673cbedcSyamt int fd; 318673cbedcSyamt 319673cbedcSyamt if ((file = fdopen(fd, "r")) != NULL) { 320673cbedcSyamt /* perform operations on the FILE * */ 321673cbedcSyamt fclose(file); 322673cbedcSyamt } else { 323673cbedcSyamt /* failure, report the error */ 324673cbedcSyamt close(fd); 325673cbedcSyamt } 326673cbedcSyamt.Ed 327