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