xref: /freebsd/lib/libc/stdio/fopen.3 (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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.\"
38.Dd June 4, 1993
39.Dt FOPEN 3
40.Os
41.Sh NAME
42.Nm fopen ,
43.Nm fdopen ,
44.Nm freopen
45.Nd stream open functions
46.Sh SYNOPSIS
47.Fd #include <stdio.h>
48.Ft FILE *
49.Fn fopen "char *path" "char *mode"
50.Ft FILE *
51.Fn fdopen "int fildes" "char *mode"
52.Ft FILE *
53.Fn freopen "char *path" "char *mode" "FILE *stream"
54.Sh DESCRIPTION
55The
56.Fn fopen
57function
58opens the file whose name is the string pointed to by
59.Fa path
60and associates a stream with it.
61.Pp
62The argument
63.Fa mode
64points to a string beginning with one of the following
65sequences (Additional characters may follow these sequences.):
66.Bl -tag -width indent
67.It Dq Li r
68Open text file for reading.
69The stream is positioned at the beginning of the file.
70.It Dq Li r+
71Open for reading and writing.
72The stream is positioned at the beginning of the file.
73.It Dq Li w
74Truncate file to zero length or create text file for writing.
75The stream is positioned at the beginning of the file.
76.No It Dq Li w+
77Open for reading and writing.
78The file is created if it does not exist, otherwise it is truncated.
79The stream is positioned at the beginning of the file.
80.It Dq Li a
81Open for writing.
82The file is created if it does not exist.
83The stream is positioned at the end of the file.
84.It Dq Li a+
85Open for reading and writing.
86The file is created if it does not exist.
87The stream is positioned at the end of the file.
88.El
89.Pp
90The
91.Fa mode
92string can also include the letter ``b'' either as a third character or
93as a character between the characters in any of the two-character strings
94described above.
95This is strictly for compatibility with
96.St -ansiC
97and has no effect; the ``b'' is ignored.
98.Pp
99Any created files will have mode
100.Pf \\*q Dv S_IRUSR
101\&|
102.Dv S_IWUSR
103\&|
104.Dv S_IRGRP
105\&|
106.Dv S_IWGRP
107\&|
108.Dv S_IROTH
109\&|
110.Dv S_IWOTH Ns \\*q
111.Pq Li 0666 ,
112as modified by the process'
113umask value (see
114.Xr umask 2 ) .
115.Pp
116Reads and writes may be intermixed on read/write streams in any order,
117and do not require an intermediate seek as in previous versions of
118.Em stdio .
119This is not portable to other systems, however;
120.Tn ANSI C
121requires that
122a file positioning function intervene between output and input, unless
123an input operation encounters end-of-file.
124.Pp
125The
126.Fn fdopen
127function associates a stream with the existing file descriptor,
128.Fa fildes .
129The
130.Fa mode
131of the stream must be compatible with the mode of the file descriptor.
132.Pp
133The
134.Fn freopen
135function
136opens the file whose name is the string pointed to by
137.Fa path
138and associates the stream pointed to by
139.Fa stream
140with it.
141The original stream (if it exists) is closed.
142The
143.Fa mode
144argument is used just as in the
145.Xr fopen
146function.
147The primary use of the
148.Fn freopen
149function
150is to change the file associated with a
151standard text stream
152.Pf ( Em stderr ,
153.Em stdin ,
154or
155.Em stdout ) .
156.Sh RETURN VALUES
157Upon successful completion
158.Fn fopen ,
159.Fn fdopen
160and
161.Fn freopen
162return a
163.Tn FILE
164pointer.
165Otherwise,
166.Dv NULL
167is returned and the global variable
168.Va errno
169is set to indicate the error.
170.Sh ERRORS
171.Bl -tag -width [EINVAL]
172.It Bq Er EINVAL
173The
174.Fa mode
175provided to
176.Fn fopen ,
177.Fn fdopen ,
178or
179.Fn freopen
180was invalid.
181.El
182.Pp
183The
184.Fn fopen ,
185.Fn fdopen
186and
187.Fn freopen
188functions
189may also fail and set
190.Va errno
191for any of the errors specified for the routine
192.Xr malloc 3 .
193.Pp
194The
195.Fn fopen
196function
197may also fail and set
198.Va errno
199for any of the errors specified for the routine
200.Xr open 2 .
201.Pp
202The
203.Fn fdopen
204function
205may also fail and set
206.Va errno
207for any of the errors specified for the routine
208.Xr fcntl 2 .
209.Pp
210The
211.Fn freopen
212function
213may also fail and set
214.Va errno
215for any of the errors specified for the routines
216.Xr open 2 ,
217.Xr fclose 3
218and
219.Xr fflush 3 .
220.Sh SEE ALSO
221.Xr open 2 ,
222.Xr fclose 3 ,
223.Xr fseek 3 ,
224.Xr funopen 3
225.Sh STANDARDS
226The
227.Fn fopen
228and
229.Fn freopen
230functions
231conform to
232.St -ansiC .
233The
234.Fn fdopen
235function
236conforms to
237.St -p1003.1-88 .
238