]> git.saurik.com Git - apple/libc.git/blame - stdio/fopen.3
Libc-594.9.1.tar.gz
[apple/libc.git] / stdio / fopen.3
CommitLineData
224c7076
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.
16.\" 3. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
37.\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.18 2003/01/26 10:01:59 tjr Exp $
38.\"
39.Dd January 26, 2003
40.Dt FOPEN 3
41.Os
42.Sh NAME
43.Nm fdopen ,
44.Nm fopen ,
45.Nm freopen
46.Nd stream open functions
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.Ft FILE *
52.Fo fdopen
53.Fa "int fildes"
54.Fa "const char *mode"
55.Fc
56.Ft FILE *
57.Fo fopen
58.Fa "const char *restrict filename"
59.Fa "const char *restrict mode"
60.Fc
61.Ft FILE *
62.Fo freopen
63.Fa "const char *restrict filename"
64.Fa "const char *restrict mode"
65.Fa "FILE *restrict stream"
66.Fc
67.Sh DESCRIPTION
68The
69.Fn fopen
70function
71opens the file whose name is the string pointed to by
72.Fa filename
73and associates a stream with it.
74.Pp
75The argument
76.Fa mode
77points to a string beginning with one of the following
78sequences (Additional characters may follow these sequences.):
79.Bl -tag -width indent
80.It Dq Li r
81Open text file for reading.
82The stream is positioned at the beginning of the file.
83.It Dq Li r+
84Open for reading and writing.
85The stream is positioned at the beginning of the file.
86.It Dq Li w
87Truncate file to zero length or create text file for writing.
88The stream is positioned at the beginning of the file.
89.It Dq Li w+
90Open for reading and writing.
91The file is created if it does not exist, otherwise it is truncated.
92The stream is positioned at the beginning of the file.
93.It Dq Li a
94Open for writing.
95The file is created if it does not exist.
96The stream is positioned at the end of the file.
97Subsequent writes to the file will always end up at the then current
98end of file, irrespective of any intervening
99.Xr fseek 3
100or similar.
101.It Dq Li a+
102Open for reading and writing.
103The file is created if it does not exist.
104The stream is positioned at the end of the file.
105Subsequent writes to the file will always end up at the then current
106end of file, irrespective of any intervening
107.Xr fseek 3
108or similar.
109.El
110.Pp
111The
112.Fa mode
113string can also include the letter ``b'' either as a third character or
114as a character between the characters in any of the two-character strings
115described above.
116This is strictly for compatibility with
117.St -isoC
118and has no effect; the ``b'' is ignored.
119.Pp
120Finally, as an extension to the standards (and thus may not be portable),
121.Fa mode
122string may end with the letter ``x'', which insists on creating a new file
123when used with ``w'' or ``a''.
124If
125.Fa path
126exists, then an error is returned (this is the equivalent of specifying
127.Dv O_EXCL
128with
129.Xr open 2 ) .
130.Pp
131Any created files will have mode
132.Pf \\*q Dv S_IRUSR
133\&|
134.Dv S_IWUSR
135\&|
136.Dv S_IRGRP
137\&|
138.Dv S_IWGRP
139\&|
140.Dv S_IROTH
141\&|
142.Dv S_IWOTH Ns \\*q
143.Pq Li 0666 ,
144as modified by the process'
145umask value (see
146.Xr umask 2 ) .
147.Pp
148Reads and writes may be intermixed on read/write streams in any order,
149and do not require an intermediate seek as in previous versions of
150.Em stdio .
151This is not portable to other systems, however;
152.Tn ANSI C
153requires that
154a file positioning function intervene between output and input, unless
155an input operation encounters end-of-file.
156.Pp
157The
158.Fn fdopen
159function associates a stream with the existing file descriptor,
160.Fa fildes .
161The mode
162of the stream must be compatible with the mode of the file descriptor.
163When the stream is closed via
164.Xr fclose 3 ,
165.Fa fildes
166is closed also.
167.Pp
168The
169.Fn freopen
170function
171opens the file whose name is the string pointed to by
172.Fa filename
173and associates the stream pointed to by
174.Fa stream
175with it.
176The original stream (if it exists) is closed.
177The
178.Fa mode
179argument is used just as in the
180.Fn fopen
181function.
182.Pp
183If the
184.Fa filename
185argument is
186.Dv NULL ,
187.Fn freopen
188attempts to re-open the file associated with
189.Fa stream
190with a new mode.
191The new mode must be compatible with the mode that the stream was originally
192opened with:
193.Bl -bullet -offset indent
194.It
195Streams originally opened with mode
196.Dq Li r
197can only be reopened with that same mode.
198.It
199Streams originally opened with mode
200.Dq Li a
201can be reopened with the same mode, or mode
202.Dq Li w .
203.It
204Streams originally opened with mode
205.Dq Li w
206can be reopened with the same mode, or mode
207.Dq Li a .
208.It
209Streams originally opened with mode
210.Dq Li r+ ,
211.Dq Li w+ ,
212or
213.Dq Li a+
214can be reopened with any mode.
215.El
216.Pp
217The primary use of the
218.Fn freopen
219function
220is to change the file associated with a
221standard text stream
222.Dv ( stderr , stdin ,
223or
224.Dv stdout ) .
225.Sh RETURN VALUES
226Upon successful completion
227.Fn fopen ,
228.Fn fdopen ,
229and
230.Fn freopen
231return a
232.Tn FILE
233pointer.
234Otherwise,
235.Dv NULL
236is returned and the global variable
237.Va errno
238is set to indicate the error.
239.Sh ERRORS
240.Bl -tag -width Er
241.It Bq Er EINVAL
242The
243.Fa mode
244argument
245to
246.Fn fopen ,
247.Fn fdopen ,
248or
249.Fn freopen
250was invalid.
251.El
252.Pp
253The
254.Fn fopen ,
255.Fn fdopen
256and
257.Fn freopen
258functions
259may also fail and set
260.Va errno
261for any of the errors specified for the routine
262.Xr malloc 3 .
263.Pp
264The
265.Fn fopen
266function
267may also fail and set
268.Va errno
269for any of the errors specified for the routine
270.Xr open 2 .
271.Pp
272The
273.Fn fdopen
274function
275may also fail and set
276.Va errno
277for any of the errors specified for the routine
278.Xr fcntl 2 .
279.Pp
280The
281.Fn freopen
282function
283may also fail and set
284.Va errno
285for any of the errors specified for the routines
286.Xr open 2 ,
287.Xr fclose 3
288and
289.Xr fflush 3 .
290.Sh SEE ALSO
291.Xr open 2 ,
292.Xr fclose 3 ,
293.Xr fileno 3 ,
294.Xr fseek 3 ,
295.Xr funopen 3
296.Sh STANDARDS
297The
298.Fn fopen
299and
300.Fn freopen
301functions
302conform to
303.St -isoC .
304The
305.Fn fdopen
306function
307conforms to
308.St -p1003.1-88 .