xref: /netbsd-src/lib/libc/stdio/fopen.3 (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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.\"     from: @(#)fopen.3	6.8 (Berkeley) 6/29/91
37.\"	$Id: fopen.3,v 1.3 1993/11/29 22:06:50 jtc Exp $
38.\"
39.Dd June 29, 1991
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 "char *path" "char *mode"
51.Ft FILE *
52.Fn fdopen "int fildes" "char *mode"
53.Ft FILE *
54.Fn freopen "char *path" "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.No 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.
133.Pp
134The
135.Fn freopen
136function
137opens the file whose name is the string pointed to by
138.Fa path
139and associates the stream pointed to by
140.Fa stream
141with it.
142The original stream (if it exists) is closed.
143The
144.Fa mode
145argument is used just as in the
146.Xr fopen
147function.
148The primary use of the
149.Fn freopen
150function
151is to change the file associated with a
152standard text stream
153.Pf ( Em stderr ,
154.Em stdin ,
155or
156.Em stdout ) .
157.Sh RETURN VALUES
158Upon successful completion
159.Fn fopen ,
160.Fn fdopen
161and
162.Fn freopen
163return a
164.Tn FILE
165pointer.
166Otherwise,
167.Dv NULL
168is returned and the global variable
169.Va errno
170is set to indicate the error.
171.Sh ERRORS
172.Bl -tag -width Er
173.It Bq Er EINVAL
174The
175.Fa mode
176provided to
177.Fn fopen ,
178.Fn fdopen ,
179or
180.Fn freopen
181was invalid.
182.El
183.Pp
184The
185.Fn fopen ,
186.Fn fdopen
187and
188.Fn freopen
189functions
190may also fail and set
191.Va errno
192for any of the errors specified for the routine
193.Xr malloc 3 .
194.Pp
195The
196.Fn fopen
197function
198may also fail and set
199.Va errno
200for any of the errors specified for the routine
201.Xr open 2 .
202.Pp
203The
204.Fn fdopen
205function
206may also fail and set
207.Va errno
208for any of the errors specified for the routine
209.Xr fcntl 2 .
210.Pp
211The
212.Fn freopen
213function
214may also fail and set
215.Va errno
216for any of the errors specified for the routines
217.Xr open 2 ,
218.Xr fclose 3
219and
220.Xr fflush 3 .
221.Sh SEE ALSO
222.Xr open 2 ,
223.Xr fclose 3 ,
224.Xr fseek 3 ,
225.Xr funopen 3
226.Sh STANDARDS
227The
228.Fn fopen
229and
230.Fn freopen
231functions
232conform to
233.St -ansiC .
234The
235.Fn fdopen
236function
237conforms to
238.St -p1003.1-88 .
239