xref: /freebsd/lib/libc/stdio/fopen.3 (revision a1a4f1a0d87b594d3f17a97dc0127eec1417e6f6)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     @(#)fopen.3	8.1 (Berkeley) 6/4/93
37.\" $FreeBSD$
38.\"
39.Dd June 4, 1993
40.Dt FOPEN 3
41.Os
42.Sh NAME
43.Nm fopen ,
44.Nm fdopen ,
45.Nm freopen
46.Nd stream open functions
47.Sh SYNOPSIS
48.Fd #include <stdio.h>
49.Ft FILE *
50.Fn fopen "const char *path" "const char *mode"
51.Ft FILE *
52.Fn fdopen "int fildes" "const char *mode"
53.Ft FILE *
54.Fn freopen "const char *path" "const char *mode" "FILE *stream"
55.Sh DESCRIPTION
56The
57.Fn fopen
58function
59opens the file whose name is the string pointed to by
60.Fa path
61and associates a stream with it.
62.Pp
63The argument
64.Fa mode
65points to a string beginning with one of the following
66sequences (Additional characters may follow these sequences.):
67.Bl -tag -width indent
68.It Dq Li r
69Open text file for reading.
70The stream is positioned at the beginning of the file.
71.It Dq Li r+
72Open for reading and writing.
73The stream is positioned at the beginning of the file.
74.It Dq Li w
75Truncate file to zero length or create text file for writing.
76The stream is positioned at the beginning of the file.
77.It Dq Li w+
78Open for reading and writing.
79The file is created if it does not exist, otherwise it is truncated.
80The stream is positioned at the beginning of the file.
81.It Dq Li a
82Open for writing.
83The file is created if it does not exist.
84The stream is positioned at the end of the file.
85.It Dq Li a+
86Open for reading and writing.
87The file is created if it does not exist.
88The stream is positioned at the end of the file.
89.El
90.Pp
91The
92.Fa mode
93string can also include the letter ``b'' either as a third character or
94as a character between the characters in any of the two-character strings
95described above.
96This is strictly for compatibility with
97.St -ansiC
98and has no effect; the ``b'' is ignored.
99.Pp
100Any created files will have mode
101.Pf \\*q Dv S_IRUSR
102\&|
103.Dv S_IWUSR
104\&|
105.Dv S_IRGRP
106\&|
107.Dv S_IWGRP
108\&|
109.Dv S_IROTH
110\&|
111.Dv S_IWOTH Ns \\*q
112.Pq Li 0666 ,
113as modified by the process'
114umask value (see
115.Xr umask 2 ) .
116.Pp
117Reads and writes may be intermixed on read/write streams in any order,
118and do not require an intermediate seek as in previous versions of
119.Em stdio .
120This is not portable to other systems, however;
121.Tn ANSI C
122requires that
123a file positioning function intervene between output and input, unless
124an input operation encounters end-of-file.
125.Pp
126The
127.Fn fdopen
128function associates a stream with the existing file descriptor,
129.Fa fildes .
130The
131.Fa mode
132of the stream must be compatible with the mode of the file descriptor.
133When the stream is closed via
134.Xr fclose 3 ,
135.Fa fildes
136is closed also.
137.Pp
138The
139.Fn freopen
140function
141opens the file whose name is the string pointed to by
142.Fa path
143and associates the stream pointed to by
144.Fa stream
145with it.
146The original stream (if it exists) is closed.
147The
148.Fa mode
149argument is used just as in the
150.Fn fopen
151function.
152The primary use of the
153.Fn freopen
154function
155is to change the file associated with a
156standard text stream
157.Pf ( Em stderr ,
158.Em stdin ,
159or
160.Em stdout ) .
161.Sh RETURN VALUES
162Upon successful completion
163.Fn fopen ,
164.Fn fdopen
165and
166.Fn freopen
167return a
168.Tn FILE
169pointer.
170Otherwise,
171.Dv NULL
172is returned and the global variable
173.Va errno
174is set to indicate the error.
175.Sh ERRORS
176.Bl -tag -width [EINVAL]
177.It Bq Er EINVAL
178The
179.Fa mode
180provided to
181.Fn fopen ,
182.Fn fdopen ,
183or
184.Fn freopen
185was invalid.
186.El
187.Pp
188The
189.Fn fopen ,
190.Fn fdopen
191and
192.Fn freopen
193functions
194may also fail and set
195.Va errno
196for any of the errors specified for the routine
197.Xr malloc 3 .
198.Pp
199The
200.Fn fopen
201function
202may also fail and set
203.Va errno
204for any of the errors specified for the routine
205.Xr open 2 .
206.Pp
207The
208.Fn fdopen
209function
210may also fail and set
211.Va errno
212for any of the errors specified for the routine
213.Xr fcntl 2 .
214.Pp
215The
216.Fn freopen
217function
218may also fail and set
219.Va errno
220for any of the errors specified for the routines
221.Xr open 2 ,
222.Xr fclose 3
223and
224.Xr fflush 3 .
225.Sh SEE ALSO
226.Xr open 2 ,
227.Xr fclose 3 ,
228.Xr fseek 3 ,
229.Xr funopen 3
230.Sh STANDARDS
231The
232.Fn fopen
233and
234.Fn freopen
235functions
236conform to
237.St -ansiC .
238The
239.Fn fdopen
240function
241conforms to
242.St -p1003.1-88 .
243