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