xref: /freebsd/lib/libc/stdio/fopen.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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.\"
325a57401eSDag-Erling Smørgrav.Dd September 1, 2023
3358f0484fSRodney W. Grimes.Dt FOPEN 3
3458f0484fSRodney W. Grimes.Os
3558f0484fSRodney W. Grimes.Sh NAME
3658f0484fSRodney W. Grimes.Nm fopen ,
3758f0484fSRodney W. Grimes.Nm fdopen ,
3896c95412SPietro Cerutti.Nm freopen ,
3996c95412SPietro Cerutti.Nm fmemopen
4058f0484fSRodney W. Grimes.Nd stream open functions
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4432eef9aeSRuslan Ermilov.In stdio.h
4558f0484fSRodney W. Grimes.Ft FILE *
4654e4e385SMike Barcroft.Fn fopen "const char * restrict path" "const char * restrict mode"
4758f0484fSRodney W. Grimes.Ft FILE *
4823f0c1fcSBruce Evans.Fn fdopen "int fildes" "const char *mode"
4958f0484fSRodney W. Grimes.Ft FILE *
5023f0c1fcSBruce Evans.Fn freopen "const char *path" "const char *mode" "FILE *stream"
5196c95412SPietro Cerutti.Ft FILE *
525a57401eSDag-Erling Smørgrav.Fn fmemopen "void * restrict buf" "size_t size" "const char * restrict mode"
5358f0484fSRodney W. Grimes.Sh DESCRIPTION
5458f0484fSRodney W. GrimesThe
5558f0484fSRodney W. Grimes.Fn fopen
5658f0484fSRodney W. Grimesfunction
5758f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
5858f0484fSRodney W. Grimes.Fa path
5958f0484fSRodney W. Grimesand associates a stream with it.
6058f0484fSRodney W. Grimes.Pp
6158f0484fSRodney W. GrimesThe argument
6258f0484fSRodney W. Grimes.Fa mode
63bd26fb81SDavid Schultzpoints to a string beginning with one of the following letters:
6458f0484fSRodney W. Grimes.Bl -tag -width indent
6558f0484fSRodney W. Grimes.It Dq Li r
66bd26fb81SDavid SchultzOpen for reading.
6758f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
68bd26fb81SDavid SchultzFail if the file does not exist.
6958f0484fSRodney W. Grimes.It Dq Li w
70bd26fb81SDavid SchultzOpen for writing.
7158f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7260419a9cSEitan AdlerTruncate the file to zero length if it exists or create the file if it does not exist.
7358f0484fSRodney W. Grimes.It Dq Li a
7458f0484fSRodney W. GrimesOpen for writing.
7558f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
7611ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current
7711ab3fc7SEivind Eklundend of file, irrespective of any intervening
7811ab3fc7SEivind Eklund.Xr fseek 3
7911ab3fc7SEivind Eklundor similar.
80bd26fb81SDavid SchultzCreate the file if it does not exist.
8158f0484fSRodney W. Grimes.El
8258f0484fSRodney W. Grimes.Pp
83bd26fb81SDavid SchultzAn optional
84bd26fb81SDavid Schultz.Dq Li +
85bd26fb81SDavid Schultzfollowing
86bd26fb81SDavid Schultz.Dq Li r ,
87bd26fb81SDavid Schultz.Dq Li w ,
88bd26fb81SDavid Schultzor
89bd26fb81SDavid Schultz.Dq Li a
90bd26fb81SDavid Schultzopens the file for both reading and writing.
91bd26fb81SDavid SchultzAn optional
92bd26fb81SDavid Schultz.Dq Li x
93bd26fb81SDavid Schultzfollowing
94bd26fb81SDavid Schultz.Dq Li w
95bd26fb81SDavid Schultzor
96bd26fb81SDavid Schultz.Dq Li w+
97bd26fb81SDavid Schultzcauses the
98bd26fb81SDavid Schultz.Fn fopen
99bd26fb81SDavid Schultzcall to fail if the file already exists.
1005ba651f0SJilles TjoelkerAn optional
1015ba651f0SJilles Tjoelker.Dq Li e
1025ba651f0SJilles Tjoelkerfollowing the above
1035ba651f0SJilles Tjoelkercauses the
1045ba651f0SJilles Tjoelker.Fn fopen
1055ba651f0SJilles Tjoelkercall to set the
1065ba651f0SJilles Tjoelker.Dv FD_CLOEXEC
1075ba651f0SJilles Tjoelkerflag on the underlying file descriptor.
108bd26fb81SDavid Schultz.Pp
10958f0484fSRodney W. GrimesThe
11058f0484fSRodney W. Grimes.Fa mode
111bd26fb81SDavid Schultzstring can also include the letter
112bd26fb81SDavid Schultz.Dq Li b
113bd26fb81SDavid Schultzafter either the
114bd26fb81SDavid Schultz.Dq Li +
115bd26fb81SDavid Schultzor the first letter.
11658f0484fSRodney W. GrimesThis is strictly for compatibility with
117588a200cSRuslan Ermilov.St -isoC
118646b68f0SPietro Ceruttiand has effect only for
1194ac9afddSMark Johnston.Fn fmemopen ;
1204ac9afddSMark Johnstonotherwise
1212e59a758SDag-Erling Smørgrav.Dq Li b
1222e59a758SDag-Erling Smørgravis ignored.
12358f0484fSRodney W. Grimes.Pp
12458f0484fSRodney W. GrimesAny created files will have mode
1252a10d6d1SUlrich Spörlein.Do Dv S_IRUSR
12658f0484fSRodney W. Grimes\&|
12758f0484fSRodney W. Grimes.Dv S_IWUSR
12858f0484fSRodney W. Grimes\&|
12958f0484fSRodney W. Grimes.Dv S_IRGRP
13058f0484fSRodney W. Grimes\&|
13158f0484fSRodney W. Grimes.Dv S_IWGRP
13258f0484fSRodney W. Grimes\&|
13358f0484fSRodney W. Grimes.Dv S_IROTH
13458f0484fSRodney W. Grimes\&|
1352a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc
13658f0484fSRodney W. Grimes.Pq Li 0666 ,
13758f0484fSRodney W. Grimesas modified by the process'
13858f0484fSRodney W. Grimesumask value (see
13958f0484fSRodney W. Grimes.Xr umask 2 ) .
14058f0484fSRodney W. Grimes.Pp
14158f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
14258f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
14358f0484fSRodney W. Grimes.Em stdio .
14458f0484fSRodney W. GrimesThis is not portable to other systems, however;
1455a57401eSDag-Erling Smørgrav.St -isoC
1465a57401eSDag-Erling Smørgravand
1475a57401eSDag-Erling Smørgrav.St -p1003.1
1485a57401eSDag-Erling Smørgravboth require that
14958f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
15058f0484fSRodney W. Grimesan input operation encounters end-of-file.
15158f0484fSRodney W. Grimes.Pp
15258f0484fSRodney W. GrimesThe
15358f0484fSRodney W. Grimes.Fn fdopen
15458f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
15558f0484fSRodney W. Grimes.Fa fildes .
1562efeeba5SRuslan ErmilovThe mode
15758f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
158bd26fb81SDavid SchultzThe
159bd26fb81SDavid Schultz.Dq Li x
160bd26fb81SDavid Schultzmode option is ignored.
1615ba651f0SJilles TjoelkerIf the
1625ba651f0SJilles Tjoelker.Dq Li e
1635ba651f0SJilles Tjoelkermode option is present, the
1645ba651f0SJilles Tjoelker.Dv FD_CLOEXEC
1655ba651f0SJilles Tjoelkerflag is set, otherwise it remains unchanged.
166f24c2153SArchie CobbsWhen the stream is closed via
167f24c2153SArchie Cobbs.Xr fclose 3 ,
168f24c2153SArchie Cobbs.Fa fildes
169f24c2153SArchie Cobbsis closed also.
17058f0484fSRodney W. Grimes.Pp
17158f0484fSRodney W. GrimesThe
17258f0484fSRodney W. Grimes.Fn freopen
17358f0484fSRodney W. Grimesfunction
17458f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
17558f0484fSRodney W. Grimes.Fa path
17658f0484fSRodney W. Grimesand associates the stream pointed to by
17758f0484fSRodney W. Grimes.Fa stream
17858f0484fSRodney W. Grimeswith it.
17958f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
18058f0484fSRodney W. GrimesThe
18158f0484fSRodney W. Grimes.Fa mode
18258f0484fSRodney W. Grimesargument is used just as in the
183064f0074SMike Pritchard.Fn fopen
18458f0484fSRodney W. Grimesfunction.
185c2974987STim J. Robbins.Pp
186c2974987STim J. RobbinsIf the
187c2974987STim J. Robbins.Fa path
188c2974987STim J. Robbinsargument is
189c2974987STim J. Robbins.Dv NULL ,
190c2974987STim J. Robbins.Fn freopen
191c2974987STim J. Robbinsattempts to re-open the file associated with
192c2974987STim J. Robbins.Fa stream
193c2974987STim J. Robbinswith a new mode.
194c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally
195c2974987STim J. Robbinsopened with:
196bd26fb81SDavid SchultzStreams open for reading can only be re-opened for reading,
197bd26fb81SDavid Schultzstreams open for writing can only be re-opened for writing,
198bd26fb81SDavid Schultzand streams open for reading and writing can be re-opened in any mode.
199bd26fb81SDavid SchultzThe
200bd26fb81SDavid Schultz.Dq Li x
201bd26fb81SDavid Schultzmode option is not meaningful in this context.
202c2974987STim J. Robbins.Pp
20358f0484fSRodney W. GrimesThe primary use of the
20458f0484fSRodney W. Grimes.Fn freopen
20558f0484fSRodney W. Grimesfunction
20658f0484fSRodney W. Grimesis to change the file associated with a
20758f0484fSRodney W. Grimesstandard text stream
208ae828962SRuslan Ermilov.Dv ( stderr , stdin ,
20958f0484fSRodney W. Grimesor
210ae828962SRuslan Ermilov.Dv stdout ) .
21196c95412SPietro Cerutti.Pp
21296c95412SPietro CeruttiThe
21396c95412SPietro Cerutti.Fn fmemopen
21496c95412SPietro Ceruttifunction
21596c95412SPietro Ceruttiassociates the buffer given by the
21696c95412SPietro Cerutti.Fa buf
21796c95412SPietro Ceruttiand
21896c95412SPietro Cerutti.Fa size
21996c95412SPietro Ceruttiarguments with a stream.
22096c95412SPietro CeruttiThe
22196c95412SPietro Cerutti.Fa buf
222646b68f0SPietro Ceruttiargument is either a null pointer or point to a buffer that
22396c95412SPietro Ceruttiis at least
22496c95412SPietro Cerutti.Fa size
22596c95412SPietro Ceruttibytes long.
22696c95412SPietro CeruttiIf a null pointer is specified as the
22796c95412SPietro Cerutti.Fa buf
22896c95412SPietro Ceruttiargument,
22996c95412SPietro Cerutti.Fn fmemopen
230646b68f0SPietro Ceruttiallocates
23196c95412SPietro Cerutti.Fa size
23239d657f5SEnji Cooperbytes of memory.
23339d657f5SEnji CooperThis buffer is automatically freed when the stream is closed.
23439d657f5SEnji CooperBuffers can be opened in text-mode (default) or binary-mode
2352e59a758SDag-Erling Smørgrav(if
2362e59a758SDag-Erling Smørgrav.Dq Li b
2372e59a758SDag-Erling Smørgravis present in the second or third position of the
238646b68f0SPietro Cerutti.Fa mode
23939d657f5SEnji Cooperargument).
24039d657f5SEnji CooperBuffers opened in text-mode make sure that writes are terminated with a
24139d657f5SEnji Cooper.Dv NULL
24239d657f5SEnji Cooperbyte, if the last write hasn't filled up the whole buffer.
24339d657f5SEnji CooperBuffers opened in binary-mode never append a
24439d657f5SEnji Cooper.Dv NULL
24539d657f5SEnji Cooperbyte.
24658f0484fSRodney W. Grimes.Sh RETURN VALUES
24758f0484fSRodney W. GrimesUpon successful completion
24858f0484fSRodney W. Grimes.Fn fopen ,
249*c9f5889dSDag-Erling Smørgrav.Fn fdopen ,
25058f0484fSRodney W. Grimes.Fn freopen
251*c9f5889dSDag-Erling Smørgravand
252*c9f5889dSDag-Erling Smørgrav.Fn fmemopen
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.
359e0f7c06dSGordon Bergling.Sh HISTORY
360e0f7c06dSGordon BerglingAn
361e0f7c06dSGordon Bergling.Fn fopen
362e0f7c06dSGordon Berglingfunction appeared in
363e0f7c06dSGordon Bergling.At v1 .
364