Upgrade openssh. 1/2
[dragonfly.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 .\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.20 2007/01/09 00:28:06 imp Exp $
34 .\"
35 .Dd February 22, 2018
36 .Dt FOPEN 3
37 .Os
38 .Sh NAME
39 .Nm fopen ,
40 .Nm fdopen ,
41 .Nm freopen
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 * restrict path" "const char * restrict mode" "FILE * restrict stream"
53 .Sh DESCRIPTION
54 The
55 .Fn fopen
56 function
57 opens the file whose name is the string pointed to by
58 .Fa path
59 and associates a stream with it.
60 .Pp
61 The argument
62 .Fa mode
63 points to a string beginning with one of the following
64 sequences (Additional characters may follow these sequences.):
65 .Bl -tag -width indent
66 .It Dq Li r
67 Open file for reading.
68 The stream is positioned at the beginning of the file.
69 .It Dq Li w
70 Open file for writing.
71 Creates the file if it does not already exist,
72 truncates the file if it does.
73 .It Dq Li a
74 Open file for append.
75 Creates the file if it does not already exist.
76 Seeks to end of the file.
77 Subsequent writes to the file will always end up at the then current
78 end of file, irrespective of any intervening
79 .Xr fseek 3
80 or similar operation.
81 .El
82 .Pp
83 Additional flags may modify the primary mode as follows:
84 .Bl -tag -width indent
85 .It Dq Li +
86 The file will be opened for both reading and writing.
87 For example 'r+' or 'w+'.
88 .It Dq Li x
89 The open will fail if the file already exists (C11).
90 For example 'wx'.
91 .It Dq Li e
92 Close-on-exec.
93 The underlying descriptor will automatically be closed on any
94 .Fn exec
95 (glibc compatibility).
96 .It Dq Li b
97 Binary mode.
98 On UNIX systems file reading and writing is always in binary
99 mode so this flag has no effect.
100 .El
101 .Pp
102 The
103 .Fa mode
104 string can also include the letter ``b'' either as a third character or
105 as a character between the characters in any of the two-character strings
106 described above.
107 This is strictly for compatibility with
108 .St -isoC-2011
109 and has no effect; the ``b'' is ignored.
110 .Pp
111 Any created files will have mode
112 .Pf \\*q Dv S_IRUSR
113 \&|
114 .Dv S_IWUSR
115 \&|
116 .Dv S_IRGRP
117 \&|
118 .Dv S_IWGRP
119 \&|
120 .Dv S_IROTH
121 \&|
122 .Dv S_IWOTH Ns \\*q
123 .Pq Li 0666 ,
124 as modified by the process'
125 umask value (see
126 .Xr umask 2 ) .
127 .Pp
128 Reads and writes may be intermixed on read/write streams in any order,
129 and do not require an intermediate seek as in previous versions of
130 .Em stdio .
131 This is not portable to other systems, however;
132 .Tn ANSI C
133 requires that
134 a file positioning function intervene between output and input, unless
135 an input operation encounters end-of-file.
136 .Pp
137 The
138 .Fn fdopen
139 function associates a stream with the existing file descriptor,
140 .Fa fildes .
141 The mode
142 of the stream must be compatible with the mode of the file descriptor.
143 When the stream is closed via
144 .Xr fclose 3 ,
145 .Fa fildes
146 is closed also.
147 .Pp
148 The
149 .Fn freopen
150 function
151 opens the file whose name is the string pointed to by
152 .Fa path
153 and associates the stream pointed to by
154 .Fa stream
155 with it.
156 The original stream (if it exists) is closed.
157 The
158 .Fa mode
159 argument is used just as in the
160 .Fn fopen
161 function.
162 .Pp
163 If the
164 .Fa path
165 argument is
166 .Dv NULL ,
167 .Fn freopen
168 attempts to re-open the file associated with
169 .Fa stream
170 with a new mode.
171 The new mode must be compatible with the mode that the stream was originally
172 opened with:
173 .Bl -bullet -offset indent
174 .It
175 Streams originally opened with mode
176 .Dq Li r
177 can only be reopened with that same mode.
178 .It
179 Streams originally opened with mode
180 .Dq Li a
181 can be reopened with the same mode, or mode
182 .Dq Li w .
183 .It
184 Streams originally opened with mode
185 .Dq Li w
186 can be reopened with the same mode, or mode
187 .Dq Li a .
188 .It
189 Streams originally opened with mode
190 .Dq Li r+ ,
191 .Dq Li w+ ,
192 or
193 .Dq Li a+
194 can be reopened with any mode.
195 .El
196 .Pp
197 The primary use of the
198 .Fn freopen
199 function
200 is to change the file associated with a
201 standard text stream
202 .Dv ( stderr , stdin ,
203 or
204 .Dv stdout ) .
205 .Sh RETURN VALUES
206 Upon successful completion
207 .Fn fopen ,
208 .Fn fdopen
209 and
210 .Fn freopen
211 return a
212 .Tn FILE
213 pointer.
214 Otherwise,
215 .Dv NULL
216 is returned and the global variable
217 .Va errno
218 is set to indicate the error.
219 .Sh ERRORS
220 .Bl -tag -width Er
221 .It Bq Er EINVAL
222 The
223 .Fa mode
224 argument
225 to
226 .Fn fopen ,
227 .Fn fdopen ,
228 or
229 .Fn freopen
230 was invalid.
231 .El
232 .Pp
233 The
234 .Fn fopen ,
235 .Fn fdopen
236 and
237 .Fn freopen
238 functions
239 may also fail and set
240 .Va errno
241 for any of the errors specified for the routine
242 .Xr malloc 3 .
243 .Pp
244 The
245 .Fn fopen
246 function
247 may also fail and set
248 .Va errno
249 for any of the errors specified for the routine
250 .Xr open 2 .
251 .Pp
252 The
253 .Fn fdopen
254 function
255 may also fail and set
256 .Va errno
257 for any of the errors specified for the routine
258 .Xr fcntl 2 .
259 .Pp
260 The
261 .Fn freopen
262 function
263 may also fail and set
264 .Va errno
265 for any of the errors specified for the routines
266 .Xr open 2 ,
267 .Xr fclose 3
268 and
269 .Xr fflush 3 .
270 .Sh SEE ALSO
271 .Xr open 2 ,
272 .Xr fclose 3 ,
273 .Xr fileno 3 ,
274 .Xr fseek 3 ,
275 .Xr funopen 3
276 .Sh STANDARDS
277 The
278 .Fn fopen
279 and
280 .Fn freopen
281 functions
282 conform to
283 .St -isoC-2011 .
284 The
285 .Fn fdopen
286 function
287 conforms to
288 .St -p1003.1-88 .