]>
Commit | Line | Data |
---|---|---|
5b2abdfb A |
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. | |
b061a43b | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
5b2abdfb A |
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 | |
b061a43b | 33 | .\" $FreeBSD$ |
5b2abdfb | 34 | .\" |
b061a43b | 35 | .Dd January 30, 2013 |
5b2abdfb A |
36 | .Dt FOPEN 3 |
37 | .Os | |
38 | .Sh NAME | |
ad3c9f2a | 39 | .Nm fopen , |
b061a43b A |
40 | .Nm fdopen , |
41 | .Nm freopen , | |
42 | .Nm fmemopen | |
5b2abdfb A |
43 | .Nd stream open functions |
44 | .Sh LIBRARY | |
45 | .Lb libc | |
46 | .Sh SYNOPSIS | |
47 | .In stdio.h | |
48 | .Ft FILE * | |
b061a43b | 49 | .Fn fopen "const char * restrict path" "const char * restrict mode" |
5b2abdfb | 50 | .Ft FILE * |
b061a43b | 51 | .Fn fdopen "int fildes" "const char *mode" |
5b2abdfb | 52 | .Ft FILE * |
b061a43b A |
53 | .Fn freopen "const char *path" "const char *mode" "FILE *stream" |
54 | .Ft FILE * | |
55 | .Fn fmemopen "void *restrict *buf" "size_t size" "const char * restrict mode" | |
5b2abdfb A |
56 | .Sh DESCRIPTION |
57 | The | |
58 | .Fn fopen | |
59 | function | |
60 | opens the file whose name is the string pointed to by | |
b061a43b | 61 | .Fa path |
5b2abdfb A |
62 | and associates a stream with it. |
63 | .Pp | |
64 | The argument | |
65 | .Fa mode | |
b061a43b | 66 | points to a string beginning with one of the following letters: |
5b2abdfb A |
67 | .Bl -tag -width indent |
68 | .It Dq Li r | |
b061a43b | 69 | Open for reading. |
5b2abdfb | 70 | The stream is positioned at the beginning of the file. |
b061a43b | 71 | Fail if the file does not exist. |
5b2abdfb | 72 | .It Dq Li w |
b061a43b | 73 | Open for writing. |
5b2abdfb | 74 | The stream is positioned at the beginning of the file. |
b061a43b | 75 | Create the file if it does not exist. |
5b2abdfb A |
76 | .It Dq Li a |
77 | Open for writing. | |
5b2abdfb A |
78 | The stream is positioned at the end of the file. |
79 | Subsequent writes to the file will always end up at the then current | |
80 | end of file, irrespective of any intervening | |
81 | .Xr fseek 3 | |
82 | or similar. | |
b061a43b | 83 | Create the file if it does not exist. |
5b2abdfb A |
84 | .El |
85 | .Pp | |
b061a43b A |
86 | An optional |
87 | .Dq Li + | |
88 | following | |
89 | .Dq Li r , | |
90 | .Dq Li w , | |
91 | or | |
92 | .Dq Li a | |
93 | opens the file for both reading and writing. | |
94 | An optional | |
95 | .Dq Li x | |
96 | following | |
97 | .Dq Li w | |
98 | or | |
99 | .Dq Li w+ | |
100 | causes the | |
101 | .Fn fopen | |
102 | call to fail if the file already exists. | |
103 | An optional | |
104 | .Dq Li e | |
105 | following the above | |
106 | causes the | |
107 | .Fn fopen | |
108 | call to set the | |
109 | .Dv FD_CLOEXEC | |
110 | flag on the underlying file descriptor. | |
111 | .Pp | |
5b2abdfb A |
112 | The |
113 | .Fa mode | |
b061a43b A |
114 | string can also include the letter |
115 | .Dq Li b | |
116 | after either the | |
117 | .Dq Li + | |
118 | or the first letter. | |
5b2abdfb A |
119 | This is strictly for compatibility with |
120 | .St -isoC | |
b061a43b A |
121 | and has effect only for |
122 | .Fn fmemopen | |
123 | ; otherwise | |
124 | .Dq Li b | |
125 | is ignored. | |
ad3c9f2a | 126 | .Pp |
5b2abdfb | 127 | Any created files will have mode |
b061a43b | 128 | .Do Dv S_IRUSR |
5b2abdfb A |
129 | \&| |
130 | .Dv S_IWUSR | |
131 | \&| | |
132 | .Dv S_IRGRP | |
133 | \&| | |
134 | .Dv S_IWGRP | |
135 | \&| | |
136 | .Dv S_IROTH | |
137 | \&| | |
b061a43b | 138 | .Dv S_IWOTH Dc |
5b2abdfb A |
139 | .Pq Li 0666 , |
140 | as modified by the process' | |
141 | umask value (see | |
142 | .Xr umask 2 ) . | |
143 | .Pp | |
5f125488 A |
144 | Reads and writes may be intermixed on read/write streams in any order; however, |
145 | a file positioning function must be called when switching between output and | |
146 | input, unless an input operation encounters end-of-file. | |
5b2abdfb A |
147 | .Pp |
148 | The | |
149 | .Fn fdopen | |
150 | function associates a stream with the existing file descriptor, | |
151 | .Fa fildes . | |
9385eb3d | 152 | The mode |
5b2abdfb | 153 | of the stream must be compatible with the mode of the file descriptor. |
b061a43b A |
154 | The |
155 | .Dq Li x | |
156 | mode option is ignored. | |
157 | If the | |
158 | .Dq Li e | |
159 | mode option is present, the | |
160 | .Dv FD_CLOEXEC | |
161 | flag is set, otherwise it remains unchanged. | |
5b2abdfb A |
162 | When the stream is closed via |
163 | .Xr fclose 3 , | |
164 | .Fa fildes | |
165 | is closed also. | |
166 | .Pp | |
167 | The | |
168 | .Fn freopen | |
169 | function | |
170 | opens the file whose name is the string pointed to by | |
b061a43b | 171 | .Fa path |
5b2abdfb A |
172 | and associates the stream pointed to by |
173 | .Fa stream | |
174 | with it. | |
175 | The original stream (if it exists) is closed. | |
176 | The | |
177 | .Fa mode | |
178 | argument is used just as in the | |
179 | .Fn fopen | |
180 | function. | |
9385eb3d A |
181 | .Pp |
182 | If the | |
b061a43b | 183 | .Fa path |
9385eb3d A |
184 | argument is |
185 | .Dv NULL , | |
186 | .Fn freopen | |
187 | attempts to re-open the file associated with | |
188 | .Fa stream | |
189 | with a new mode. | |
190 | The new mode must be compatible with the mode that the stream was originally | |
191 | opened with: | |
b061a43b A |
192 | Streams open for reading can only be re-opened for reading, |
193 | streams open for writing can only be re-opened for writing, | |
194 | and streams open for reading and writing can be re-opened in any mode. | |
195 | The | |
196 | .Dq Li x | |
197 | mode option is not meaningful in this context. | |
9385eb3d | 198 | .Pp |
5b2abdfb A |
199 | The primary use of the |
200 | .Fn freopen | |
201 | function | |
202 | is to change the file associated with a | |
203 | standard text stream | |
9385eb3d | 204 | .Dv ( stderr , stdin , |
5b2abdfb | 205 | or |
9385eb3d | 206 | .Dv stdout ) . |
b061a43b A |
207 | .Pp |
208 | The | |
209 | .Fn fmemopen | |
210 | function | |
211 | associates the buffer given by the | |
212 | .Fa buf | |
213 | and | |
214 | .Fa size | |
215 | arguments with a stream. | |
216 | The | |
217 | .Fa buf | |
218 | argument is either a null pointer or a pointer to a buffer that | |
219 | is at least | |
220 | .Fa size | |
221 | bytes long. | |
222 | If a null pointer is specified as the | |
223 | .Fa buf | |
224 | argument, | |
225 | .Fn fmemopen | |
226 | allocates | |
227 | .Fa size | |
228 | bytes of memory, and this allocation is automatically freed when the stream is | |
229 | closed. | |
230 | If a non-null pointer is specified, the caller retains ownership of | |
231 | the buffer and is responsible for disposing of it after the stream has been | |
232 | closed. | |
233 | Buffers can be opened in text-mode (default) or binary-mode | |
234 | (if | |
235 | .Dq Li b | |
236 | is present in the second or third position of the | |
237 | .Fa mode | |
238 | argument). Buffers opened in text-mode make sure that writes are terminated with | |
239 | a NULL byte, if the last write hasn't filled up the whole buffer. Buffers | |
240 | opened in binary-mode never append a NULL byte. | |
70ad1dc8 A |
241 | .Pp |
242 | Input and output against the opened stream will be fully buffered, unless | |
243 | it refers to an interactive terminal device, or a different kind of buffering | |
244 | is specified in the environment. | |
245 | See | |
246 | .Xr setvbuf 3 | |
247 | for additional details. | |
5b2abdfb A |
248 | .Sh RETURN VALUES |
249 | Upon successful completion | |
250 | .Fn fopen , | |
ad3c9f2a | 251 | .Fn fdopen , |
5b2abdfb | 252 | .Fn freopen |
b061a43b A |
253 | and |
254 | .Fn fmemopen | |
5b2abdfb A |
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 | |
9385eb3d A |
268 | argument |
269 | to | |
5b2abdfb A |
270 | .Fn fopen , |
271 | .Fn fdopen , | |
b061a43b | 272 | .Fn freopen , |
5b2abdfb | 273 | or |
b061a43b | 274 | .Fn fmemopen |
5b2abdfb A |
275 | was invalid. |
276 | .El | |
277 | .Pp | |
278 | The | |
279 | .Fn fopen , | |
b061a43b | 280 | .Fn fdopen , |
5b2abdfb | 281 | .Fn freopen |
b061a43b A |
282 | and |
283 | .Fn fmemopen | |
5b2abdfb A |
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 . | |
b061a43b A |
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. | |
5b2abdfb A |
325 | .Sh SEE ALSO |
326 | .Xr open 2 , | |
327 | .Xr fclose 3 , | |
9385eb3d | 328 | .Xr fileno 3 , |
5b2abdfb A |
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 | |
b061a43b A |
338 | .St -isoC , |
339 | with the exception of the | |
340 | .Dq Li x | |
341 | mode option which conforms to | |
342 | .St -isoC-2011 . | |
5b2abdfb A |
343 | The |
344 | .Fn fdopen | |
345 | function | |
346 | conforms to | |
347 | .St -p1003.1-88 . | |
b061a43b A |
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. |