158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by 558f0484fSRodney W. Grimes.\" Chris Torek and the American National Standards Committee X3, 658f0484fSRodney W. Grimes.\" on Information Processing Systems. 758f0484fSRodney W. Grimes.\" 858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes.\" are met: 1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes.\" without specific prior written permission. 1958f0484fSRodney W. Grimes.\" 2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes.\" SUCH DAMAGE. 3158f0484fSRodney W. Grimes.\" 3258f0484fSRodney W. Grimes.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 337f3dea24SPeter Wemm.\" $FreeBSD$ 3458f0484fSRodney W. Grimes.\" 3596c95412SPietro Cerutti.Dd January 30, 2013 3658f0484fSRodney W. Grimes.Dt FOPEN 3 3758f0484fSRodney W. Grimes.Os 3858f0484fSRodney W. Grimes.Sh NAME 3958f0484fSRodney W. Grimes.Nm fopen , 4058f0484fSRodney W. Grimes.Nm fdopen , 4196c95412SPietro Cerutti.Nm freopen , 4296c95412SPietro Cerutti.Nm fmemopen 4358f0484fSRodney W. Grimes.Nd stream open functions 4425bb73e0SAlexey Zelkin.Sh LIBRARY 4525bb73e0SAlexey Zelkin.Lb libc 4658f0484fSRodney W. Grimes.Sh SYNOPSIS 4732eef9aeSRuslan Ermilov.In stdio.h 4858f0484fSRodney W. Grimes.Ft FILE * 4954e4e385SMike Barcroft.Fn fopen "const char * restrict path" "const char * restrict mode" 5058f0484fSRodney W. Grimes.Ft FILE * 5123f0c1fcSBruce Evans.Fn fdopen "int fildes" "const char *mode" 5258f0484fSRodney W. Grimes.Ft FILE * 5323f0c1fcSBruce Evans.Fn freopen "const char *path" "const char *mode" "FILE *stream" 5496c95412SPietro Cerutti.Ft FILE * 5596c95412SPietro Cerutti.Fn fmemopen "void *restrict *buf" "size_t size" "const char * restrict mode" 5658f0484fSRodney W. Grimes.Sh DESCRIPTION 5758f0484fSRodney W. GrimesThe 5858f0484fSRodney W. Grimes.Fn fopen 5958f0484fSRodney W. Grimesfunction 6058f0484fSRodney W. Grimesopens the file whose name is the string pointed to by 6158f0484fSRodney W. Grimes.Fa path 6258f0484fSRodney W. Grimesand associates a stream with it. 6358f0484fSRodney W. Grimes.Pp 6458f0484fSRodney W. GrimesThe argument 6558f0484fSRodney W. Grimes.Fa mode 66bd26fb81SDavid Schultzpoints to a string beginning with one of the following letters: 6758f0484fSRodney W. Grimes.Bl -tag -width indent 6858f0484fSRodney W. Grimes.It Dq Li r 69bd26fb81SDavid SchultzOpen for reading. 7058f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file. 71bd26fb81SDavid SchultzFail if the file does not exist. 7258f0484fSRodney W. Grimes.It Dq Li w 73bd26fb81SDavid SchultzOpen for writing. 7458f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file. 75bd26fb81SDavid SchultzCreate the file if it does not exist. 7658f0484fSRodney W. Grimes.It Dq Li a 7758f0484fSRodney W. GrimesOpen for writing. 7858f0484fSRodney W. GrimesThe stream is positioned at the end of the file. 7911ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current 8011ab3fc7SEivind Eklundend of file, irrespective of any intervening 8111ab3fc7SEivind Eklund.Xr fseek 3 8211ab3fc7SEivind Eklundor similar. 83bd26fb81SDavid SchultzCreate the file if it does not exist. 8458f0484fSRodney W. Grimes.El 8558f0484fSRodney W. Grimes.Pp 86bd26fb81SDavid SchultzAn optional 87bd26fb81SDavid Schultz.Dq Li + 88bd26fb81SDavid Schultzfollowing 89bd26fb81SDavid Schultz.Dq Li r , 90bd26fb81SDavid Schultz.Dq Li w , 91bd26fb81SDavid Schultzor 92bd26fb81SDavid Schultz.Dq Li a 93bd26fb81SDavid Schultzopens the file for both reading and writing. 94bd26fb81SDavid SchultzAn optional 95bd26fb81SDavid Schultz.Dq Li x 96bd26fb81SDavid Schultzfollowing 97bd26fb81SDavid Schultz.Dq Li w 98bd26fb81SDavid Schultzor 99bd26fb81SDavid Schultz.Dq Li w+ 100bd26fb81SDavid Schultzcauses the 101bd26fb81SDavid Schultz.Fn fopen 102bd26fb81SDavid Schultzcall to fail if the file already exists. 1035ba651f0SJilles TjoelkerAn optional 1045ba651f0SJilles Tjoelker.Dq Li e 1055ba651f0SJilles Tjoelkerfollowing the above 1065ba651f0SJilles Tjoelkercauses the 1075ba651f0SJilles Tjoelker.Fn fopen 1085ba651f0SJilles Tjoelkercall to set the 1095ba651f0SJilles Tjoelker.Dv FD_CLOEXEC 1105ba651f0SJilles Tjoelkerflag on the underlying file descriptor. 111bd26fb81SDavid Schultz.Pp 11258f0484fSRodney W. GrimesThe 11358f0484fSRodney W. Grimes.Fa mode 114bd26fb81SDavid Schultzstring can also include the letter 115bd26fb81SDavid Schultz.Dq Li b 116bd26fb81SDavid Schultzafter either the 117bd26fb81SDavid Schultz.Dq Li + 118bd26fb81SDavid Schultzor the first letter. 11958f0484fSRodney W. GrimesThis is strictly for compatibility with 120588a200cSRuslan Ermilov.St -isoC 121646b68f0SPietro Ceruttiand has effect only for 122646b68f0SPietro Cerutti.Fn fmemopen 1232e59a758SDag-Erling Smørgrav; otherwise 1242e59a758SDag-Erling Smørgrav.Dq Li b 1252e59a758SDag-Erling Smørgravis ignored. 12658f0484fSRodney W. Grimes.Pp 12758f0484fSRodney W. GrimesAny created files will have mode 1282a10d6d1SUlrich Spörlein.Do Dv S_IRUSR 12958f0484fSRodney W. Grimes\&| 13058f0484fSRodney W. Grimes.Dv S_IWUSR 13158f0484fSRodney W. Grimes\&| 13258f0484fSRodney W. Grimes.Dv S_IRGRP 13358f0484fSRodney W. Grimes\&| 13458f0484fSRodney W. Grimes.Dv S_IWGRP 13558f0484fSRodney W. Grimes\&| 13658f0484fSRodney W. Grimes.Dv S_IROTH 13758f0484fSRodney W. Grimes\&| 1382a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc 13958f0484fSRodney W. Grimes.Pq Li 0666 , 14058f0484fSRodney W. Grimesas modified by the process' 14158f0484fSRodney W. Grimesumask value (see 14258f0484fSRodney W. Grimes.Xr umask 2 ) . 14358f0484fSRodney W. Grimes.Pp 14458f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order, 14558f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of 14658f0484fSRodney W. Grimes.Em stdio . 14758f0484fSRodney W. GrimesThis is not portable to other systems, however; 14858f0484fSRodney W. Grimes.Tn ANSI C 14958f0484fSRodney W. Grimesrequires that 15058f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless 15158f0484fSRodney W. Grimesan input operation encounters end-of-file. 15258f0484fSRodney W. Grimes.Pp 15358f0484fSRodney W. GrimesThe 15458f0484fSRodney W. Grimes.Fn fdopen 15558f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor, 15658f0484fSRodney W. Grimes.Fa fildes . 1572efeeba5SRuslan ErmilovThe mode 15858f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor. 159bd26fb81SDavid SchultzThe 160bd26fb81SDavid Schultz.Dq Li x 161bd26fb81SDavid Schultzmode option is ignored. 1625ba651f0SJilles TjoelkerIf the 1635ba651f0SJilles Tjoelker.Dq Li e 1645ba651f0SJilles Tjoelkermode option is present, the 1655ba651f0SJilles Tjoelker.Dv FD_CLOEXEC 1665ba651f0SJilles Tjoelkerflag is set, otherwise it remains unchanged. 167f24c2153SArchie CobbsWhen the stream is closed via 168f24c2153SArchie Cobbs.Xr fclose 3 , 169f24c2153SArchie Cobbs.Fa fildes 170f24c2153SArchie Cobbsis closed also. 17158f0484fSRodney W. Grimes.Pp 17258f0484fSRodney W. GrimesThe 17358f0484fSRodney W. Grimes.Fn freopen 17458f0484fSRodney W. Grimesfunction 17558f0484fSRodney W. Grimesopens the file whose name is the string pointed to by 17658f0484fSRodney W. Grimes.Fa path 17758f0484fSRodney W. Grimesand associates the stream pointed to by 17858f0484fSRodney W. Grimes.Fa stream 17958f0484fSRodney W. Grimeswith it. 18058f0484fSRodney W. GrimesThe original stream (if it exists) is closed. 18158f0484fSRodney W. GrimesThe 18258f0484fSRodney W. Grimes.Fa mode 18358f0484fSRodney W. Grimesargument is used just as in the 184064f0074SMike Pritchard.Fn fopen 18558f0484fSRodney W. Grimesfunction. 186c2974987STim J. Robbins.Pp 187c2974987STim J. RobbinsIf the 188c2974987STim J. Robbins.Fa path 189c2974987STim J. Robbinsargument is 190c2974987STim J. Robbins.Dv NULL , 191c2974987STim J. Robbins.Fn freopen 192c2974987STim J. Robbinsattempts to re-open the file associated with 193c2974987STim J. Robbins.Fa stream 194c2974987STim J. Robbinswith a new mode. 195c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally 196c2974987STim J. Robbinsopened with: 197bd26fb81SDavid SchultzStreams open for reading can only be re-opened for reading, 198bd26fb81SDavid Schultzstreams open for writing can only be re-opened for writing, 199bd26fb81SDavid Schultzand streams open for reading and writing can be re-opened in any mode. 200bd26fb81SDavid SchultzThe 201bd26fb81SDavid Schultz.Dq Li x 202bd26fb81SDavid Schultzmode option is not meaningful in this context. 203c2974987STim J. Robbins.Pp 20458f0484fSRodney W. GrimesThe primary use of the 20558f0484fSRodney W. Grimes.Fn freopen 20658f0484fSRodney W. Grimesfunction 20758f0484fSRodney W. Grimesis to change the file associated with a 20858f0484fSRodney W. Grimesstandard text stream 209ae828962SRuslan Ermilov.Dv ( stderr , stdin , 21058f0484fSRodney W. Grimesor 211ae828962SRuslan Ermilov.Dv stdout ) . 21296c95412SPietro Cerutti.Pp 21396c95412SPietro CeruttiThe 21496c95412SPietro Cerutti.Fn fmemopen 21596c95412SPietro Ceruttifunction 21696c95412SPietro Ceruttiassociates the buffer given by the 21796c95412SPietro Cerutti.Fa buf 21896c95412SPietro Ceruttiand 21996c95412SPietro Cerutti.Fa size 22096c95412SPietro Ceruttiarguments with a stream. 22196c95412SPietro CeruttiThe 22296c95412SPietro Cerutti.Fa buf 223646b68f0SPietro Ceruttiargument is either a null pointer or point to a buffer that 22496c95412SPietro Ceruttiis at least 22596c95412SPietro Cerutti.Fa size 22696c95412SPietro Ceruttibytes long. 22796c95412SPietro CeruttiIf a null pointer is specified as the 22896c95412SPietro Cerutti.Fa buf 22996c95412SPietro Ceruttiargument, 23096c95412SPietro Cerutti.Fn fmemopen 231646b68f0SPietro Ceruttiallocates 23296c95412SPietro Cerutti.Fa size 233*39d657f5SEnji Cooperbytes of memory. 234*39d657f5SEnji CooperThis buffer is automatically freed when the stream is closed. 235*39d657f5SEnji CooperBuffers can be opened in text-mode (default) or binary-mode 2362e59a758SDag-Erling Smørgrav(if 2372e59a758SDag-Erling Smørgrav.Dq Li b 2382e59a758SDag-Erling Smørgravis present in the second or third position of the 239646b68f0SPietro Cerutti.Fa mode 240*39d657f5SEnji Cooperargument). 241*39d657f5SEnji CooperBuffers opened in text-mode make sure that writes are terminated with a 242*39d657f5SEnji Cooper.Dv NULL 243*39d657f5SEnji Cooperbyte, if the last write hasn't filled up the whole buffer. 244*39d657f5SEnji CooperBuffers opened in binary-mode never append a 245*39d657f5SEnji Cooper.Dv NULL 246*39d657f5SEnji Cooperbyte. 24758f0484fSRodney W. Grimes.Sh RETURN VALUES 24858f0484fSRodney W. GrimesUpon successful completion 24958f0484fSRodney W. Grimes.Fn fopen , 25058f0484fSRodney W. Grimes.Fn fdopen 25158f0484fSRodney W. Grimesand 25258f0484fSRodney W. Grimes.Fn freopen 25358f0484fSRodney W. Grimesreturn a 25458f0484fSRodney W. Grimes.Tn FILE 25558f0484fSRodney W. Grimespointer. 25658f0484fSRodney W. GrimesOtherwise, 25758f0484fSRodney W. Grimes.Dv NULL 25858f0484fSRodney W. Grimesis returned and the global variable 25958f0484fSRodney W. Grimes.Va errno 26058f0484fSRodney W. Grimesis set to indicate the error. 26158f0484fSRodney W. Grimes.Sh ERRORS 26203fc6303SAlexey Zelkin.Bl -tag -width Er 26358f0484fSRodney W. Grimes.It Bq Er EINVAL 26458f0484fSRodney W. GrimesThe 26558f0484fSRodney W. Grimes.Fa mode 2662efeeba5SRuslan Ermilovargument 2672efeeba5SRuslan Ermilovto 26858f0484fSRodney W. Grimes.Fn fopen , 26958f0484fSRodney W. Grimes.Fn fdopen , 27096c95412SPietro Cerutti.Fn freopen , 27158f0484fSRodney W. Grimesor 27296c95412SPietro Cerutti.Fn fmemopen 27358f0484fSRodney W. Grimeswas invalid. 27458f0484fSRodney W. Grimes.El 27558f0484fSRodney W. Grimes.Pp 27658f0484fSRodney W. GrimesThe 27758f0484fSRodney W. Grimes.Fn fopen , 27896c95412SPietro Cerutti.Fn fdopen , 27958f0484fSRodney W. Grimes.Fn freopen 28096c95412SPietro Ceruttiand 28196c95412SPietro Cerutti.Fn fmemopen 28258f0484fSRodney W. Grimesfunctions 28358f0484fSRodney W. Grimesmay also fail and set 28458f0484fSRodney W. Grimes.Va errno 28558f0484fSRodney W. Grimesfor any of the errors specified for the routine 28658f0484fSRodney W. Grimes.Xr malloc 3 . 28758f0484fSRodney W. Grimes.Pp 28858f0484fSRodney W. GrimesThe 28958f0484fSRodney W. Grimes.Fn fopen 29058f0484fSRodney W. Grimesfunction 29158f0484fSRodney W. Grimesmay also fail and set 29258f0484fSRodney W. Grimes.Va errno 29358f0484fSRodney W. Grimesfor any of the errors specified for the routine 29458f0484fSRodney W. Grimes.Xr open 2 . 29558f0484fSRodney W. Grimes.Pp 29658f0484fSRodney W. GrimesThe 29758f0484fSRodney W. Grimes.Fn fdopen 29858f0484fSRodney W. Grimesfunction 29958f0484fSRodney W. Grimesmay also fail and set 30058f0484fSRodney W. Grimes.Va errno 30158f0484fSRodney W. Grimesfor any of the errors specified for the routine 30258f0484fSRodney W. Grimes.Xr fcntl 2 . 30358f0484fSRodney W. Grimes.Pp 30458f0484fSRodney W. GrimesThe 30558f0484fSRodney W. Grimes.Fn freopen 30658f0484fSRodney W. Grimesfunction 30758f0484fSRodney W. Grimesmay also fail and set 30858f0484fSRodney W. Grimes.Va errno 30958f0484fSRodney W. Grimesfor any of the errors specified for the routines 31058f0484fSRodney W. Grimes.Xr open 2 , 31158f0484fSRodney W. Grimes.Xr fclose 3 31258f0484fSRodney W. Grimesand 31358f0484fSRodney W. Grimes.Xr fflush 3 . 3144c524a42SPietro Cerutti.Pp 3154c524a42SPietro CeruttiThe 3164c524a42SPietro Cerutti.Fn fmemopen 3174c524a42SPietro Ceruttifunction 3184c524a42SPietro Ceruttimay also fail and set 3194c524a42SPietro Cerutti.Va errno 3204c524a42SPietro Ceruttiif the 3214c524a42SPietro Cerutti.Fa size 3224c524a42SPietro Ceruttiargument is 0. 32358f0484fSRodney W. Grimes.Sh SEE ALSO 32458f0484fSRodney W. Grimes.Xr open 2 , 32558f0484fSRodney W. Grimes.Xr fclose 3 , 3262f52231cSRuslan Ermilov.Xr fileno 3 , 32758f0484fSRodney W. Grimes.Xr fseek 3 , 3282f52231cSRuslan Ermilov.Xr funopen 3 32958f0484fSRodney W. Grimes.Sh STANDARDS 33058f0484fSRodney W. GrimesThe 33158f0484fSRodney W. Grimes.Fn fopen 33258f0484fSRodney W. Grimesand 33358f0484fSRodney W. Grimes.Fn freopen 33458f0484fSRodney W. Grimesfunctions 33558f0484fSRodney W. Grimesconform to 336cc32b2edSJilles Tjoelker.St -isoC , 337cc32b2edSJilles Tjoelkerwith the exception of the 338cc32b2edSJilles Tjoelker.Dq Li x 339cc32b2edSJilles Tjoelkermode option which conforms to 340cc32b2edSJilles Tjoelker.St -isoC-2011 . 34158f0484fSRodney W. GrimesThe 34258f0484fSRodney W. Grimes.Fn fdopen 34358f0484fSRodney W. Grimesfunction 34458f0484fSRodney W. Grimesconforms to 34558f0484fSRodney W. Grimes.St -p1003.1-88 . 3465ba651f0SJilles TjoelkerThe 3475ba651f0SJilles Tjoelker.Dq Li e 3485ba651f0SJilles Tjoelkermode option does not conform to any standard 3495ba651f0SJilles Tjoelkerbut is also supported by glibc. 35096c95412SPietro CeruttiThe 35196c95412SPietro Cerutti.Fn fmemopen 35296c95412SPietro Ceruttifunction 35396c95412SPietro Cerutticonforms to 35496c95412SPietro Cerutti.St -p1003.1-2008 . 3552e59a758SDag-Erling SmørgravThe 3562e59a758SDag-Erling Smørgrav.Dq Li b 3572e59a758SDag-Erling Smørgravmode does not conform to any standard 358646b68f0SPietro Ceruttibut is also supported by glibc. 359