xref: /freebsd/lib/libc/stdio/fopen.3 (revision cc32b2eda8d1394e39d997c347bdecb98c5c5fce)
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.\"
35bd26fb81SDavid Schultz.Dd October 17, 2011
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.
100bd26fb81SDavid Schultz.Pp
10158f0484fSRodney W. GrimesThe
10258f0484fSRodney W. Grimes.Fa mode
103bd26fb81SDavid Schultzstring can also include the letter
104bd26fb81SDavid Schultz.Dq Li b
105bd26fb81SDavid Schultzafter either the
106bd26fb81SDavid Schultz.Dq Li +
107bd26fb81SDavid Schultzor the first letter.
10858f0484fSRodney W. GrimesThis is strictly for compatibility with
109588a200cSRuslan Ermilov.St -isoC
11058f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
11158f0484fSRodney W. Grimes.Pp
11258f0484fSRodney W. GrimesAny created files will have mode
1132a10d6d1SUlrich Spörlein.Do Dv S_IRUSR
11458f0484fSRodney W. Grimes\&|
11558f0484fSRodney W. Grimes.Dv S_IWUSR
11658f0484fSRodney W. Grimes\&|
11758f0484fSRodney W. Grimes.Dv S_IRGRP
11858f0484fSRodney W. Grimes\&|
11958f0484fSRodney W. Grimes.Dv S_IWGRP
12058f0484fSRodney W. Grimes\&|
12158f0484fSRodney W. Grimes.Dv S_IROTH
12258f0484fSRodney W. Grimes\&|
1232a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc
12458f0484fSRodney W. Grimes.Pq Li 0666 ,
12558f0484fSRodney W. Grimesas modified by the process'
12658f0484fSRodney W. Grimesumask value (see
12758f0484fSRodney W. Grimes.Xr umask 2 ) .
12858f0484fSRodney W. Grimes.Pp
12958f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
13058f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
13158f0484fSRodney W. Grimes.Em stdio .
13258f0484fSRodney W. GrimesThis is not portable to other systems, however;
13358f0484fSRodney W. Grimes.Tn ANSI C
13458f0484fSRodney W. Grimesrequires that
13558f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
13658f0484fSRodney W. Grimesan input operation encounters end-of-file.
13758f0484fSRodney W. Grimes.Pp
13858f0484fSRodney W. GrimesThe
13958f0484fSRodney W. Grimes.Fn fdopen
14058f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
14158f0484fSRodney W. Grimes.Fa fildes .
1422efeeba5SRuslan ErmilovThe mode
14358f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
144bd26fb81SDavid SchultzThe
145bd26fb81SDavid Schultz.Dq Li x
146bd26fb81SDavid Schultzmode option is ignored.
147f24c2153SArchie CobbsWhen the stream is closed via
148f24c2153SArchie Cobbs.Xr fclose 3 ,
149f24c2153SArchie Cobbs.Fa fildes
150f24c2153SArchie Cobbsis closed also.
15158f0484fSRodney W. Grimes.Pp
15258f0484fSRodney W. GrimesThe
15358f0484fSRodney W. Grimes.Fn freopen
15458f0484fSRodney W. Grimesfunction
15558f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
15658f0484fSRodney W. Grimes.Fa path
15758f0484fSRodney W. Grimesand associates the stream pointed to by
15858f0484fSRodney W. Grimes.Fa stream
15958f0484fSRodney W. Grimeswith it.
16058f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
16158f0484fSRodney W. GrimesThe
16258f0484fSRodney W. Grimes.Fa mode
16358f0484fSRodney W. Grimesargument is used just as in the
164064f0074SMike Pritchard.Fn fopen
16558f0484fSRodney W. Grimesfunction.
166c2974987STim J. Robbins.Pp
167c2974987STim J. RobbinsIf the
168c2974987STim J. Robbins.Fa path
169c2974987STim J. Robbinsargument is
170c2974987STim J. Robbins.Dv NULL ,
171c2974987STim J. Robbins.Fn freopen
172c2974987STim J. Robbinsattempts to re-open the file associated with
173c2974987STim J. Robbins.Fa stream
174c2974987STim J. Robbinswith a new mode.
175c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally
176c2974987STim J. Robbinsopened with:
177bd26fb81SDavid SchultzStreams open for reading can only be re-opened for reading,
178bd26fb81SDavid Schultzstreams open for writing can only be re-opened for writing,
179bd26fb81SDavid Schultzand streams open for reading and writing can be re-opened in any mode.
180bd26fb81SDavid SchultzThe
181bd26fb81SDavid Schultz.Dq Li x
182bd26fb81SDavid Schultzmode option is not meaningful in this context.
183c2974987STim J. Robbins.Pp
18458f0484fSRodney W. GrimesThe primary use of the
18558f0484fSRodney W. Grimes.Fn freopen
18658f0484fSRodney W. Grimesfunction
18758f0484fSRodney W. Grimesis to change the file associated with a
18858f0484fSRodney W. Grimesstandard text stream
189ae828962SRuslan Ermilov.Dv ( stderr , stdin ,
19058f0484fSRodney W. Grimesor
191ae828962SRuslan Ermilov.Dv stdout ) .
19258f0484fSRodney W. Grimes.Sh RETURN VALUES
19358f0484fSRodney W. GrimesUpon successful completion
19458f0484fSRodney W. Grimes.Fn fopen ,
19558f0484fSRodney W. Grimes.Fn fdopen
19658f0484fSRodney W. Grimesand
19758f0484fSRodney W. Grimes.Fn freopen
19858f0484fSRodney W. Grimesreturn a
19958f0484fSRodney W. Grimes.Tn FILE
20058f0484fSRodney W. Grimespointer.
20158f0484fSRodney W. GrimesOtherwise,
20258f0484fSRodney W. Grimes.Dv NULL
20358f0484fSRodney W. Grimesis returned and the global variable
20458f0484fSRodney W. Grimes.Va errno
20558f0484fSRodney W. Grimesis set to indicate the error.
20658f0484fSRodney W. Grimes.Sh ERRORS
20703fc6303SAlexey Zelkin.Bl -tag -width Er
20858f0484fSRodney W. Grimes.It Bq Er EINVAL
20958f0484fSRodney W. GrimesThe
21058f0484fSRodney W. Grimes.Fa mode
2112efeeba5SRuslan Ermilovargument
2122efeeba5SRuslan Ermilovto
21358f0484fSRodney W. Grimes.Fn fopen ,
21458f0484fSRodney W. Grimes.Fn fdopen ,
21558f0484fSRodney W. Grimesor
21658f0484fSRodney W. Grimes.Fn freopen
21758f0484fSRodney W. Grimeswas invalid.
21858f0484fSRodney W. Grimes.El
21958f0484fSRodney W. Grimes.Pp
22058f0484fSRodney W. GrimesThe
22158f0484fSRodney W. Grimes.Fn fopen ,
22258f0484fSRodney W. Grimes.Fn fdopen
22358f0484fSRodney W. Grimesand
22458f0484fSRodney W. Grimes.Fn freopen
22558f0484fSRodney W. Grimesfunctions
22658f0484fSRodney W. Grimesmay also fail and set
22758f0484fSRodney W. Grimes.Va errno
22858f0484fSRodney W. Grimesfor any of the errors specified for the routine
22958f0484fSRodney W. Grimes.Xr malloc 3 .
23058f0484fSRodney W. Grimes.Pp
23158f0484fSRodney W. GrimesThe
23258f0484fSRodney W. Grimes.Fn fopen
23358f0484fSRodney W. Grimesfunction
23458f0484fSRodney W. Grimesmay also fail and set
23558f0484fSRodney W. Grimes.Va errno
23658f0484fSRodney W. Grimesfor any of the errors specified for the routine
23758f0484fSRodney W. Grimes.Xr open 2 .
23858f0484fSRodney W. Grimes.Pp
23958f0484fSRodney W. GrimesThe
24058f0484fSRodney W. Grimes.Fn fdopen
24158f0484fSRodney W. Grimesfunction
24258f0484fSRodney W. Grimesmay also fail and set
24358f0484fSRodney W. Grimes.Va errno
24458f0484fSRodney W. Grimesfor any of the errors specified for the routine
24558f0484fSRodney W. Grimes.Xr fcntl 2 .
24658f0484fSRodney W. Grimes.Pp
24758f0484fSRodney W. GrimesThe
24858f0484fSRodney W. Grimes.Fn freopen
24958f0484fSRodney W. Grimesfunction
25058f0484fSRodney W. Grimesmay also fail and set
25158f0484fSRodney W. Grimes.Va errno
25258f0484fSRodney W. Grimesfor any of the errors specified for the routines
25358f0484fSRodney W. Grimes.Xr open 2 ,
25458f0484fSRodney W. Grimes.Xr fclose 3
25558f0484fSRodney W. Grimesand
25658f0484fSRodney W. Grimes.Xr fflush 3 .
25758f0484fSRodney W. Grimes.Sh SEE ALSO
25858f0484fSRodney W. Grimes.Xr open 2 ,
25958f0484fSRodney W. Grimes.Xr fclose 3 ,
2602f52231cSRuslan Ermilov.Xr fileno 3 ,
26158f0484fSRodney W. Grimes.Xr fseek 3 ,
2622f52231cSRuslan Ermilov.Xr funopen 3
26358f0484fSRodney W. Grimes.Sh STANDARDS
26458f0484fSRodney W. GrimesThe
26558f0484fSRodney W. Grimes.Fn fopen
26658f0484fSRodney W. Grimesand
26758f0484fSRodney W. Grimes.Fn freopen
26858f0484fSRodney W. Grimesfunctions
26958f0484fSRodney W. Grimesconform to
270*cc32b2edSJilles Tjoelker.St -isoC ,
271*cc32b2edSJilles Tjoelkerwith the exception of the
272*cc32b2edSJilles Tjoelker.Dq Li x
273*cc32b2edSJilles Tjoelkermode option which conforms to
274*cc32b2edSJilles Tjoelker.St -isoC-2011 .
27558f0484fSRodney W. GrimesThe
27658f0484fSRodney W. Grimes.Fn fdopen
27758f0484fSRodney W. Grimesfunction
27858f0484fSRodney W. Grimesconforms to
27958f0484fSRodney W. Grimes.St -p1003.1-88 .
280