xref: /freebsd/lib/libc/stdio/fopen.3 (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)fopen.3	8.1 (Berkeley) 6/4/93
33.\"
34.Dd May 1, 2020
35.Dt FOPEN 3
36.Os
37.Sh NAME
38.Nm fopen ,
39.Nm fdopen ,
40.Nm freopen ,
41.Nm fmemopen
42.Nd stream open functions
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft FILE *
48.Fn fopen "const char * restrict path" "const char * restrict mode"
49.Ft FILE *
50.Fn fdopen "int fildes" "const char *mode"
51.Ft FILE *
52.Fn freopen "const char *path" "const char *mode" "FILE *stream"
53.Ft FILE *
54.Fn fmemopen "void *restrict *buf" "size_t size" "const char * restrict mode"
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 letters:
66.Bl -tag -width indent
67.It Dq Li r
68Open for reading.
69The stream is positioned at the beginning of the file.
70Fail if the file does not exist.
71.It Dq Li w
72Open for writing.
73The stream is positioned at the beginning of the file.
74Truncate the file to zero length if it exists or create the file if it does not exist.
75.It Dq Li a
76Open for writing.
77The stream is positioned at the end of the file.
78Subsequent writes to the file will always end up at the then current
79end of file, irrespective of any intervening
80.Xr fseek 3
81or similar.
82Create the file if it does not exist.
83.El
84.Pp
85An optional
86.Dq Li +
87following
88.Dq Li r ,
89.Dq Li w ,
90or
91.Dq Li a
92opens the file for both reading and writing.
93An optional
94.Dq Li x
95following
96.Dq Li w
97or
98.Dq Li w+
99causes the
100.Fn fopen
101call to fail if the file already exists.
102An optional
103.Dq Li e
104following the above
105causes the
106.Fn fopen
107call to set the
108.Dv FD_CLOEXEC
109flag on the underlying file descriptor.
110.Pp
111The
112.Fa mode
113string can also include the letter
114.Dq Li b
115after either the
116.Dq Li +
117or the first letter.
118This is strictly for compatibility with
119.St -isoC
120and has effect only for
121.Fn fmemopen ;
122otherwise
123.Dq Li b
124is ignored.
125.Pp
126Any created files will have mode
127.Do Dv S_IRUSR
128\&|
129.Dv S_IWUSR
130\&|
131.Dv S_IRGRP
132\&|
133.Dv S_IWGRP
134\&|
135.Dv S_IROTH
136\&|
137.Dv S_IWOTH Dc
138.Pq Li 0666 ,
139as modified by the process'
140umask value (see
141.Xr umask 2 ) .
142.Pp
143Reads and writes may be intermixed on read/write streams in any order,
144and do not require an intermediate seek as in previous versions of
145.Em stdio .
146This is not portable to other systems, however;
147.Tn ANSI C
148requires that
149a file positioning function intervene between output and input, unless
150an input operation encounters end-of-file.
151.Pp
152The
153.Fn fdopen
154function associates a stream with the existing file descriptor,
155.Fa fildes .
156The mode
157of the stream must be compatible with the mode of the file descriptor.
158The
159.Dq Li x
160mode option is ignored.
161If the
162.Dq Li e
163mode option is present, the
164.Dv FD_CLOEXEC
165flag is set, otherwise it remains unchanged.
166When the stream is closed via
167.Xr fclose 3 ,
168.Fa fildes
169is closed also.
170.Pp
171The
172.Fn freopen
173function
174opens the file whose name is the string pointed to by
175.Fa path
176and associates the stream pointed to by
177.Fa stream
178with it.
179The original stream (if it exists) is closed.
180The
181.Fa mode
182argument is used just as in the
183.Fn fopen
184function.
185.Pp
186If the
187.Fa path
188argument is
189.Dv NULL ,
190.Fn freopen
191attempts to re-open the file associated with
192.Fa stream
193with a new mode.
194The new mode must be compatible with the mode that the stream was originally
195opened with:
196Streams open for reading can only be re-opened for reading,
197streams open for writing can only be re-opened for writing,
198and streams open for reading and writing can be re-opened in any mode.
199The
200.Dq Li x
201mode option is not meaningful in this context.
202.Pp
203The primary use of the
204.Fn freopen
205function
206is to change the file associated with a
207standard text stream
208.Dv ( stderr , stdin ,
209or
210.Dv stdout ) .
211.Pp
212The
213.Fn fmemopen
214function
215associates the buffer given by the
216.Fa buf
217and
218.Fa size
219arguments with a stream.
220The
221.Fa buf
222argument is either a null pointer or point to a buffer that
223is at least
224.Fa size
225bytes long.
226If a null pointer is specified as the
227.Fa buf
228argument,
229.Fn fmemopen
230allocates
231.Fa size
232bytes of memory.
233This buffer is automatically freed when the stream is closed.
234Buffers can be opened in text-mode (default) or binary-mode
235(if
236.Dq Li b
237is present in the second or third position of the
238.Fa mode
239argument).
240Buffers opened in text-mode make sure that writes are terminated with a
241.Dv NULL
242byte, if the last write hasn't filled up the whole buffer.
243Buffers opened in binary-mode never append a
244.Dv NULL
245byte.
246.Sh RETURN VALUES
247Upon successful completion
248.Fn fopen ,
249.Fn fdopen
250and
251.Fn freopen
252return a
253.Tn FILE
254pointer.
255Otherwise,
256.Dv NULL
257is returned and the global variable
258.Va errno
259is set to indicate the error.
260.Sh ERRORS
261.Bl -tag -width Er
262.It Bq Er EINVAL
263The
264.Fa mode
265argument
266to
267.Fn fopen ,
268.Fn fdopen ,
269.Fn freopen ,
270or
271.Fn fmemopen
272was invalid.
273.El
274.Pp
275The
276.Fn fopen ,
277.Fn fdopen ,
278.Fn freopen
279and
280.Fn fmemopen
281functions
282may also fail and set
283.Va errno
284for any of the errors specified for the routine
285.Xr malloc 3 .
286.Pp
287The
288.Fn fopen
289function
290may also fail and set
291.Va errno
292for any of the errors specified for the routine
293.Xr open 2 .
294.Pp
295The
296.Fn fdopen
297function
298may also fail and set
299.Va errno
300for any of the errors specified for the routine
301.Xr fcntl 2 .
302.Pp
303The
304.Fn freopen
305function
306may also fail and set
307.Va errno
308for any of the errors specified for the routines
309.Xr open 2 ,
310.Xr fclose 3
311and
312.Xr fflush 3 .
313.Pp
314The
315.Fn fmemopen
316function
317may also fail and set
318.Va errno
319if the
320.Fa size
321argument is 0.
322.Sh SEE ALSO
323.Xr open 2 ,
324.Xr fclose 3 ,
325.Xr fileno 3 ,
326.Xr fseek 3 ,
327.Xr funopen 3
328.Sh STANDARDS
329The
330.Fn fopen
331and
332.Fn freopen
333functions
334conform to
335.St -isoC ,
336with the exception of the
337.Dq Li x
338mode option which conforms to
339.St -isoC-2011 .
340The
341.Fn fdopen
342function
343conforms to
344.St -p1003.1-88 .
345The
346.Dq Li e
347mode option does not conform to any standard
348but is also supported by glibc.
349The
350.Fn fmemopen
351function
352conforms to
353.St -p1003.1-2008 .
354The
355.Dq Li b
356mode does not conform to any standard
357but is also supported by glibc.
358.Sh HISTORY
359An
360.Fn fopen
361function appeared in
362.At v1 .
363