xref: /freebsd/lib/libc/stdio/fopen.3 (revision 5ba651f00489791d21b1831cf7101c76fb3b3ae6)
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.\"
35*5ba651f0SJilles Tjoelker.Dd November 30, 2012
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 ,
4158f0484fSRodney W. Grimes.Nm freopen
4258f0484fSRodney W. Grimes.Nd stream open functions
4325bb73e0SAlexey Zelkin.Sh LIBRARY
4425bb73e0SAlexey Zelkin.Lb libc
4558f0484fSRodney W. Grimes.Sh SYNOPSIS
4632eef9aeSRuslan Ermilov.In stdio.h
4758f0484fSRodney W. Grimes.Ft FILE *
4854e4e385SMike Barcroft.Fn fopen "const char * restrict path" "const char * restrict mode"
4958f0484fSRodney W. Grimes.Ft FILE *
5023f0c1fcSBruce Evans.Fn fdopen "int fildes" "const char *mode"
5158f0484fSRodney W. Grimes.Ft FILE *
5223f0c1fcSBruce Evans.Fn freopen "const char *path" "const char *mode" "FILE *stream"
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.
72bd26fb81SDavid SchultzCreate 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.
100*5ba651f0SJilles TjoelkerAn optional
101*5ba651f0SJilles Tjoelker.Dq Li e
102*5ba651f0SJilles Tjoelkerfollowing the above
103*5ba651f0SJilles Tjoelkercauses the
104*5ba651f0SJilles Tjoelker.Fn fopen
105*5ba651f0SJilles Tjoelkercall to set the
106*5ba651f0SJilles Tjoelker.Dv FD_CLOEXEC
107*5ba651f0SJilles 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
11858f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
11958f0484fSRodney W. Grimes.Pp
12058f0484fSRodney W. GrimesAny created files will have mode
1212a10d6d1SUlrich Spörlein.Do Dv S_IRUSR
12258f0484fSRodney W. Grimes\&|
12358f0484fSRodney W. Grimes.Dv S_IWUSR
12458f0484fSRodney W. Grimes\&|
12558f0484fSRodney W. Grimes.Dv S_IRGRP
12658f0484fSRodney W. Grimes\&|
12758f0484fSRodney W. Grimes.Dv S_IWGRP
12858f0484fSRodney W. Grimes\&|
12958f0484fSRodney W. Grimes.Dv S_IROTH
13058f0484fSRodney W. Grimes\&|
1312a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc
13258f0484fSRodney W. Grimes.Pq Li 0666 ,
13358f0484fSRodney W. Grimesas modified by the process'
13458f0484fSRodney W. Grimesumask value (see
13558f0484fSRodney W. Grimes.Xr umask 2 ) .
13658f0484fSRodney W. Grimes.Pp
13758f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
13858f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
13958f0484fSRodney W. Grimes.Em stdio .
14058f0484fSRodney W. GrimesThis is not portable to other systems, however;
14158f0484fSRodney W. Grimes.Tn ANSI C
14258f0484fSRodney W. Grimesrequires that
14358f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
14458f0484fSRodney W. Grimesan input operation encounters end-of-file.
14558f0484fSRodney W. Grimes.Pp
14658f0484fSRodney W. GrimesThe
14758f0484fSRodney W. Grimes.Fn fdopen
14858f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
14958f0484fSRodney W. Grimes.Fa fildes .
1502efeeba5SRuslan ErmilovThe mode
15158f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
152bd26fb81SDavid SchultzThe
153bd26fb81SDavid Schultz.Dq Li x
154bd26fb81SDavid Schultzmode option is ignored.
155*5ba651f0SJilles TjoelkerIf the
156*5ba651f0SJilles Tjoelker.Dq Li e
157*5ba651f0SJilles Tjoelkermode option is present, the
158*5ba651f0SJilles Tjoelker.Dv FD_CLOEXEC
159*5ba651f0SJilles Tjoelkerflag is set, otherwise it remains unchanged.
160f24c2153SArchie CobbsWhen the stream is closed via
161f24c2153SArchie Cobbs.Xr fclose 3 ,
162f24c2153SArchie Cobbs.Fa fildes
163f24c2153SArchie Cobbsis closed also.
16458f0484fSRodney W. Grimes.Pp
16558f0484fSRodney W. GrimesThe
16658f0484fSRodney W. Grimes.Fn freopen
16758f0484fSRodney W. Grimesfunction
16858f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
16958f0484fSRodney W. Grimes.Fa path
17058f0484fSRodney W. Grimesand associates the stream pointed to by
17158f0484fSRodney W. Grimes.Fa stream
17258f0484fSRodney W. Grimeswith it.
17358f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
17458f0484fSRodney W. GrimesThe
17558f0484fSRodney W. Grimes.Fa mode
17658f0484fSRodney W. Grimesargument is used just as in the
177064f0074SMike Pritchard.Fn fopen
17858f0484fSRodney W. Grimesfunction.
179c2974987STim J. Robbins.Pp
180c2974987STim J. RobbinsIf the
181c2974987STim J. Robbins.Fa path
182c2974987STim J. Robbinsargument is
183c2974987STim J. Robbins.Dv NULL ,
184c2974987STim J. Robbins.Fn freopen
185c2974987STim J. Robbinsattempts to re-open the file associated with
186c2974987STim J. Robbins.Fa stream
187c2974987STim J. Robbinswith a new mode.
188c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally
189c2974987STim J. Robbinsopened with:
190bd26fb81SDavid SchultzStreams open for reading can only be re-opened for reading,
191bd26fb81SDavid Schultzstreams open for writing can only be re-opened for writing,
192bd26fb81SDavid Schultzand streams open for reading and writing can be re-opened in any mode.
193bd26fb81SDavid SchultzThe
194bd26fb81SDavid Schultz.Dq Li x
195bd26fb81SDavid Schultzmode option is not meaningful in this context.
196c2974987STim J. Robbins.Pp
19758f0484fSRodney W. GrimesThe primary use of the
19858f0484fSRodney W. Grimes.Fn freopen
19958f0484fSRodney W. Grimesfunction
20058f0484fSRodney W. Grimesis to change the file associated with a
20158f0484fSRodney W. Grimesstandard text stream
202ae828962SRuslan Ermilov.Dv ( stderr , stdin ,
20358f0484fSRodney W. Grimesor
204ae828962SRuslan Ermilov.Dv stdout ) .
20558f0484fSRodney W. Grimes.Sh RETURN VALUES
20658f0484fSRodney W. GrimesUpon successful completion
20758f0484fSRodney W. Grimes.Fn fopen ,
20858f0484fSRodney W. Grimes.Fn fdopen
20958f0484fSRodney W. Grimesand
21058f0484fSRodney W. Grimes.Fn freopen
21158f0484fSRodney W. Grimesreturn a
21258f0484fSRodney W. Grimes.Tn FILE
21358f0484fSRodney W. Grimespointer.
21458f0484fSRodney W. GrimesOtherwise,
21558f0484fSRodney W. Grimes.Dv NULL
21658f0484fSRodney W. Grimesis returned and the global variable
21758f0484fSRodney W. Grimes.Va errno
21858f0484fSRodney W. Grimesis set to indicate the error.
21958f0484fSRodney W. Grimes.Sh ERRORS
22003fc6303SAlexey Zelkin.Bl -tag -width Er
22158f0484fSRodney W. Grimes.It Bq Er EINVAL
22258f0484fSRodney W. GrimesThe
22358f0484fSRodney W. Grimes.Fa mode
2242efeeba5SRuslan Ermilovargument
2252efeeba5SRuslan Ermilovto
22658f0484fSRodney W. Grimes.Fn fopen ,
22758f0484fSRodney W. Grimes.Fn fdopen ,
22858f0484fSRodney W. Grimesor
22958f0484fSRodney W. Grimes.Fn freopen
23058f0484fSRodney W. Grimeswas invalid.
23158f0484fSRodney W. Grimes.El
23258f0484fSRodney W. Grimes.Pp
23358f0484fSRodney W. GrimesThe
23458f0484fSRodney W. Grimes.Fn fopen ,
23558f0484fSRodney W. Grimes.Fn fdopen
23658f0484fSRodney W. Grimesand
23758f0484fSRodney W. Grimes.Fn freopen
23858f0484fSRodney W. Grimesfunctions
23958f0484fSRodney W. Grimesmay also fail and set
24058f0484fSRodney W. Grimes.Va errno
24158f0484fSRodney W. Grimesfor any of the errors specified for the routine
24258f0484fSRodney W. Grimes.Xr malloc 3 .
24358f0484fSRodney W. Grimes.Pp
24458f0484fSRodney W. GrimesThe
24558f0484fSRodney W. Grimes.Fn fopen
24658f0484fSRodney W. Grimesfunction
24758f0484fSRodney W. Grimesmay also fail and set
24858f0484fSRodney W. Grimes.Va errno
24958f0484fSRodney W. Grimesfor any of the errors specified for the routine
25058f0484fSRodney W. Grimes.Xr open 2 .
25158f0484fSRodney W. Grimes.Pp
25258f0484fSRodney W. GrimesThe
25358f0484fSRodney W. Grimes.Fn fdopen
25458f0484fSRodney W. Grimesfunction
25558f0484fSRodney W. Grimesmay also fail and set
25658f0484fSRodney W. Grimes.Va errno
25758f0484fSRodney W. Grimesfor any of the errors specified for the routine
25858f0484fSRodney W. Grimes.Xr fcntl 2 .
25958f0484fSRodney W. Grimes.Pp
26058f0484fSRodney W. GrimesThe
26158f0484fSRodney W. Grimes.Fn freopen
26258f0484fSRodney W. Grimesfunction
26358f0484fSRodney W. Grimesmay also fail and set
26458f0484fSRodney W. Grimes.Va errno
26558f0484fSRodney W. Grimesfor any of the errors specified for the routines
26658f0484fSRodney W. Grimes.Xr open 2 ,
26758f0484fSRodney W. Grimes.Xr fclose 3
26858f0484fSRodney W. Grimesand
26958f0484fSRodney W. Grimes.Xr fflush 3 .
27058f0484fSRodney W. Grimes.Sh SEE ALSO
27158f0484fSRodney W. Grimes.Xr open 2 ,
27258f0484fSRodney W. Grimes.Xr fclose 3 ,
2732f52231cSRuslan Ermilov.Xr fileno 3 ,
27458f0484fSRodney W. Grimes.Xr fseek 3 ,
2752f52231cSRuslan Ermilov.Xr funopen 3
27658f0484fSRodney W. Grimes.Sh STANDARDS
27758f0484fSRodney W. GrimesThe
27858f0484fSRodney W. Grimes.Fn fopen
27958f0484fSRodney W. Grimesand
28058f0484fSRodney W. Grimes.Fn freopen
28158f0484fSRodney W. Grimesfunctions
28258f0484fSRodney W. Grimesconform to
283cc32b2edSJilles Tjoelker.St -isoC ,
284cc32b2edSJilles Tjoelkerwith the exception of the
285cc32b2edSJilles Tjoelker.Dq Li x
286cc32b2edSJilles Tjoelkermode option which conforms to
287cc32b2edSJilles Tjoelker.St -isoC-2011 .
28858f0484fSRodney W. GrimesThe
28958f0484fSRodney W. Grimes.Fn fdopen
29058f0484fSRodney W. Grimesfunction
29158f0484fSRodney W. Grimesconforms to
29258f0484fSRodney W. Grimes.St -p1003.1-88 .
293*5ba651f0SJilles TjoelkerThe
294*5ba651f0SJilles Tjoelker.Dq Li e
295*5ba651f0SJilles Tjoelkermode option does not conform to any standard
296*5ba651f0SJilles Tjoelkerbut is also supported by glibc.
297