7139 Sync mDNS with mDNSResponder-625.41.2
[unleashed.git] / usr / src / man / man3c / stdio.3c
blobce4b9d1f126729101350cfdf01fe75a99a0fc5ae
1 '\" te
2 .\" Copyright 1989 AT&T.  Copyright (c) 2005, Sun Microsystems, Inc.  All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH STDIO 3C "May 18, 2005"
7 .SH NAME
8 stdio, stdin, stdout, stderr \- standard buffered input/output package
9 .SH SYNOPSIS
10 .LP
11 .nf
12 #include <stdio.h>
13 .fi
15 .LP
16 .nf
17 \fBextern FILE *\fR\fIstdin\fR\fB;\fR
18 .fi
20 .LP
21 .nf
22 \fBextern FILE *\fR\fIstdout\fR\fB;\fR
23 .fi
25 .LP
26 .nf
27 \fBextern FILE *\fR\fIstderr\fR\fB;\fR
28 .fi
30 .SH DESCRIPTION
31 .sp
32 .LP
33 The standard I/O functions described in section 3C of this manual constitute an
34 efficient, user-level \fBI/O\fR buffering scheme. The in-line macros
35 \fBgetc()\fR and \fBputc()\fR handle characters quickly. The macros
36 \fBgetchar\fR(3C) and \fBputchar\fR(3C), and the higher-level routines
37 \fBfgetc\fR(3C), \fBfgets\fR(3C), \fBfprintf\fR(3C), \fBfputc\fR(3C),
38 \fBfputs\fR(3C), \fBfread\fR(3C), \fBfscanf\fR(3C), \fBfwrite\fR(3C),
39 \fBgets\fR(3C), \fBgetw\fR(3C), \fBprintf\fR(3C), \fBputs\fR(3C),
40 \fBputw\fR(3C), and \fBscanf\fR(3C) all use or act as if they use \fBgetc()\fR
41 and \fBputc()\fR; they can be freely intermixed.
42 .sp
43 .LP
44 A file with associated buffering is called a \fIstream\fR (see \fBIntro\fR(3))
45 and is declared to be a pointer to a defined type \fBFILE\fR. The
46 \fBfopen\fR(3C) function creates certain descriptive data for a stream and
47 returns a pointer to designate the stream in all further transactions.
48 Normally, there are three open streams with constant pointers declared in the
49 \fB<stdio.h>\fR header and associated with the standard open files:
50 .sp
51 .ne 2
52 .na
53 \fB\fBstdin\fR\fR
54 .ad
55 .RS 10n
56 standard input file
57 .RE
59 .sp
60 .ne 2
61 .na
62 \fB\fBstdout\fR\fR
63 .ad
64 .RS 10n
65 standard output file
66 .RE
68 .sp
69 .ne 2
70 .na
71 \fB\fBstderr\fR\fR
72 .ad
73 .RS 10n
74 standard error file
75 .RE
77 .sp
78 .LP
79 The following symbolic values in <\fBunistd.h\fR> define the file descriptors
80 that will be associated with the C-language \fBstdin\fR, \fBstdout\fR and
81 \fBstderr\fR when the application is started:
82 .sp
84 .sp
85 .TS
86 l l l l
87 l l l l .
88 \fBSTDIN_FILENO\fR      Standard input value    0       \fBstdin\fR
89 \fBSTDOUT_FILENO\fR     Standard output value   1       \fBstdout\fR
90 \fBSTDERR_FILENO\fR     Standard error value    2       \fBstderr\fR
91 .TE
93 .sp
94 .LP
95 The constant \fINULL\fR designates a null pointer.
96 .sp
97 .LP
98 The integer-constant \fBEOF\fR is returned upon end-of-file or error by most
99 integer functions that deal with streams (see the individual descriptions for
100 details).
103 The integer constant \fBBUFSIZ\fR specifies the size of the buffers used by the
104 particular implementation.
107 The integer constant \fBFILENAME_MAX\fR specifies the number of bytes needed to
108 hold the longest pathname of a file allowed by the implementation.  If the
109 system does not impose a maximum limit, this value is the recommended size for
110 a buffer intended to hold a file's pathname.
113 The integer constant \fBFOPEN_MAX\fR specifies the minimum number of files that
114 the implementation guarantees can be open simultaneously. Note that no more
115 than 255 files may be opened using \fBfopen()\fR, and only file descriptors 0
116 through 255 can be used in a stream.
119 The functions and constants mentioned in the entries of section 3S of this
120 manual are declared in that header and need no further declaration. The
121 constants and the following "functions" are implemented as macros
122 (redeclaration of these names is perilous): \fBgetc()\fR, \fBgetchar()\fR,
123 \fBputc()\fR, \fBputchar()\fR, \fBferror\fR(3C), \fBfeof\fR(3C),
124 \fBclearerr\fR(3C), and \fBfileno\fR(3C). There are also function versions of
125 \fBgetc()\fR, \fBgetchar()\fR, \fBputc()\fR, \fBputchar()\fR, \fBferror()\fR,
126 \fBfeof()\fR, \fBclearerr()\fR, and \fBfileno()\fR.
129 Output streams, with the exception of the standard error stream \fBstderr\fR,
130 are by default buffered if the output refers to a file and line-buffered if the
131 output refers to a terminal. The standard error output stream \fBstderr\fR is
132 by default unbuffered, but use of \fBfreopen()\fR (see \fBfopen\fR(3C)) will
133 cause it to become buffered or line-buffered. When an output stream is
134 unbuffered, information is queued for writing on the destination file or
135 terminal as soon as written; when it is buffered, many characters are saved up
136 and written as a block. When it is line-buffered, each line of output is queued
137 for writing on the destination terminal as soon as the line is completed (that
138 is, as soon as a new-line character is written or terminal input is requested).
139 The \fBsetbuf()\fR or \fBsetvbuf()\fR functions (both described on the
140 \fBsetbuf\fR(3C) manual page) may be used to change the stream's buffering
141 strategy.
142 .SS "Interactions of Other FILE-Type C Functions"
145 A single open file description can be accessed both through streams and through
146 file descriptors.  Either a file descriptor or a stream will be called a
147 \fIhandle\fR on the open file description to which it refers; an open file
148 description may have several handles.
151 Handles can be created or destroyed by user action without affecting the
152 underlying open file description.  Some of the ways to create them include
153 \fBfcntl\fR(2), \fBdup\fR(2), \fBfdopen\fR(3C), \fBfileno\fR(3C) and
154 \fBfork\fR(2) (which duplicates existing ones into new processes). They can be
155 destroyed by at least \fBfclose\fR(3C) and \fBclose\fR(2), and by the
156 \fBexec\fR functions (see \fBexec\fR(2)), which close some file descriptors and
157 destroy streams.
160 A file descriptor that is never used in an operation and could affect the file
161 offset (for example \fBread\fR(2), \fBwrite\fR(2), or \fBlseek\fR(2)) is not
162 considered a handle in this discussion, but could give rise to one (as a
163 consequence of  \fBfdopen()\fR, \fBdup()\fR, or \fBfork()\fR, for example).
164 This exception does include the file descriptor underlying a stream, whether
165 created with  \fBfopen()\fR or \fBfdopen()\fR, as long as it is not used
166 directly by the application to affect the file offset.  (The \fBread()\fR and
167 \fBwrite()\fR functions implicitly affect the file offset;  \fBlseek()\fR
168 explicitly affects it.)
171 If two or more handles are used, and any one of them is a stream, their actions
172 shall be coordinated as described below.  If this is not done, the result is
173 undefined.
176 A handle that is a stream is considered to be closed when either an
177 \fBfclose()\fR or \fBfreopen\fR(3C) is executed on it (the result of
178 \fBfreopen()\fR is a new stream for this discussion, which cannot be a handle
179 on the same open file description as its previous value) or when the process
180 owning that stream terminates the \fBexit\fR(2) or \fBabort\fR(3C). A file
181 descriptor is closed by \fBclose()\fR, \fB_exit()\fR (see \fBexit\fR(2)), or by
182 one of the \fBexec\fR functions when \fBFD_CLOEXEC\fR is set on that file
183 descriptor.
186 For a handle to become the active handle, the actions below must be performed
187 between the last other user of the first handle (the current active handle) and
188 the first other user of the second handle (the future active handle). The
189 second handle then becomes the active handle. All activity by the application
190 affecting the file offset on the first handle shall be suspended until it again
191 becomes the active handle. (If a stream function has as an underlying function
192 that affects the file offset, the stream function will be considered to affect
193 the file offset.  The underlying functions are described below.)
196 The handles need not be in the same process for these rules to apply. Note that
197 after a \fBfork()\fR, two handles exist where one existed before.  The
198 application shall assure that, if both handles will ever be accessed, that they
199 will both be in a state where the other could become the active handle first.
200 The application shall prepare for a \fBfork()\fR exactly as if it were a change
201 of active handle.  (If the only action performed by one of the processes is one
202 of the \fBexec\fR functions or \fB_exit()\fR, the handle is never accessed in
203 that process.)
204 .RS +4
207 For the first handle, the first applicable condition below shall apply.
208 After the actions required below are taken, the handle may be closed if it is
209 still open.
210 .RS +4
213 If it is a file descriptor, no action is required.
215 .RS +4
218 If the only further action to be performed on any handle to this open file
219 description is to close it, no action need be taken.
221 .RS +4
224 If it is a stream that is unbuffered, no action need be taken.
226 .RS +4
229 If it is a stream that is line-buffered and the last character written to
230 the stream was a newline (that is, as if a \fBputc('\en')\fR was the most
231 recent operation on that stream), no action need be taken.
233 .RS +4
236 If it is a stream that is open for writing or append (but not also open for
237 reading), either an \fBfflush\fR(3C) shall occur or the stream shall be closed.
239 .RS +4
242 If the stream is open for reading and it is at the end of the file (
243 \fBfeof\fR(3C) is true), no action need be taken.
245 .RS +4
248 If the stream is open with a mode that allows reading and the underlying
249 open file description refers to a device that is capable of seeking, either an
250 \fBfflush()\fR shall occur or the stream shall be closed.
252 .RS +4
255 Otherwise, the result is undefined.
258 .RS +4
261 For the second handle: if any previous active handle has called a function
262 that explicitly changed the file offset, except as required above for the first
263 handle, the application shall perform an \fBlseek()\fR or an \fBfseek\fR(3C)
264 (as appropriate to the type of the handle) to an appropriate location.
266 .RS +4
269 If the active handle ceases to be accessible before the requirements on the
270 first handle above have been met, the state of the open file description
271 becomes undefined.  This might occur, for example, during a  \fBfork()\fR or an
272 \fB_exit()\fR.
274 .RS +4
277 The \fBexec\fR functions shall be considered to make inaccessible all
278 streams that are open at the time they are called, independent of what streams
279 or file descriptors may be available to the new process image.
281 .RS +4
284 Implementation shall assure that an application, even one consisting of
285 several processes, shall yield correct results (no data is lost or duplicated
286 when writing, all data is written in order, except as requested by seeks) when
287 the rules above are followed, regardless of the sequence of handles used.  If
288 the rules above are not followed, the result is unspecified. When these rules
289 are followed, it is implementation defined whether, and under what conditions,
290 all input is seen exactly once.
292 .SS "Use of stdio in Multithreaded Applications"
295 All the \fBstdio\fR functions are safe unless they have the \fB_unlocked\fR
296 suffix.  Each \fBFILE\fR pointer has its own lock to guarantee that only one
297 thread can access it. In the case that output needs to be synchronized, the
298 lock for the \fBFILE\fR pointer can be acquired before performing a series of
299 \fBstdio\fR operations.  For example:
301 .in +2
303 FILE iop;
304 flockfile(iop);
305 fprintf(iop, "hello ");
306 fprintf(iop, "world);
307 fputc(iop, 'a');
308 funlockfile(iop);
310 .in -2
314 will print everything out together, blocking other threads that might want to
315 write to the same file between calls to \fBfprintf()\fR.
318 An unlocked interface is available in case performace is an issue.  For
319 example:
321 .in +2
323 flockfile(iop);
324 while (!feof(iop)) {
325         *c++ = getc_unlocked(iop);
327 funlockfile(iop);
329 .in -2
331 .SH RETURN VALUES
334 Invalid stream pointers usually cause grave disorder, possibly including
335 program termination. Individual function descriptions describe the possible
336 error conditions.
337 .SH SEE ALSO
340 \fBclose\fR(2), \fBlseek\fR(2), \fBopen\fR(2), \fBpipe\fR(2), \fBread\fR(2),
341 \fBwrite\fR(2), \fBctermid\fR(3C), \fBcuserid\fR(3C), \fBfclose\fR(3C),
342 \fBferror\fR(3C), \fBfopen\fR(3C), \fBfread\fR(3C), \fBfseek\fR(3C),
343 \fBflockfile\fR(3C), \fBgetc\fR(3C), \fBgets\fR(3C), \fBpopen\fR(3C),
344 \fBprintf\fR(3C), \fBputc\fR(3C), \fBputs\fR(3C), \fBscanf\fR(3C),
345 \fBsetbuf\fR(3C), \fBsystem\fR(3C), \fBtmpfile\fR(3C), \fBtmpnam\fR(3C),
346 \fBungetc\fR(3C)