xref: /freebsd/lib/libc/stdio/fopen.3 (revision f24c2153d56ecc6eb99327f2c96ed3e7c271b63d)
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
3758f0484fSRodney W. Grimes.\"
3858f0484fSRodney W. Grimes.Dd June 4, 1993
3958f0484fSRodney W. Grimes.Dt FOPEN 3
4058f0484fSRodney W. Grimes.Os
4158f0484fSRodney W. Grimes.Sh NAME
4258f0484fSRodney W. Grimes.Nm fopen ,
4358f0484fSRodney W. Grimes.Nm fdopen ,
4458f0484fSRodney W. Grimes.Nm freopen
4558f0484fSRodney W. Grimes.Nd stream open functions
4658f0484fSRodney W. Grimes.Sh SYNOPSIS
4758f0484fSRodney W. Grimes.Fd #include <stdio.h>
4858f0484fSRodney W. Grimes.Ft FILE *
4923f0c1fcSBruce Evans.Fn fopen "const char *path" "const char *mode"
5058f0484fSRodney W. Grimes.Ft FILE *
5123f0c1fcSBruce Evans.Fn fdopen "int fildes" "const char *mode"
5258f0484fSRodney W. Grimes.Ft FILE *
5323f0c1fcSBruce Evans.Fn freopen "const char *path" "const char *mode" "FILE *stream"
5458f0484fSRodney W. Grimes.Sh DESCRIPTION
5558f0484fSRodney W. GrimesThe
5658f0484fSRodney W. Grimes.Fn fopen
5758f0484fSRodney W. Grimesfunction
5858f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
5958f0484fSRodney W. Grimes.Fa path
6058f0484fSRodney W. Grimesand associates a stream with it.
6158f0484fSRodney W. Grimes.Pp
6258f0484fSRodney W. GrimesThe argument
6358f0484fSRodney W. Grimes.Fa mode
6458f0484fSRodney W. Grimespoints to a string beginning with one of the following
6558f0484fSRodney W. Grimessequences (Additional characters may follow these sequences.):
6658f0484fSRodney W. Grimes.Bl -tag -width indent
6758f0484fSRodney W. Grimes.It Dq Li r
6858f0484fSRodney W. GrimesOpen text file for reading.
6958f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7058f0484fSRodney W. Grimes.It Dq Li r+
7158f0484fSRodney W. GrimesOpen for reading and writing.
7258f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
7358f0484fSRodney W. Grimes.It Dq Li w
7458f0484fSRodney W. GrimesTruncate file to zero length or create text file for writing.
7558f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
766c0aebfaSMike Pritchard.It Dq Li w+
7758f0484fSRodney W. GrimesOpen for reading and writing.
7858f0484fSRodney W. GrimesThe file is created if it does not exist, otherwise it is truncated.
7958f0484fSRodney W. GrimesThe stream is positioned at the beginning of the file.
8058f0484fSRodney W. Grimes.It Dq Li a
8158f0484fSRodney W. GrimesOpen for writing.
8258f0484fSRodney W. GrimesThe file is created if it does not exist.
8358f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
8458f0484fSRodney W. Grimes.It Dq Li a+
8558f0484fSRodney W. GrimesOpen for reading and writing.
8658f0484fSRodney W. GrimesThe file is created if it does not exist.
8758f0484fSRodney W. GrimesThe stream is positioned at the end of the file.
8858f0484fSRodney W. Grimes.El
8958f0484fSRodney W. Grimes.Pp
9058f0484fSRodney W. GrimesThe
9158f0484fSRodney W. Grimes.Fa mode
9258f0484fSRodney W. Grimesstring can also include the letter ``b'' either as a third character or
9358f0484fSRodney W. Grimesas a character between the characters in any of the two-character strings
9458f0484fSRodney W. Grimesdescribed above.
9558f0484fSRodney W. GrimesThis is strictly for compatibility with
9658f0484fSRodney W. Grimes.St -ansiC
9758f0484fSRodney W. Grimesand has no effect; the ``b'' is ignored.
9858f0484fSRodney W. Grimes.Pp
9958f0484fSRodney W. GrimesAny created files will have mode
10058f0484fSRodney W. Grimes.Pf \\*q Dv S_IRUSR
10158f0484fSRodney W. Grimes\&|
10258f0484fSRodney W. Grimes.Dv S_IWUSR
10358f0484fSRodney W. Grimes\&|
10458f0484fSRodney W. Grimes.Dv S_IRGRP
10558f0484fSRodney W. Grimes\&|
10658f0484fSRodney W. Grimes.Dv S_IWGRP
10758f0484fSRodney W. Grimes\&|
10858f0484fSRodney W. Grimes.Dv S_IROTH
10958f0484fSRodney W. Grimes\&|
11058f0484fSRodney W. Grimes.Dv S_IWOTH Ns \\*q
11158f0484fSRodney W. Grimes.Pq Li 0666 ,
11258f0484fSRodney W. Grimesas modified by the process'
11358f0484fSRodney W. Grimesumask value (see
11458f0484fSRodney W. Grimes.Xr umask 2 ) .
11558f0484fSRodney W. Grimes.Pp
11658f0484fSRodney W. GrimesReads and writes may be intermixed on read/write streams in any order,
11758f0484fSRodney W. Grimesand do not require an intermediate seek as in previous versions of
11858f0484fSRodney W. Grimes.Em stdio .
11958f0484fSRodney W. GrimesThis is not portable to other systems, however;
12058f0484fSRodney W. Grimes.Tn ANSI C
12158f0484fSRodney W. Grimesrequires that
12258f0484fSRodney W. Grimesa file positioning function intervene between output and input, unless
12358f0484fSRodney W. Grimesan input operation encounters end-of-file.
12458f0484fSRodney W. Grimes.Pp
12558f0484fSRodney W. GrimesThe
12658f0484fSRodney W. Grimes.Fn fdopen
12758f0484fSRodney W. Grimesfunction associates a stream with the existing file descriptor,
12858f0484fSRodney W. Grimes.Fa fildes .
12958f0484fSRodney W. GrimesThe
13058f0484fSRodney W. Grimes.Fa mode
13158f0484fSRodney W. Grimesof the stream must be compatible with the mode of the file descriptor.
132f24c2153SArchie CobbsWhen the stream is closed via
133f24c2153SArchie Cobbs.Xr fclose 3 ,
134f24c2153SArchie Cobbs.Fa fildes
135f24c2153SArchie Cobbsis closed also.
13658f0484fSRodney W. Grimes.Pp
13758f0484fSRodney W. GrimesThe
13858f0484fSRodney W. Grimes.Fn freopen
13958f0484fSRodney W. Grimesfunction
14058f0484fSRodney W. Grimesopens the file whose name is the string pointed to by
14158f0484fSRodney W. Grimes.Fa path
14258f0484fSRodney W. Grimesand associates the stream pointed to by
14358f0484fSRodney W. Grimes.Fa stream
14458f0484fSRodney W. Grimeswith it.
14558f0484fSRodney W. GrimesThe original stream (if it exists) is closed.
14658f0484fSRodney W. GrimesThe
14758f0484fSRodney W. Grimes.Fa mode
14858f0484fSRodney W. Grimesargument is used just as in the
149064f0074SMike Pritchard.Fn fopen
15058f0484fSRodney W. Grimesfunction.
15158f0484fSRodney W. GrimesThe primary use of the
15258f0484fSRodney W. Grimes.Fn freopen
15358f0484fSRodney W. Grimesfunction
15458f0484fSRodney W. Grimesis to change the file associated with a
15558f0484fSRodney W. Grimesstandard text stream
15658f0484fSRodney W. Grimes.Pf ( Em stderr ,
15758f0484fSRodney W. Grimes.Em stdin ,
15858f0484fSRodney W. Grimesor
15958f0484fSRodney W. Grimes.Em stdout ) .
16058f0484fSRodney W. Grimes.Sh RETURN VALUES
16158f0484fSRodney W. GrimesUpon successful completion
16258f0484fSRodney W. Grimes.Fn fopen ,
16358f0484fSRodney W. Grimes.Fn fdopen
16458f0484fSRodney W. Grimesand
16558f0484fSRodney W. Grimes.Fn freopen
16658f0484fSRodney W. Grimesreturn a
16758f0484fSRodney W. Grimes.Tn FILE
16858f0484fSRodney W. Grimespointer.
16958f0484fSRodney W. GrimesOtherwise,
17058f0484fSRodney W. Grimes.Dv NULL
17158f0484fSRodney W. Grimesis returned and the global variable
17258f0484fSRodney W. Grimes.Va errno
17358f0484fSRodney W. Grimesis set to indicate the error.
17458f0484fSRodney W. Grimes.Sh ERRORS
17558f0484fSRodney W. Grimes.Bl -tag -width [EINVAL]
17658f0484fSRodney W. Grimes.It Bq Er EINVAL
17758f0484fSRodney W. GrimesThe
17858f0484fSRodney W. Grimes.Fa mode
17958f0484fSRodney W. Grimesprovided to
18058f0484fSRodney W. Grimes.Fn fopen ,
18158f0484fSRodney W. Grimes.Fn fdopen ,
18258f0484fSRodney W. Grimesor
18358f0484fSRodney W. Grimes.Fn freopen
18458f0484fSRodney W. Grimeswas invalid.
18558f0484fSRodney W. Grimes.El
18658f0484fSRodney W. Grimes.Pp
18758f0484fSRodney W. GrimesThe
18858f0484fSRodney W. Grimes.Fn fopen ,
18958f0484fSRodney W. Grimes.Fn fdopen
19058f0484fSRodney W. Grimesand
19158f0484fSRodney W. Grimes.Fn freopen
19258f0484fSRodney W. Grimesfunctions
19358f0484fSRodney W. Grimesmay also fail and set
19458f0484fSRodney W. Grimes.Va errno
19558f0484fSRodney W. Grimesfor any of the errors specified for the routine
19658f0484fSRodney W. Grimes.Xr malloc 3 .
19758f0484fSRodney W. Grimes.Pp
19858f0484fSRodney W. GrimesThe
19958f0484fSRodney W. Grimes.Fn fopen
20058f0484fSRodney W. Grimesfunction
20158f0484fSRodney W. Grimesmay also fail and set
20258f0484fSRodney W. Grimes.Va errno
20358f0484fSRodney W. Grimesfor any of the errors specified for the routine
20458f0484fSRodney W. Grimes.Xr open 2 .
20558f0484fSRodney W. Grimes.Pp
20658f0484fSRodney W. GrimesThe
20758f0484fSRodney W. Grimes.Fn fdopen
20858f0484fSRodney W. Grimesfunction
20958f0484fSRodney W. Grimesmay also fail and set
21058f0484fSRodney W. Grimes.Va errno
21158f0484fSRodney W. Grimesfor any of the errors specified for the routine
21258f0484fSRodney W. Grimes.Xr fcntl 2 .
21358f0484fSRodney W. Grimes.Pp
21458f0484fSRodney W. GrimesThe
21558f0484fSRodney W. Grimes.Fn freopen
21658f0484fSRodney W. Grimesfunction
21758f0484fSRodney W. Grimesmay also fail and set
21858f0484fSRodney W. Grimes.Va errno
21958f0484fSRodney W. Grimesfor any of the errors specified for the routines
22058f0484fSRodney W. Grimes.Xr open 2 ,
22158f0484fSRodney W. Grimes.Xr fclose 3
22258f0484fSRodney W. Grimesand
22358f0484fSRodney W. Grimes.Xr fflush 3 .
22458f0484fSRodney W. Grimes.Sh SEE ALSO
22558f0484fSRodney W. Grimes.Xr open 2 ,
22658f0484fSRodney W. Grimes.Xr fclose 3 ,
22758f0484fSRodney W. Grimes.Xr fseek 3 ,
22858f0484fSRodney W. Grimes.Xr funopen 3
22958f0484fSRodney W. Grimes.Sh STANDARDS
23058f0484fSRodney W. GrimesThe
23158f0484fSRodney W. Grimes.Fn fopen
23258f0484fSRodney W. Grimesand
23358f0484fSRodney W. Grimes.Fn freopen
23458f0484fSRodney W. Grimesfunctions
23558f0484fSRodney W. Grimesconform to
23658f0484fSRodney W. Grimes.St -ansiC .
23758f0484fSRodney W. GrimesThe
23858f0484fSRodney W. Grimes.Fn fdopen
23958f0484fSRodney W. Grimesfunction
24058f0484fSRodney W. Grimesconforms to
24158f0484fSRodney W. Grimes.St -p1003.1-88 .
242