xref: /freebsd/lib/libc/stdio/fopen.3 (revision 4c524a4287966d65275cd24fa4c7ac201fcf4918)
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.
1658f0484fSRodney W. Grimes.\" 4. 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
123646b68f0SPietro Cerutti; otherwise the ``b'' is ignored.
12458f0484fSRodney W. Grimes.Pp
12558f0484fSRodney W. GrimesAny created files will have mode
1262a10d6d1SUlrich Spörlein.Do Dv S_IRUSR
12758f0484fSRodney W. Grimes\&|
12858f0484fSRodney W. Grimes.Dv S_IWUSR
12958f0484fSRodney W. Grimes\&|
13058f0484fSRodney W. Grimes.Dv S_IRGRP
13158f0484fSRodney W. Grimes\&|
13258f0484fSRodney W. Grimes.Dv S_IWGRP
13358f0484fSRodney W. Grimes\&|
13458f0484fSRodney W. Grimes.Dv S_IROTH
13558f0484fSRodney W. Grimes\&|
1362a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc
13758f0484fSRodney W. Grimes.Pq Li 0666 ,
13858f0484fSRodney W. Grimesas modified by the process'
13958f0484fSRodney W. Grimesumask value (see
14058f0484fSRodney W. Grimes.Xr umask 2 ) .
14158f0484fSRodney W. Grimes.Pp
14258f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
14358f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
14458f0484fSRodney W. Grimes.Em stdio .
14558f0484fSRodney W. GrimesThis is not portable to other systems, however;
14658f0484fSRodney W. Grimes.Tn ANSI C
14758f0484fSRodney W. Grimesrequires that
14858f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
14958f0484fSRodney W. Grimesan input operation encounters end-of-file.
15058f0484fSRodney W. Grimes.Pp
15158f0484fSRodney W. GrimesThe
15258f0484fSRodney W. Grimes.Fn fdopen
15358f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
15458f0484fSRodney W. Grimes.Fa fildes .
1552efeeba5SRuslan ErmilovThe mode
15658f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
157bd26fb81SDavid SchultzThe
158bd26fb81SDavid Schultz.Dq Li x
159bd26fb81SDavid Schultzmode option is ignored.
1605ba651f0SJilles TjoelkerIf the
1615ba651f0SJilles Tjoelker.Dq Li e
1625ba651f0SJilles Tjoelkermode option is present, the
1635ba651f0SJilles Tjoelker.Dv FD_CLOEXEC
1645ba651f0SJilles Tjoelkerflag is set, otherwise it remains unchanged.
165f24c2153SArchie CobbsWhen the stream is closed via
166f24c2153SArchie Cobbs.Xr fclose 3 ,
167f24c2153SArchie Cobbs.Fa fildes
168f24c2153SArchie Cobbsis closed also.
16958f0484fSRodney W. Grimes.Pp
17058f0484fSRodney W. GrimesThe
17158f0484fSRodney W. Grimes.Fn freopen
17258f0484fSRodney W. Grimesfunction
17358f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
17458f0484fSRodney W. Grimes.Fa path
17558f0484fSRodney W. Grimesand associates the stream pointed to by
17658f0484fSRodney W. Grimes.Fa stream
17758f0484fSRodney W. Grimeswith it.
17858f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
17958f0484fSRodney W. GrimesThe
18058f0484fSRodney W. Grimes.Fa mode
18158f0484fSRodney W. Grimesargument is used just as in the
182064f0074SMike Pritchard.Fn fopen
18358f0484fSRodney W. Grimesfunction.
184c2974987STim J. Robbins.Pp
185c2974987STim J. RobbinsIf the
186c2974987STim J. Robbins.Fa path
187c2974987STim J. Robbinsargument is
188c2974987STim J. Robbins.Dv NULL ,
189c2974987STim J. Robbins.Fn freopen
190c2974987STim J. Robbinsattempts to re-open the file associated with
191c2974987STim J. Robbins.Fa stream
192c2974987STim J. Robbinswith a new mode.
193c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally
194c2974987STim J. Robbinsopened with:
195bd26fb81SDavid SchultzStreams open for reading can only be re-opened for reading,
196bd26fb81SDavid Schultzstreams open for writing can only be re-opened for writing,
197bd26fb81SDavid Schultzand streams open for reading and writing can be re-opened in any mode.
198bd26fb81SDavid SchultzThe
199bd26fb81SDavid Schultz.Dq Li x
200bd26fb81SDavid Schultzmode option is not meaningful in this context.
201c2974987STim J. Robbins.Pp
20258f0484fSRodney W. GrimesThe primary use of the
20358f0484fSRodney W. Grimes.Fn freopen
20458f0484fSRodney W. Grimesfunction
20558f0484fSRodney W. Grimesis to change the file associated with a
20658f0484fSRodney W. Grimesstandard text stream
207ae828962SRuslan Ermilov.Dv ( stderr , stdin ,
20858f0484fSRodney W. Grimesor
209ae828962SRuslan Ermilov.Dv stdout ) .
21096c95412SPietro Cerutti.Pp
21196c95412SPietro CeruttiThe
21296c95412SPietro Cerutti.Fn fmemopen
21396c95412SPietro Ceruttifunction
21496c95412SPietro Ceruttiassociates the buffer given by the
21596c95412SPietro Cerutti.Fa buf
21696c95412SPietro Ceruttiand
21796c95412SPietro Cerutti.Fa size
21896c95412SPietro Ceruttiarguments with a stream.
21996c95412SPietro CeruttiThe
22096c95412SPietro Cerutti.Fa buf
221646b68f0SPietro Ceruttiargument is either a null pointer or point to a buffer that
22296c95412SPietro Ceruttiis at least
22396c95412SPietro Cerutti.Fa size
22496c95412SPietro Ceruttibytes long.
22596c95412SPietro CeruttiIf a null pointer is specified as the
22696c95412SPietro Cerutti.Fa buf
22796c95412SPietro Ceruttiargument,
22896c95412SPietro Cerutti.Fn fmemopen
229646b68f0SPietro Ceruttiallocates
23096c95412SPietro Cerutti.Fa size
231646b68f0SPietro Ceruttibytes of memory. This buffer is automatically freed when the
232646b68f0SPietro Ceruttistream is closed. Buffers can be opened in text-mode (default) or binary-mode
233646b68f0SPietro Cerutti(if ``b'' is present in the second or third position of the
234646b68f0SPietro Cerutti.Fa mode
235646b68f0SPietro Ceruttiargument). Buffers opened in text-mode make sure that writes are terminated with
236646b68f0SPietro Ceruttia NULL byte, if the last write hasn't filled up the whole buffer. Buffers
237646b68f0SPietro Ceruttiopened in binary-mode never append a NULL byte.
23858f0484fSRodney W. Grimes.Sh RETURN VALUES
23958f0484fSRodney W. GrimesUpon successful completion
24058f0484fSRodney W. Grimes.Fn fopen ,
24158f0484fSRodney W. Grimes.Fn fdopen
24258f0484fSRodney W. Grimesand
24358f0484fSRodney W. Grimes.Fn freopen
24458f0484fSRodney W. Grimesreturn a
24558f0484fSRodney W. Grimes.Tn FILE
24658f0484fSRodney W. Grimespointer.
24758f0484fSRodney W. GrimesOtherwise,
24858f0484fSRodney W. Grimes.Dv NULL
24958f0484fSRodney W. Grimesis returned and the global variable
25058f0484fSRodney W. Grimes.Va errno
25158f0484fSRodney W. Grimesis set to indicate the error.
25258f0484fSRodney W. Grimes.Sh ERRORS
25303fc6303SAlexey Zelkin.Bl -tag -width Er
25458f0484fSRodney W. Grimes.It Bq Er EINVAL
25558f0484fSRodney W. GrimesThe
25658f0484fSRodney W. Grimes.Fa mode
2572efeeba5SRuslan Ermilovargument
2582efeeba5SRuslan Ermilovto
25958f0484fSRodney W. Grimes.Fn fopen ,
26058f0484fSRodney W. Grimes.Fn fdopen ,
26196c95412SPietro Cerutti.Fn freopen ,
26258f0484fSRodney W. Grimesor
26396c95412SPietro Cerutti.Fn fmemopen
26458f0484fSRodney W. Grimeswas invalid.
26558f0484fSRodney W. Grimes.El
26658f0484fSRodney W. Grimes.Pp
26758f0484fSRodney W. GrimesThe
26858f0484fSRodney W. Grimes.Fn fopen ,
26996c95412SPietro Cerutti.Fn fdopen ,
27058f0484fSRodney W. Grimes.Fn freopen
27196c95412SPietro Ceruttiand
27296c95412SPietro Cerutti.Fn fmemopen
27358f0484fSRodney W. Grimesfunctions
27458f0484fSRodney W. Grimesmay also fail and set
27558f0484fSRodney W. Grimes.Va errno
27658f0484fSRodney W. Grimesfor any of the errors specified for the routine
27758f0484fSRodney W. Grimes.Xr malloc 3 .
27858f0484fSRodney W. Grimes.Pp
27958f0484fSRodney W. GrimesThe
28058f0484fSRodney W. Grimes.Fn fopen
28158f0484fSRodney W. Grimesfunction
28258f0484fSRodney W. Grimesmay also fail and set
28358f0484fSRodney W. Grimes.Va errno
28458f0484fSRodney W. Grimesfor any of the errors specified for the routine
28558f0484fSRodney W. Grimes.Xr open 2 .
28658f0484fSRodney W. Grimes.Pp
28758f0484fSRodney W. GrimesThe
28858f0484fSRodney W. Grimes.Fn fdopen
28958f0484fSRodney W. Grimesfunction
29058f0484fSRodney W. Grimesmay also fail and set
29158f0484fSRodney W. Grimes.Va errno
29258f0484fSRodney W. Grimesfor any of the errors specified for the routine
29358f0484fSRodney W. Grimes.Xr fcntl 2 .
29458f0484fSRodney W. Grimes.Pp
29558f0484fSRodney W. GrimesThe
29658f0484fSRodney W. Grimes.Fn freopen
29758f0484fSRodney W. Grimesfunction
29858f0484fSRodney W. Grimesmay also fail and set
29958f0484fSRodney W. Grimes.Va errno
30058f0484fSRodney W. Grimesfor any of the errors specified for the routines
30158f0484fSRodney W. Grimes.Xr open 2 ,
30258f0484fSRodney W. Grimes.Xr fclose 3
30358f0484fSRodney W. Grimesand
30458f0484fSRodney W. Grimes.Xr fflush 3 .
305*4c524a42SPietro Cerutti.Pp
306*4c524a42SPietro CeruttiThe
307*4c524a42SPietro Cerutti.Fn fmemopen
308*4c524a42SPietro Ceruttifunction
309*4c524a42SPietro Ceruttimay also fail and set
310*4c524a42SPietro Cerutti.Va errno
311*4c524a42SPietro Ceruttiif the
312*4c524a42SPietro Cerutti.Fa size
313*4c524a42SPietro Ceruttiargument is 0.
31458f0484fSRodney W. Grimes.Sh SEE ALSO
31558f0484fSRodney W. Grimes.Xr open 2 ,
31658f0484fSRodney W. Grimes.Xr fclose 3 ,
3172f52231cSRuslan Ermilov.Xr fileno 3 ,
31858f0484fSRodney W. Grimes.Xr fseek 3 ,
3192f52231cSRuslan Ermilov.Xr funopen 3
32058f0484fSRodney W. Grimes.Sh STANDARDS
32158f0484fSRodney W. GrimesThe
32258f0484fSRodney W. Grimes.Fn fopen
32358f0484fSRodney W. Grimesand
32458f0484fSRodney W. Grimes.Fn freopen
32558f0484fSRodney W. Grimesfunctions
32658f0484fSRodney W. Grimesconform to
327cc32b2edSJilles Tjoelker.St -isoC ,
328cc32b2edSJilles Tjoelkerwith the exception of the
329cc32b2edSJilles Tjoelker.Dq Li x
330cc32b2edSJilles Tjoelkermode option which conforms to
331cc32b2edSJilles Tjoelker.St -isoC-2011 .
33258f0484fSRodney W. GrimesThe
33358f0484fSRodney W. Grimes.Fn fdopen
33458f0484fSRodney W. Grimesfunction
33558f0484fSRodney W. Grimesconforms to
33658f0484fSRodney W. Grimes.St -p1003.1-88 .
3375ba651f0SJilles TjoelkerThe
3385ba651f0SJilles Tjoelker.Dq Li e
3395ba651f0SJilles Tjoelkermode option does not conform to any standard
3405ba651f0SJilles Tjoelkerbut is also supported by glibc.
34196c95412SPietro CeruttiThe
34296c95412SPietro Cerutti.Fn fmemopen
34396c95412SPietro Ceruttifunction
34496c95412SPietro Cerutticonforms to
34596c95412SPietro Cerutti.St -p1003.1-2008 .
346646b68f0SPietro CeruttiThe ``b'' mode does not conform to any standard
347646b68f0SPietro Ceruttibut is also supported by glibc.
348