]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/stdio/fopen.3
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 .\" 4. 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 .\" $FreeBSD$
34 .\"
35 .Dd January 30, 2013
36 .Dt FOPEN 3
37 .Os
38 .Sh NAME
39 .Nm fopen ,
40 .Nm fdopen ,
41 .Nm freopen ,
42 .Nm fmemopen
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 *path" "const char *mode" "FILE *stream"
54 .Ft FILE *
55 .Fn fmemopen "void *restrict *buf" "size_t size" "const char * restrict mode"
56 .Sh DESCRIPTION
57 The
58 .Fn fopen
59 function
60 opens the file whose name is the string pointed to by
61 .Fa path
62 and associates a stream with it.
63 .Pp
64 The argument
65 .Fa mode
66 points to a string beginning with one of the following letters:
67 .Bl -tag -width indent
68 .It Dq Li r
69 Open for reading.
70 The stream is positioned at the beginning of the file.
71 Fail if the file does not exist.
72 .It Dq Li w
73 Open for writing.
74 The stream is positioned at the beginning of the file.
75 Create the file if it does not exist.
76 .It Dq Li a
77 Open for writing.
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.
83 Create the file if it does not exist.
84 .El
85 .Pp
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 .Pp
104 The
105 .Fa mode
106 string can also include the letter
107 .Dq Li b
108 after either the
109 .Dq Li +
110 or the first letter.
111 This is strictly for compatibility with
112 .St -isoC
113 and has effect only for
114 .Fn fmemopen
115 ; otherwise the ``b'' is ignored.
116 .Pp
117 Any created files will have mode
118 .Do Dv S_IRUSR
119 \&|
120 .Dv S_IWUSR
121 \&|
122 .Dv S_IRGRP
123 \&|
124 .Dv S_IWGRP
125 \&|
126 .Dv S_IROTH
127 \&|
128 .Dv S_IWOTH Dc
129 .Pq Li 0666 ,
130 as modified by the process'
131 umask value (see
132 .Xr umask 2 ) .
133 .Pp
134 Reads and writes may be intermixed on read/write streams in any order,
135 and do not require an intermediate seek as in previous versions of
136 .Em stdio .
137 This is not portable to other systems, however;
138 .Tn ANSI C
139 requires that
140 a file positioning function intervene between output and input, unless
141 an input operation encounters end-of-file.
142 .Pp
143 The
144 .Fn fdopen
145 function associates a stream with the existing file descriptor,
146 .Fa fildes .
147 The mode
148 of the stream must be compatible with the mode of the file descriptor.
149 The
150 .Dq Li x
151 mode option is ignored.
152 When the stream is closed via
153 .Xr fclose 3 ,
154 .Fa fildes
155 is closed also.
156 .Pp
157 The
158 .Fn freopen
159 function
160 opens the file whose name is the string pointed to by
161 .Fa path
162 and associates the stream pointed to by
163 .Fa stream
164 with it.
165 The original stream (if it exists) is closed.
166 The
167 .Fa mode
168 argument is used just as in the
169 .Fn fopen
170 function.
171 .Pp
172 If the
173 .Fa path
174 argument is
175 .Dv NULL ,
176 .Fn freopen
177 attempts to re-open the file associated with
178 .Fa stream
179 with a new mode.
180 The new mode must be compatible with the mode that the stream was originally
181 opened with:
182 Streams open for reading can only be re-opened for reading,
183 streams open for writing can only be re-opened for writing,
184 and streams open for reading and writing can be re-opened in any mode.
185 The
186 .Dq Li x
187 mode option is not meaningful in this context.
188 .Pp
189 The primary use of the
190 .Fn freopen
191 function
192 is to change the file associated with a
193 standard text stream
194 .Dv ( stderr , stdin ,
195 or
196 .Dv stdout ) .
197 .Pp
198 The
199 .Fn fmemopen
200 function
201 associates the buffer given by the
202 .Fa buf
203 and
204 .Fa size
205 arguments with a stream.
206 The
207 .Fa buf
208 argument is either a null pointer or point to a buffer that
209 is at least
210 .Fa size
211 bytes long.
212 If a null pointer is specified as the
213 .Fa buf
214 argument,
215 .Fn fmemopen
216 allocates
217 .Fa size
218 bytes of memory. This buffer is automatically freed when the
219 stream is closed. Buffers can be opened in text-mode (default) or binary-mode
220 (if ``b'' is present in the second or third position of the
221 .Fa mode
222 argument). Buffers opened in text-mode make sure that writes are terminated with
223 a NULL byte, if the last write hasn't filled up the whole buffer. Buffers
224 opened in binary-mode never append a NULL byte.
225 .Sh RETURN VALUES
226 Upon successful completion
227 .Fn fopen ,
228 .Fn fdopen
229 and
230 .Fn freopen
231 return a
232 .Tn FILE
233 pointer.
234 Otherwise,
235 .Dv NULL
236 is returned and the global variable
237 .Va errno
238 is set to indicate the error.
239 .Sh ERRORS
240 .Bl -tag -width Er
241 .It Bq Er EINVAL
242 The
243 .Fa mode
244 argument
245 to
246 .Fn fopen ,
247 .Fn fdopen ,
248 .Fn freopen ,
249 or
250 .Fn fmemopen
251 was invalid.
252 .El
253 .Pp
254 The
255 .Fn fopen ,
256 .Fn fdopen ,
257 .Fn freopen
258 and
259 .Fn fmemopen
260 functions
261 may also fail and set
262 .Va errno
263 for any of the errors specified for the routine
264 .Xr malloc 3 .
265 .Pp
266 The
267 .Fn fopen
268 function
269 may also fail and set
270 .Va errno
271 for any of the errors specified for the routine
272 .Xr open 2 .
273 .Pp
274 The
275 .Fn fdopen
276 function
277 may also fail and set
278 .Va errno
279 for any of the errors specified for the routine
280 .Xr fcntl 2 .
281 .Pp
282 The
283 .Fn freopen
284 function
285 may also fail and set
286 .Va errno
287 for any of the errors specified for the routines
288 .Xr open 2 ,
289 .Xr fclose 3
290 and
291 .Xr fflush 3 .
292 .Sh SEE ALSO
293 .Xr open 2 ,
294 .Xr fclose 3 ,
295 .Xr fileno 3 ,
296 .Xr fseek 3 ,
297 .Xr funopen 3
298 .Sh STANDARDS
299 The
300 .Fn fopen
301 and
302 .Fn freopen
303 functions
304 conform to
305 .St -isoC ,
306 with the exception of the
307 .Dq Li x
308 mode option which conforms to
309 .St -isoC-2011 .
310 The
311 .Fn fdopen
312 function
313 conforms to
314 .St -p1003.1-88 .
315 The
316 .Fn fmemopen
317 function
318 conforms to
319 .St -p1003.1-2008 .
320 The ``b'' mode does not conform to any standard
321 but is also supported by glibc.