xref: /freebsd/lib/libc/stdio/fopen.3 (revision 25bb73e063c17cd9048cf60100dbc0ac5177e94a)
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.
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.
9158f0484fSRodney W. Grimes.El
9258f0484fSRodney W. Grimes.Pp
9358f0484fSRodney W. GrimesThe
9458f0484fSRodney W. Grimes.Fa mode
9558f0484fSRodney W. Grimesstring can also include the letter ``b'' either as a third character or
9658f0484fSRodney W. Grimesas a character between the characters in any of the two-character strings
9758f0484fSRodney W. Grimesdescribed above.
9858f0484fSRodney W. GrimesThis is strictly for compatibility with
9958f0484fSRodney W. Grimes.St -ansiC
10058f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
10158f0484fSRodney W. Grimes.Pp
10258f0484fSRodney W. GrimesAny created files will have mode
10358f0484fSRodney W. Grimes.Pf \\*q Dv S_IRUSR
10458f0484fSRodney W. Grimes\&|
10558f0484fSRodney W. Grimes.Dv S_IWUSR
10658f0484fSRodney W. Grimes\&|
10758f0484fSRodney W. Grimes.Dv S_IRGRP
10858f0484fSRodney W. Grimes\&|
10958f0484fSRodney W. Grimes.Dv S_IWGRP
11058f0484fSRodney W. Grimes\&|
11158f0484fSRodney W. Grimes.Dv S_IROTH
11258f0484fSRodney W. Grimes\&|
11358f0484fSRodney W. Grimes.Dv S_IWOTH Ns \\*q
11458f0484fSRodney W. Grimes.Pq Li 0666 ,
11558f0484fSRodney W. Grimesas modified by the process'
11658f0484fSRodney W. Grimesumask value (see
11758f0484fSRodney W. Grimes.Xr umask 2 ) .
11858f0484fSRodney W. Grimes.Pp
11958f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
12058f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
12158f0484fSRodney W. Grimes.Em stdio .
12258f0484fSRodney W. GrimesThis is not portable to other systems, however;
12358f0484fSRodney W. Grimes.Tn ANSI C
12458f0484fSRodney W. Grimesrequires that
12558f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
12658f0484fSRodney W. Grimesan input operation encounters end-of-file.
12758f0484fSRodney W. Grimes.Pp
12858f0484fSRodney W. GrimesThe
12958f0484fSRodney W. Grimes.Fn fdopen
13058f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
13158f0484fSRodney W. Grimes.Fa fildes .
13258f0484fSRodney W. GrimesThe
13358f0484fSRodney W. Grimes.Fa mode
13458f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
135f24c2153SArchie CobbsWhen the stream is closed via
136f24c2153SArchie Cobbs.Xr fclose 3 ,
137f24c2153SArchie Cobbs.Fa fildes
138f24c2153SArchie Cobbsis closed also.
13958f0484fSRodney W. Grimes.Pp
14058f0484fSRodney W. GrimesThe
14158f0484fSRodney W. Grimes.Fn freopen
14258f0484fSRodney W. Grimesfunction
14358f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
14458f0484fSRodney W. Grimes.Fa path
14558f0484fSRodney W. Grimesand associates the stream pointed to by
14658f0484fSRodney W. Grimes.Fa stream
14758f0484fSRodney W. Grimeswith it.
14858f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
14958f0484fSRodney W. GrimesThe
15058f0484fSRodney W. Grimes.Fa mode
15158f0484fSRodney W. Grimesargument is used just as in the
152064f0074SMike Pritchard.Fn fopen
15358f0484fSRodney W. Grimesfunction.
15458f0484fSRodney W. GrimesThe primary use of the
15558f0484fSRodney W. Grimes.Fn freopen
15658f0484fSRodney W. Grimesfunction
15758f0484fSRodney W. Grimesis to change the file associated with a
15858f0484fSRodney W. Grimesstandard text stream
15958f0484fSRodney W. Grimes.Pf ( Em stderr ,
16058f0484fSRodney W. Grimes.Em stdin ,
16158f0484fSRodney W. Grimesor
16258f0484fSRodney W. Grimes.Em stdout ) .
16358f0484fSRodney W. Grimes.Sh RETURN VALUES
16458f0484fSRodney W. GrimesUpon successful completion
16558f0484fSRodney W. Grimes.Fn fopen ,
16658f0484fSRodney W. Grimes.Fn fdopen
16758f0484fSRodney W. Grimesand
16858f0484fSRodney W. Grimes.Fn freopen
16958f0484fSRodney W. Grimesreturn a
17058f0484fSRodney W. Grimes.Tn FILE
17158f0484fSRodney W. Grimespointer.
17258f0484fSRodney W. GrimesOtherwise,
17358f0484fSRodney W. Grimes.Dv NULL
17458f0484fSRodney W. Grimesis returned and the global variable
17558f0484fSRodney W. Grimes.Va errno
17658f0484fSRodney W. Grimesis set to indicate the error.
17758f0484fSRodney W. Grimes.Sh ERRORS
17858f0484fSRodney W. Grimes.Bl -tag -width [EINVAL]
17958f0484fSRodney W. Grimes.It Bq Er EINVAL
18058f0484fSRodney W. GrimesThe
18158f0484fSRodney W. Grimes.Fa mode
18258f0484fSRodney W. Grimesprovided to
18358f0484fSRodney W. Grimes.Fn fopen ,
18458f0484fSRodney W. Grimes.Fn fdopen ,
18558f0484fSRodney W. Grimesor
18658f0484fSRodney W. Grimes.Fn freopen
18758f0484fSRodney W. Grimeswas invalid.
18858f0484fSRodney W. Grimes.El
18958f0484fSRodney W. Grimes.Pp
19058f0484fSRodney W. GrimesThe
19158f0484fSRodney W. Grimes.Fn fopen ,
19258f0484fSRodney W. Grimes.Fn fdopen
19358f0484fSRodney W. Grimesand
19458f0484fSRodney W. Grimes.Fn freopen
19558f0484fSRodney W. Grimesfunctions
19658f0484fSRodney W. Grimesmay also fail and set
19758f0484fSRodney W. Grimes.Va errno
19858f0484fSRodney W. Grimesfor any of the errors specified for the routine
19958f0484fSRodney W. Grimes.Xr malloc 3 .
20058f0484fSRodney W. Grimes.Pp
20158f0484fSRodney W. GrimesThe
20258f0484fSRodney W. Grimes.Fn fopen
20358f0484fSRodney W. Grimesfunction
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 open 2 .
20858f0484fSRodney W. Grimes.Pp
20958f0484fSRodney W. GrimesThe
21058f0484fSRodney W. Grimes.Fn fdopen
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 fcntl 2 .
21658f0484fSRodney W. Grimes.Pp
21758f0484fSRodney W. GrimesThe
21858f0484fSRodney W. Grimes.Fn freopen
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 routines
22358f0484fSRodney W. Grimes.Xr open 2 ,
22458f0484fSRodney W. Grimes.Xr fclose 3
22558f0484fSRodney W. Grimesand
22658f0484fSRodney W. Grimes.Xr fflush 3 .
22758f0484fSRodney W. Grimes.Sh SEE ALSO
22858f0484fSRodney W. Grimes.Xr open 2 ,
22958f0484fSRodney W. Grimes.Xr fclose 3 ,
23058f0484fSRodney W. Grimes.Xr fseek 3 ,
23158f0484fSRodney W. Grimes.Xr funopen 3
23258f0484fSRodney W. Grimes.Sh STANDARDS
23358f0484fSRodney W. GrimesThe
23458f0484fSRodney W. Grimes.Fn fopen
23558f0484fSRodney W. Grimesand
23658f0484fSRodney W. Grimes.Fn freopen
23758f0484fSRodney W. Grimesfunctions
23858f0484fSRodney W. Grimesconform to
23958f0484fSRodney W. Grimes.St -ansiC .
24058f0484fSRodney W. GrimesThe
24158f0484fSRodney W. Grimes.Fn fdopen
24258f0484fSRodney W. Grimesfunction
24358f0484fSRodney W. Grimesconforms to
24458f0484fSRodney W. Grimes.St -p1003.1-88 .
245