xref: /freebsd/lib/libc/stdio/fopen.3 (revision 11ab3fc7cbfd3d8b74348255ba3a395d2626ab1e)
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.\" 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1858f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1958f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes.\"    without specific prior written permission.
2358f0484fSRodney W. Grimes.\"
2458f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3558f0484fSRodney W. Grimes.\"
3658f0484fSRodney W. Grimes.\"     @(#)fopen.3	8.1 (Berkeley) 6/4/93
377f3dea24SPeter Wemm.\" $FreeBSD$
3858f0484fSRodney W. Grimes.\"
3958f0484fSRodney W. Grimes.Dd June 4, 1993
4058f0484fSRodney W. Grimes.Dt FOPEN 3
4158f0484fSRodney W. Grimes.Os
4258f0484fSRodney W. Grimes.Sh NAME
4358f0484fSRodney W. Grimes.Nm fopen ,
4458f0484fSRodney W. Grimes.Nm fdopen ,
4558f0484fSRodney W. Grimes.Nm freopen
4658f0484fSRodney W. Grimes.Nd stream open functions
4725bb73e0SAlexey Zelkin.Sh LIBRARY
4825bb73e0SAlexey Zelkin.Lb libc
4958f0484fSRodney W. Grimes.Sh SYNOPSIS
5058f0484fSRodney W. Grimes.Fd #include <stdio.h>
5158f0484fSRodney W. Grimes.Ft FILE *
5223f0c1fcSBruce Evans.Fn fopen "const char *path" "const char *mode"
5358f0484fSRodney W. Grimes.Ft FILE *
5423f0c1fcSBruce Evans.Fn fdopen "int fildes" "const char *mode"
5558f0484fSRodney W. Grimes.Ft FILE *
5623f0c1fcSBruce Evans.Fn freopen "const char *path" "const char *mode" "FILE *stream"
5758f0484fSRodney W. Grimes.Sh DESCRIPTION
5858f0484fSRodney W. GrimesThe
5958f0484fSRodney W. Grimes.Fn fopen
6058f0484fSRodney W. Grimesfunction
6158f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
6258f0484fSRodney W. Grimes.Fa path
6358f0484fSRodney W. Grimesand associates a stream with it.
6458f0484fSRodney W. Grimes.Pp
6558f0484fSRodney W. GrimesThe argument
6658f0484fSRodney W. Grimes.Fa mode
6758f0484fSRodney W. Grimespoints to a string beginning with one of the following
6858f0484fSRodney W. Grimessequences (Additional characters may follow these sequences.):
6958f0484fSRodney W. Grimes.Bl -tag -width indent
7058f0484fSRodney W. Grimes.It Dq Li r
7158f0484fSRodney W. GrimesOpen text file for reading.
7258f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7358f0484fSRodney W. Grimes.It Dq Li r+
7458f0484fSRodney W. GrimesOpen for reading and writing.
7558f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7658f0484fSRodney W. Grimes.It Dq Li w
7758f0484fSRodney W. GrimesTruncate file to zero length or create text file for writing.
7858f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
796c0aebfaSMike Pritchard.It Dq Li w+
8058f0484fSRodney W. GrimesOpen for reading and writing.
8158f0484fSRodney W. GrimesThe file is created if it does not exist, otherwise it is truncated.
8258f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
8358f0484fSRodney W. Grimes.It Dq Li a
8458f0484fSRodney W. GrimesOpen for writing.
8558f0484fSRodney W. GrimesThe file is created if it does not exist.
8658f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
8711ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current
8811ab3fc7SEivind Eklundend of file, irrespective of any intervening
8911ab3fc7SEivind Eklund.Xr fseek 3
9011ab3fc7SEivind Eklundor similar.
9158f0484fSRodney W. Grimes.It Dq Li a+
9258f0484fSRodney W. GrimesOpen for reading and writing.
9358f0484fSRodney W. GrimesThe file is created if it does not exist.
9458f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
9511ab3fc7SEivind EklundSubsequent writes to the file will always end up at the then current
9611ab3fc7SEivind Eklundend of file, irrespective of any intervening
9711ab3fc7SEivind Eklund.Xr fseek 3
9811ab3fc7SEivind Eklundor similar.
9958f0484fSRodney W. Grimes.El
10058f0484fSRodney W. Grimes.Pp
10158f0484fSRodney W. GrimesThe
10258f0484fSRodney W. Grimes.Fa mode
10358f0484fSRodney W. Grimesstring can also include the letter ``b'' either as a third character or
10458f0484fSRodney W. Grimesas a character between the characters in any of the two-character strings
10558f0484fSRodney W. Grimesdescribed above.
10658f0484fSRodney W. GrimesThis is strictly for compatibility with
10758f0484fSRodney W. Grimes.St -ansiC
10858f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
10958f0484fSRodney W. Grimes.Pp
11058f0484fSRodney W. GrimesAny created files will have mode
11158f0484fSRodney W. Grimes.Pf \\*q Dv S_IRUSR
11258f0484fSRodney W. Grimes\&|
11358f0484fSRodney W. Grimes.Dv S_IWUSR
11458f0484fSRodney W. Grimes\&|
11558f0484fSRodney W. Grimes.Dv S_IRGRP
11658f0484fSRodney W. Grimes\&|
11758f0484fSRodney W. Grimes.Dv S_IWGRP
11858f0484fSRodney W. Grimes\&|
11958f0484fSRodney W. Grimes.Dv S_IROTH
12058f0484fSRodney W. Grimes\&|
12158f0484fSRodney W. Grimes.Dv S_IWOTH Ns \\*q
12258f0484fSRodney W. Grimes.Pq Li 0666 ,
12358f0484fSRodney W. Grimesas modified by the process'
12458f0484fSRodney W. Grimesumask value (see
12558f0484fSRodney W. Grimes.Xr umask 2 ) .
12658f0484fSRodney W. Grimes.Pp
12758f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
12858f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
12958f0484fSRodney W. Grimes.Em stdio .
13058f0484fSRodney W. GrimesThis is not portable to other systems, however;
13158f0484fSRodney W. Grimes.Tn ANSI C
13258f0484fSRodney W. Grimesrequires that
13358f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
13458f0484fSRodney W. Grimesan input operation encounters end-of-file.
13558f0484fSRodney W. Grimes.Pp
13658f0484fSRodney W. GrimesThe
13758f0484fSRodney W. Grimes.Fn fdopen
13858f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
13958f0484fSRodney W. Grimes.Fa fildes .
14058f0484fSRodney W. GrimesThe
14158f0484fSRodney W. Grimes.Fa mode
14258f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
143f24c2153SArchie CobbsWhen the stream is closed via
144f24c2153SArchie Cobbs.Xr fclose 3 ,
145f24c2153SArchie Cobbs.Fa fildes
146f24c2153SArchie Cobbsis closed also.
14758f0484fSRodney W. Grimes.Pp
14858f0484fSRodney W. GrimesThe
14958f0484fSRodney W. Grimes.Fn freopen
15058f0484fSRodney W. Grimesfunction
15158f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
15258f0484fSRodney W. Grimes.Fa path
15358f0484fSRodney W. Grimesand associates the stream pointed to by
15458f0484fSRodney W. Grimes.Fa stream
15558f0484fSRodney W. Grimeswith it.
15658f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
15758f0484fSRodney W. GrimesThe
15858f0484fSRodney W. Grimes.Fa mode
15958f0484fSRodney W. Grimesargument is used just as in the
160064f0074SMike Pritchard.Fn fopen
16158f0484fSRodney W. Grimesfunction.
16258f0484fSRodney W. GrimesThe primary use of the
16358f0484fSRodney W. Grimes.Fn freopen
16458f0484fSRodney W. Grimesfunction
16558f0484fSRodney W. Grimesis to change the file associated with a
16658f0484fSRodney W. Grimesstandard text stream
16758f0484fSRodney W. Grimes.Pf ( Em stderr ,
16858f0484fSRodney W. Grimes.Em stdin ,
16958f0484fSRodney W. Grimesor
17058f0484fSRodney W. Grimes.Em stdout ) .
17158f0484fSRodney W. Grimes.Sh RETURN VALUES
17258f0484fSRodney W. GrimesUpon successful completion
17358f0484fSRodney W. Grimes.Fn fopen ,
17458f0484fSRodney W. Grimes.Fn fdopen
17558f0484fSRodney W. Grimesand
17658f0484fSRodney W. Grimes.Fn freopen
17758f0484fSRodney W. Grimesreturn a
17858f0484fSRodney W. Grimes.Tn FILE
17958f0484fSRodney W. Grimespointer.
18058f0484fSRodney W. GrimesOtherwise,
18158f0484fSRodney W. Grimes.Dv NULL
18258f0484fSRodney W. Grimesis returned and the global variable
18358f0484fSRodney W. Grimes.Va errno
18458f0484fSRodney W. Grimesis set to indicate the error.
18558f0484fSRodney W. Grimes.Sh ERRORS
18603fc6303SAlexey Zelkin.Bl -tag -width Er
18758f0484fSRodney W. Grimes.It Bq Er EINVAL
18858f0484fSRodney W. GrimesThe
18958f0484fSRodney W. Grimes.Fa mode
19058f0484fSRodney W. Grimesprovided to
19158f0484fSRodney W. Grimes.Fn fopen ,
19258f0484fSRodney W. Grimes.Fn fdopen ,
19358f0484fSRodney W. Grimesor
19458f0484fSRodney W. Grimes.Fn freopen
19558f0484fSRodney W. Grimeswas invalid.
19658f0484fSRodney W. Grimes.El
19758f0484fSRodney W. Grimes.Pp
19858f0484fSRodney W. GrimesThe
19958f0484fSRodney W. Grimes.Fn fopen ,
20058f0484fSRodney W. Grimes.Fn fdopen
20158f0484fSRodney W. Grimesand
20258f0484fSRodney W. Grimes.Fn freopen
20358f0484fSRodney W. Grimesfunctions
20458f0484fSRodney W. Grimesmay also fail and set
20558f0484fSRodney W. Grimes.Va errno
20658f0484fSRodney W. Grimesfor any of the errors specified for the routine
20758f0484fSRodney W. Grimes.Xr malloc 3 .
20858f0484fSRodney W. Grimes.Pp
20958f0484fSRodney W. GrimesThe
21058f0484fSRodney W. Grimes.Fn fopen
21158f0484fSRodney W. Grimesfunction
21258f0484fSRodney W. Grimesmay also fail and set
21358f0484fSRodney W. Grimes.Va errno
21458f0484fSRodney W. Grimesfor any of the errors specified for the routine
21558f0484fSRodney W. Grimes.Xr open 2 .
21658f0484fSRodney W. Grimes.Pp
21758f0484fSRodney W. GrimesThe
21858f0484fSRodney W. Grimes.Fn fdopen
21958f0484fSRodney W. Grimesfunction
22058f0484fSRodney W. Grimesmay also fail and set
22158f0484fSRodney W. Grimes.Va errno
22258f0484fSRodney W. Grimesfor any of the errors specified for the routine
22358f0484fSRodney W. Grimes.Xr fcntl 2 .
22458f0484fSRodney W. Grimes.Pp
22558f0484fSRodney W. GrimesThe
22658f0484fSRodney W. Grimes.Fn freopen
22758f0484fSRodney W. Grimesfunction
22858f0484fSRodney W. Grimesmay also fail and set
22958f0484fSRodney W. Grimes.Va errno
23058f0484fSRodney W. Grimesfor any of the errors specified for the routines
23158f0484fSRodney W. Grimes.Xr open 2 ,
23258f0484fSRodney W. Grimes.Xr fclose 3
23358f0484fSRodney W. Grimesand
23458f0484fSRodney W. Grimes.Xr fflush 3 .
23558f0484fSRodney W. Grimes.Sh SEE ALSO
23658f0484fSRodney W. Grimes.Xr open 2 ,
23758f0484fSRodney W. Grimes.Xr fclose 3 ,
23858f0484fSRodney W. Grimes.Xr fseek 3 ,
23958f0484fSRodney W. Grimes.Xr funopen 3
24058f0484fSRodney W. Grimes.Sh STANDARDS
24158f0484fSRodney W. GrimesThe
24258f0484fSRodney W. Grimes.Fn fopen
24358f0484fSRodney W. Grimesand
24458f0484fSRodney W. Grimes.Fn freopen
24558f0484fSRodney W. Grimesfunctions
24658f0484fSRodney W. Grimesconform to
24758f0484fSRodney W. Grimes.St -ansiC .
24858f0484fSRodney W. GrimesThe
24958f0484fSRodney W. Grimes.Fn fdopen
25058f0484fSRodney W. Grimesfunction
25158f0484fSRodney W. Grimesconforms to
25258f0484fSRodney W. Grimes.St -p1003.1-88 .
253