xref: /freebsd/lib/libc/stdio/fopen.3 (revision 2a10d6d199a26685818450d21975a0f7f7067085)
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.\"
35c2974987STim J. Robbins.Dd January 26, 2003
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
6358f0484fSRodney W. Grimespoints to a string beginning with one of the following
6458f0484fSRodney W. Grimessequences (Additional characters may follow these sequences.):
6558f0484fSRodney W. Grimes.Bl -tag -width indent
6658f0484fSRodney W. Grimes.It Dq Li r
6758f0484fSRodney W. GrimesOpen text file for reading.
6858f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
6958f0484fSRodney W. Grimes.It Dq Li r+
7058f0484fSRodney W. GrimesOpen for reading and writing.
7158f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7258f0484fSRodney W. Grimes.It Dq Li w
73bc8695dcSDag-Erling SmørgravTruncate to zero length or create text file for writing.
7458f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
756c0aebfaSMike Pritchard.It Dq Li w+
7658f0484fSRodney W. GrimesOpen for reading and writing.
7758f0484fSRodney W. GrimesThe file is created if it does not exist, otherwise it is truncated.
7858f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7958f0484fSRodney W. Grimes.It Dq Li a
8058f0484fSRodney W. GrimesOpen for writing.
8158f0484fSRodney W. GrimesThe file is created if it does not exist.
8258f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
8311ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current
8411ab3fc7SEivind Eklundend of file, irrespective of any intervening
8511ab3fc7SEivind Eklund.Xr fseek 3
8611ab3fc7SEivind Eklundor similar.
8758f0484fSRodney W. Grimes.It Dq Li a+
8858f0484fSRodney W. GrimesOpen for reading and writing.
8958f0484fSRodney W. GrimesThe file is created if it does not exist.
9058f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
9111ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current
9211ab3fc7SEivind Eklundend of file, irrespective of any intervening
9311ab3fc7SEivind Eklund.Xr fseek 3
9411ab3fc7SEivind Eklundor similar.
9558f0484fSRodney W. Grimes.El
9658f0484fSRodney W. Grimes.Pp
9758f0484fSRodney W. GrimesThe
9858f0484fSRodney W. Grimes.Fa mode
994522791bSEd Schoutenstring can also include the letter ``b'' either as last character or
10058f0484fSRodney W. Grimesas a character between the characters in any of the two-character strings
10158f0484fSRodney W. Grimesdescribed above.
10258f0484fSRodney W. GrimesThis is strictly for compatibility with
103588a200cSRuslan Ermilov.St -isoC
10458f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
10558f0484fSRodney W. Grimes.Pp
10658f0484fSRodney W. GrimesAny created files will have mode
107*2a10d6d1SUlrich Spörlein.Do Dv S_IRUSR
10858f0484fSRodney W. Grimes\&|
10958f0484fSRodney W. Grimes.Dv S_IWUSR
11058f0484fSRodney W. Grimes\&|
11158f0484fSRodney W. Grimes.Dv S_IRGRP
11258f0484fSRodney W. Grimes\&|
11358f0484fSRodney W. Grimes.Dv S_IWGRP
11458f0484fSRodney W. Grimes\&|
11558f0484fSRodney W. Grimes.Dv S_IROTH
11658f0484fSRodney W. Grimes\&|
117*2a10d6d1SUlrich Spörlein.Dv S_IWOTH Dc
11858f0484fSRodney W. Grimes.Pq Li 0666 ,
11958f0484fSRodney W. Grimesas modified by the process'
12058f0484fSRodney W. Grimesumask value (see
12158f0484fSRodney W. Grimes.Xr umask 2 ) .
12258f0484fSRodney W. Grimes.Pp
12358f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
12458f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
12558f0484fSRodney W. Grimes.Em stdio .
12658f0484fSRodney W. GrimesThis is not portable to other systems, however;
12758f0484fSRodney W. Grimes.Tn ANSI C
12858f0484fSRodney W. Grimesrequires that
12958f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
13058f0484fSRodney W. Grimesan input operation encounters end-of-file.
13158f0484fSRodney W. Grimes.Pp
13258f0484fSRodney W. GrimesThe
13358f0484fSRodney W. Grimes.Fn fdopen
13458f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
13558f0484fSRodney W. Grimes.Fa fildes .
1362efeeba5SRuslan ErmilovThe mode
13758f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
138f24c2153SArchie CobbsWhen the stream is closed via
139f24c2153SArchie Cobbs.Xr fclose 3 ,
140f24c2153SArchie Cobbs.Fa fildes
141f24c2153SArchie Cobbsis closed also.
14258f0484fSRodney W. Grimes.Pp
14358f0484fSRodney W. GrimesThe
14458f0484fSRodney W. Grimes.Fn freopen
14558f0484fSRodney W. Grimesfunction
14658f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
14758f0484fSRodney W. Grimes.Fa path
14858f0484fSRodney W. Grimesand associates the stream pointed to by
14958f0484fSRodney W. Grimes.Fa stream
15058f0484fSRodney W. Grimeswith it.
15158f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
15258f0484fSRodney W. GrimesThe
15358f0484fSRodney W. Grimes.Fa mode
15458f0484fSRodney W. Grimesargument is used just as in the
155064f0074SMike Pritchard.Fn fopen
15658f0484fSRodney W. Grimesfunction.
157c2974987STim J. Robbins.Pp
158c2974987STim J. RobbinsIf the
159c2974987STim J. Robbins.Fa path
160c2974987STim J. Robbinsargument is
161c2974987STim J. Robbins.Dv NULL ,
162c2974987STim J. Robbins.Fn freopen
163c2974987STim J. Robbinsattempts to re-open the file associated with
164c2974987STim J. Robbins.Fa stream
165c2974987STim J. Robbinswith a new mode.
166c2974987STim J. RobbinsThe new mode must be compatible with the mode that the stream was originally
167c2974987STim J. Robbinsopened with:
168c2974987STim J. Robbins.Bl -bullet -offset indent
169c2974987STim J. Robbins.It
170c2974987STim J. RobbinsStreams originally opened with mode
171c2974987STim J. Robbins.Dq Li r
172c2974987STim J. Robbinscan only be reopened with that same mode.
173c2974987STim J. Robbins.It
174c2974987STim J. RobbinsStreams originally opened with mode
175c2974987STim J. Robbins.Dq Li a
176c2974987STim J. Robbinscan be reopened with the same mode, or mode
177c2974987STim J. Robbins.Dq Li w .
178c2974987STim J. Robbins.It
179c2974987STim J. RobbinsStreams originally opened with mode
180c2974987STim J. Robbins.Dq Li w
181c2974987STim J. Robbinscan be reopened with the same mode, or mode
182c2974987STim J. Robbins.Dq Li a .
183c2974987STim J. Robbins.It
184c2974987STim J. RobbinsStreams originally opened with mode
185c2974987STim J. Robbins.Dq Li r+ ,
186c2974987STim J. Robbins.Dq Li w+ ,
187c2974987STim J. Robbinsor
188c2974987STim J. Robbins.Dq Li a+
189c2974987STim J. Robbinscan be reopened with any mode.
190c2974987STim J. Robbins.El
191c2974987STim J. Robbins.Pp
19258f0484fSRodney W. GrimesThe primary use of the
19358f0484fSRodney W. Grimes.Fn freopen
19458f0484fSRodney W. Grimesfunction
19558f0484fSRodney W. Grimesis to change the file associated with a
19658f0484fSRodney W. Grimesstandard text stream
197ae828962SRuslan Ermilov.Dv ( stderr , stdin ,
19858f0484fSRodney W. Grimesor
199ae828962SRuslan Ermilov.Dv stdout ) .
20058f0484fSRodney W. Grimes.Sh RETURN VALUES
20158f0484fSRodney W. GrimesUpon successful completion
20258f0484fSRodney W. Grimes.Fn fopen ,
20358f0484fSRodney W. Grimes.Fn fdopen
20458f0484fSRodney W. Grimesand
20558f0484fSRodney W. Grimes.Fn freopen
20658f0484fSRodney W. Grimesreturn a
20758f0484fSRodney W. Grimes.Tn FILE
20858f0484fSRodney W. Grimespointer.
20958f0484fSRodney W. GrimesOtherwise,
21058f0484fSRodney W. Grimes.Dv NULL
21158f0484fSRodney W. Grimesis returned and the global variable
21258f0484fSRodney W. Grimes.Va errno
21358f0484fSRodney W. Grimesis set to indicate the error.
21458f0484fSRodney W. Grimes.Sh ERRORS
21503fc6303SAlexey Zelkin.Bl -tag -width Er
21658f0484fSRodney W. Grimes.It Bq Er EINVAL
21758f0484fSRodney W. GrimesThe
21858f0484fSRodney W. Grimes.Fa mode
2192efeeba5SRuslan Ermilovargument
2202efeeba5SRuslan Ermilovto
22158f0484fSRodney W. Grimes.Fn fopen ,
22258f0484fSRodney W. Grimes.Fn fdopen ,
22358f0484fSRodney W. Grimesor
22458f0484fSRodney W. Grimes.Fn freopen
22558f0484fSRodney W. Grimeswas invalid.
22658f0484fSRodney W. Grimes.El
22758f0484fSRodney W. Grimes.Pp
22858f0484fSRodney W. GrimesThe
22958f0484fSRodney W. Grimes.Fn fopen ,
23058f0484fSRodney W. Grimes.Fn fdopen
23158f0484fSRodney W. Grimesand
23258f0484fSRodney W. Grimes.Fn freopen
23358f0484fSRodney W. Grimesfunctions
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 malloc 3 .
23858f0484fSRodney W. Grimes.Pp
23958f0484fSRodney W. GrimesThe
24058f0484fSRodney W. Grimes.Fn fopen
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 open 2 .
24658f0484fSRodney W. Grimes.Pp
24758f0484fSRodney W. GrimesThe
24858f0484fSRodney W. Grimes.Fn fdopen
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 routine
25358f0484fSRodney W. Grimes.Xr fcntl 2 .
25458f0484fSRodney W. Grimes.Pp
25558f0484fSRodney W. GrimesThe
25658f0484fSRodney W. Grimes.Fn freopen
25758f0484fSRodney W. Grimesfunction
25858f0484fSRodney W. Grimesmay also fail and set
25958f0484fSRodney W. Grimes.Va errno
26058f0484fSRodney W. Grimesfor any of the errors specified for the routines
26158f0484fSRodney W. Grimes.Xr open 2 ,
26258f0484fSRodney W. Grimes.Xr fclose 3
26358f0484fSRodney W. Grimesand
26458f0484fSRodney W. Grimes.Xr fflush 3 .
26558f0484fSRodney W. Grimes.Sh SEE ALSO
26658f0484fSRodney W. Grimes.Xr open 2 ,
26758f0484fSRodney W. Grimes.Xr fclose 3 ,
2682f52231cSRuslan Ermilov.Xr fileno 3 ,
26958f0484fSRodney W. Grimes.Xr fseek 3 ,
2702f52231cSRuslan Ermilov.Xr funopen 3
27158f0484fSRodney W. Grimes.Sh STANDARDS
27258f0484fSRodney W. GrimesThe
27358f0484fSRodney W. Grimes.Fn fopen
27458f0484fSRodney W. Grimesand
27558f0484fSRodney W. Grimes.Fn freopen
27658f0484fSRodney W. Grimesfunctions
27758f0484fSRodney W. Grimesconform to
278588a200cSRuslan Ermilov.St -isoC .
27958f0484fSRodney W. GrimesThe
28058f0484fSRodney W. Grimes.Fn fdopen
28158f0484fSRodney W. Grimesfunction
28258f0484fSRodney W. Grimesconforms to
28358f0484fSRodney W. Grimes.St -p1003.1-88 .
284