xref: /freebsd/lib/libc/stdio/fopen.3 (revision edf8578117e8844e02c0121147f45e4609b30680)
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 September 1, 2023
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.St -isoC
148and
149.St -p1003.1
150both require that
151a file positioning function intervene between output and input, unless
152an input operation encounters end-of-file.
153.Pp
154The
155.Fn fdopen
156function associates a stream with the existing file descriptor,
157.Fa fildes .
158The mode
159of the stream must be compatible with the mode of the file descriptor.
160The
161.Dq Li x
162mode option is ignored.
163If the
164.Dq Li e
165mode option is present, the
166.Dv FD_CLOEXEC
167flag is set, otherwise it remains unchanged.
168When the stream is closed via
169.Xr fclose 3 ,
170.Fa fildes
171is closed also.
172.Pp
173The
174.Fn freopen
175function
176opens the file whose name is the string pointed to by
177.Fa path
178and associates the stream pointed to by
179.Fa stream
180with it.
181The original stream (if it exists) is closed.
182The
183.Fa mode
184argument is used just as in the
185.Fn fopen
186function.
187.Pp
188If the
189.Fa path
190argument is
191.Dv NULL ,
192.Fn freopen
193attempts to re-open the file associated with
194.Fa stream
195with a new mode.
196The new mode must be compatible with the mode that the stream was originally
197opened with:
198Streams open for reading can only be re-opened for reading,
199streams open for writing can only be re-opened for writing,
200and streams open for reading and writing can be re-opened in any mode.
201The
202.Dq Li x
203mode option is not meaningful in this context.
204.Pp
205The primary use of the
206.Fn freopen
207function
208is to change the file associated with a
209standard text stream
210.Dv ( stderr , stdin ,
211or
212.Dv stdout ) .
213.Pp
214The
215.Fn fmemopen
216function
217associates the buffer given by the
218.Fa buf
219and
220.Fa size
221arguments with a stream.
222The
223.Fa buf
224argument is either a null pointer or point to a buffer that
225is at least
226.Fa size
227bytes long.
228If a null pointer is specified as the
229.Fa buf
230argument,
231.Fn fmemopen
232allocates
233.Fa size
234bytes of memory.
235This buffer is automatically freed when the stream is closed.
236Buffers can be opened in text-mode (default) or binary-mode
237(if
238.Dq Li b
239is present in the second or third position of the
240.Fa mode
241argument).
242Buffers opened in text-mode make sure that writes are terminated with a
243.Dv NULL
244byte, if the last write hasn't filled up the whole buffer.
245Buffers opened in binary-mode never append a
246.Dv NULL
247byte.
248.Sh RETURN VALUES
249Upon successful completion
250.Fn fopen ,
251.Fn fdopen ,
252.Fn freopen
253and
254.Fn fmemopen
255return a
256.Tn FILE
257pointer.
258Otherwise,
259.Dv NULL
260is returned and the global variable
261.Va errno
262is set to indicate the error.
263.Sh ERRORS
264.Bl -tag -width Er
265.It Bq Er EINVAL
266The
267.Fa mode
268argument
269to
270.Fn fopen ,
271.Fn fdopen ,
272.Fn freopen ,
273or
274.Fn fmemopen
275was invalid.
276.El
277.Pp
278The
279.Fn fopen ,
280.Fn fdopen ,
281.Fn freopen
282and
283.Fn fmemopen
284functions
285may also fail and set
286.Va errno
287for any of the errors specified for the routine
288.Xr malloc 3 .
289.Pp
290The
291.Fn fopen
292function
293may also fail and set
294.Va errno
295for any of the errors specified for the routine
296.Xr open 2 .
297.Pp
298The
299.Fn fdopen
300function
301may also fail and set
302.Va errno
303for any of the errors specified for the routine
304.Xr fcntl 2 .
305.Pp
306The
307.Fn freopen
308function
309may also fail and set
310.Va errno
311for any of the errors specified for the routines
312.Xr open 2 ,
313.Xr fclose 3
314and
315.Xr fflush 3 .
316.Pp
317The
318.Fn fmemopen
319function
320may also fail and set
321.Va errno
322if the
323.Fa size
324argument is 0.
325.Sh SEE ALSO
326.Xr open 2 ,
327.Xr fclose 3 ,
328.Xr fileno 3 ,
329.Xr fseek 3 ,
330.Xr funopen 3
331.Sh STANDARDS
332The
333.Fn fopen
334and
335.Fn freopen
336functions
337conform to
338.St -isoC ,
339with the exception of the
340.Dq Li x
341mode option which conforms to
342.St -isoC-2011 .
343The
344.Fn fdopen
345function
346conforms to
347.St -p1003.1-88 .
348The
349.Dq Li e
350mode option does not conform to any standard
351but is also supported by glibc.
352The
353.Fn fmemopen
354function
355conforms to
356.St -p1003.1-2008 .
357The
358.Dq Li b
359mode does not conform to any standard
360but is also supported by glibc.
361.Sh HISTORY
362An
363.Fn fopen
364function appeared in
365.At v1 .
366