]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdio/fopen.3
lib{c,lzma,z}: remove -DSYMBOL_VERSIONING from CFLAGS
[FreeBSD/FreeBSD.git] / lib / libc / stdio / fopen.3
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
56 The
57 .Fn fopen
58 function
59 opens the file whose name is the string pointed to by
60 .Fa path
61 and associates a stream with it.
62 .Pp
63 The argument
64 .Fa mode
65 points to a string beginning with one of the following letters:
66 .Bl -tag -width indent
67 .It Dq Li r
68 Open for reading.
69 The stream is positioned at the beginning of the file.
70 Fail if the file does not exist.
71 .It Dq Li w
72 Open for writing.
73 The stream is positioned at the beginning of the file.
74 Truncate the file to zero length if it exists or create the file if it does not exist.
75 .It Dq Li a
76 Open for writing.
77 The stream is positioned at the end of the file.
78 Subsequent writes to the file will always end up at the then current
79 end of file, irrespective of any intervening
80 .Xr fseek 3
81 or similar.
82 Create the file if it does not exist.
83 .El
84 .Pp
85 An optional
86 .Dq Li +
87 following
88 .Dq Li r ,
89 .Dq Li w ,
90 or
91 .Dq Li a
92 opens the file for both reading and writing.
93 An optional
94 .Dq Li x
95 following
96 .Dq Li w
97 or
98 .Dq Li w+
99 causes the
100 .Fn fopen
101 call to fail if the file already exists.
102 An optional
103 .Dq Li e
104 following the above
105 causes the
106 .Fn fopen
107 call to set the
108 .Dv FD_CLOEXEC
109 flag on the underlying file descriptor.
110 .Pp
111 The
112 .Fa mode
113 string can also include the letter
114 .Dq Li b
115 after either the
116 .Dq Li +
117 or the first letter.
118 This is strictly for compatibility with
119 .St -isoC
120 and has effect only for
121 .Fn fmemopen ;
122 otherwise
123 .Dq Li b
124 is ignored.
125 .Pp
126 Any 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 ,
139 as modified by the process'
140 umask value (see
141 .Xr umask 2 ) .
142 .Pp
143 Reads and writes may be intermixed on read/write streams in any order,
144 and do not require an intermediate seek as in previous versions of
145 .Em stdio .
146 This is not portable to other systems, however;
147 .St -isoC
148 and
149 .St -p1003.1
150 both require that
151 a file positioning function intervene between output and input, unless
152 an input operation encounters end-of-file.
153 .Pp
154 The
155 .Fn fdopen
156 function associates a stream with the existing file descriptor,
157 .Fa fildes .
158 The mode
159 of the stream must be compatible with the mode of the file descriptor.
160 The
161 .Dq Li x
162 mode option is ignored.
163 If the
164 .Dq Li e
165 mode option is present, the
166 .Dv FD_CLOEXEC
167 flag is set, otherwise it remains unchanged.
168 When the stream is closed via
169 .Xr fclose 3 ,
170 .Fa fildes
171 is closed also.
172 .Pp
173 The
174 .Fn freopen
175 function
176 opens the file whose name is the string pointed to by
177 .Fa path
178 and associates the stream pointed to by
179 .Fa stream
180 with it.
181 The original stream (if it exists) is closed.
182 The
183 .Fa mode
184 argument is used just as in the
185 .Fn fopen
186 function.
187 .Pp
188 If the
189 .Fa path
190 argument is
191 .Dv NULL ,
192 .Fn freopen
193 attempts to re-open the file associated with
194 .Fa stream
195 with a new mode.
196 The new mode must be compatible with the mode that the stream was originally
197 opened with:
198 Streams open for reading can only be re-opened for reading,
199 streams open for writing can only be re-opened for writing,
200 and streams open for reading and writing can be re-opened in any mode.
201 The
202 .Dq Li x
203 mode option is not meaningful in this context.
204 .Pp
205 The primary use of the
206 .Fn freopen
207 function
208 is to change the file associated with a
209 standard text stream
210 .Dv ( stderr , stdin ,
211 or
212 .Dv stdout ) .
213 .Pp
214 The
215 .Fn fmemopen
216 function
217 associates the buffer given by the
218 .Fa buf
219 and
220 .Fa size
221 arguments with a stream.
222 The
223 .Fa buf
224 argument is either a null pointer or point to a buffer that
225 is at least
226 .Fa size
227 bytes long.
228 If a null pointer is specified as the
229 .Fa buf
230 argument,
231 .Fn fmemopen
232 allocates
233 .Fa size
234 bytes of memory.
235 This buffer is automatically freed when the stream is closed.
236 Buffers can be opened in text-mode (default) or binary-mode
237 (if
238 .Dq Li b
239 is present in the second or third position of the
240 .Fa mode
241 argument).
242 Buffers opened in text-mode make sure that writes are terminated with a
243 .Dv NULL
244 byte, if the last write hasn't filled up the whole buffer.
245 Buffers opened in binary-mode never append a
246 .Dv NULL
247 byte.
248 .Sh RETURN VALUES
249 Upon successful completion
250 .Fn fopen ,
251 .Fn fdopen ,
252 .Fn freopen
253 and
254 .Fn fmemopen
255 return a
256 .Tn FILE
257 pointer.
258 Otherwise,
259 .Dv NULL
260 is returned and the global variable
261 .Va errno
262 is set to indicate the error.
263 .Sh ERRORS
264 .Bl -tag -width Er
265 .It Bq Er EINVAL
266 The
267 .Fa mode
268 argument
269 to
270 .Fn fopen ,
271 .Fn fdopen ,
272 .Fn freopen ,
273 or
274 .Fn fmemopen
275 was invalid.
276 .El
277 .Pp
278 The
279 .Fn fopen ,
280 .Fn fdopen ,
281 .Fn freopen
282 and
283 .Fn fmemopen
284 functions
285 may also fail and set
286 .Va errno
287 for any of the errors specified for the routine
288 .Xr malloc 3 .
289 .Pp
290 The
291 .Fn fopen
292 function
293 may also fail and set
294 .Va errno
295 for any of the errors specified for the routine
296 .Xr open 2 .
297 .Pp
298 The
299 .Fn fdopen
300 function
301 may also fail and set
302 .Va errno
303 for any of the errors specified for the routine
304 .Xr fcntl 2 .
305 .Pp
306 The
307 .Fn freopen
308 function
309 may also fail and set
310 .Va errno
311 for any of the errors specified for the routines
312 .Xr open 2 ,
313 .Xr fclose 3
314 and
315 .Xr fflush 3 .
316 .Pp
317 The
318 .Fn fmemopen
319 function
320 may also fail and set
321 .Va errno
322 if the
323 .Fa size
324 argument 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
332 The
333 .Fn fopen
334 and
335 .Fn freopen
336 functions
337 conform to
338 .St -isoC ,
339 with the exception of the
340 .Dq Li x
341 mode option which conforms to
342 .St -isoC-2011 .
343 The
344 .Fn fdopen
345 function
346 conforms to
347 .St -p1003.1-88 .
348 The
349 .Dq Li e
350 mode option does not conform to any standard
351 but is also supported by glibc.
352 The
353 .Fn fmemopen
354 function
355 conforms to
356 .St -p1003.1-2008 .
357 The
358 .Dq Li b
359 mode does not conform to any standard
360 but is also supported by glibc.
361 .Sh HISTORY
362 An
363 .Fn fopen
364 function appeared in
365 .At v1 .