xref: /openbsd-src/lib/libc/stdio/stdio.3 (revision 0eea0d082377cb9c3ec583313dc4d52b7b6a4d6d)
1.\"	$OpenBSD: stdio.3,v 1.17 2004/06/20 21:09:30 jfb Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd April 19, 1994
31.Dt STDIO 3
32.Os
33.Sh NAME
34.Nm stdio
35.Nd standard input/output library functions
36.Sh SYNOPSIS
37.Fd #include <stdio.h>
38.Pp
39.Fd FILE *stdin;
40.Fd FILE *stdout;
41.Fd FILE *stderr;
42.Sh DESCRIPTION
43The standard
44.Tn I/O
45library provides a simple and efficient buffered stream
46.Tn I/O
47interface.
48Input and output is mapped into logical data streams and the physical
49.Tn I/O
50characteristics are concealed.
51The functions and macros are listed below;
52more information is available from the individual man pages.
53.Pp
54A stream is associated with an external file (which may be a physical
55device) by
56.Dq opening
57a file, which may involve creating a new file.
58Creating an existing file causes its former contents to be discarded.
59If a file can support positioning requests (such as a disk file, as opposed
60to a terminal) then a
61.Dq file position indicator
62associated with the stream is positioned at the start of the file (byte
63zero), unless the file is opened with append mode.
64If append mode
65is used, the position indicator will be placed at the end-of-file.
66The position indicator is maintained by subsequent reads, writes
67and positioning requests.
68All input occurs as if the characters
69were read by successive calls to the
70.Xr fgetc 3
71function; all output takes place as if all characters were
72written by successive calls to the
73.Xr fputc 3
74function.
75.Pp
76A file is disassociated from a stream by
77.Dq closing
78it.
79Output streams are flushed (any unwritten buffer contents are transferred
80to the host environment) before the stream is disassociated from the file.
81The value of a pointer to a
82.Dv FILE
83object is indeterminate (garbage) after a file is closed.
84.Pp
85A file may be subsequently reopened, by the same or another program
86execution, and its contents reclaimed or modified (if it can be repositioned
87at the start).
88If the main function returns to its original caller, or the
89.Xr exit 3
90function is called, all open files are closed (hence all output
91streams are flushed) before program termination.
92Other methods of program termination may not close files properly and hence
93buffered output may be lost.
94In particular,
95.Xr _exit 2
96does not flush stdio files.
97Neither does an exit due to a signal.
98Buffers are flushed by
99.Xr abort 3
100as required by POSIX, although previous implementations did not.
101.Pp
102This implementation needs and makes
103no distinction between
104.Dq text
105and
106.Dq binary
107streams.
108In effect, all streams are binary.
109No translation is performed and no extra padding appears on any stream.
110.Pp
111At program startup, three streams are predefined and need not be
112opened explicitly:
113.Pp
114.Bl -bullet -compact -offset indent
115.It
116.Em standard input
117(for reading conventional input),
118.It
119.Em standard output
120(for writing conventional output), and
121.It
122.Em standard error
123(for writing diagnostic output).
124.El
125.Pp
126These streams are abbreviated
127.Em stdin ,
128.Em stdout ,
129and
130.Em stderr .
131Initially, the standard error stream
132is unbuffered; the standard input and output streams are
133fully buffered if and only if the streams do not refer to
134an interactive or
135.Dq terminal
136device, as determined by the
137.Xr isatty 3
138function.
139In fact,
140.Em all
141freshly opened streams that refer to terminal devices
142default to line buffering, and
143pending output to such streams is written automatically
144whenever such an input stream is read.
145Note that this applies only to
146.Dq "true reads" ;
147if the read request can be satisfied by existing buffered data,
148no automatic flush will occur.
149In these cases,
150or when a large amount of computation is done after printing
151part of a line on an output terminal, it is necessary to
152.Xr fflush 3
153the standard output before going off and computing so that the output
154will appear.
155Alternatively, these defaults may be modified via the
156.Xr setvbuf 3
157function.
158.Pp
159The
160.Nm stdio
161library is a part of the library
162.Xr libc
163and routines are automatically loaded as needed by the compiler.
164The
165.Tn SYNOPSIS
166sections of the following manual pages indicate which include files
167are to be used, what the compiler declaration for the function
168looks like and which external variables are of interest.
169.Pp
170The following are defined as macros;
171these names may not be re-used
172without first removing their current definitions with
173.Dv #undef :
174.Dv BUFSIZ ,
175.Dv EOF ,
176.Dv FILENAME_MAX ,
177.Dv FOPEN_MAX ,
178.Dv L_cuserid ,
179.Dv L_ctermid ,
180.Dv L_tmpnam ,
181.Dv NULL ,
182.Dv SEEK_END ,
183.Dv SEEK_SET ,
184.Dv SEE_CUR ,
185.Dv TMP_MAX ,
186.Dv clearerr ,
187.Dv feof ,
188.Dv ferror ,
189.Dv fileno ,
190.Dv freopen ,
191.Dv fwopen ,
192.Dv getc ,
193.Dv getchar ,
194.Dv putc ,
195.Dv putchar ,
196.Dv stderr ,
197.Dv stdin ,
198.Dv stdout .
199Function versions of the macro functions
200.Xr feof ,
201.Xr ferror ,
202.Xr clearerr ,
203.Xr fileno ,
204.Xr getc ,
205.Xr getchar ,
206.Xr putc ,
207and
208.Xr putchar
209exist and will be used if the macro
210definitions are explicitly removed.
211.Sh LIST OF FUNCTIONS
212.Bl -column "Description"
213.Sy Function	Description
214asprintf	formatted output conversion with allocation
215clearerr	check and reset stream status
216fclose	close a stream
217fdopen	stream open functions
218feof	check and reset stream status
219ferror	check and reset stream status
220fflush	flush a stream
221fgetc	get next character or word from input stream
222fgetln	get a line from a stream
223fgetpos	reposition a stream
224fgets	get a line from a stream
225fileno	get a stream's underlying file descriptor
226fopen	stream open functions
227fprintf	formatted output conversion
228fpurge	flush a stream
229fputc	output a character or word to a stream
230fputs	output a line to a stream
231fread	binary stream input/output
232freopen	stream open functions
233fropen	open a stream
234fscanf	input format conversion
235fseek	reposition a stream
236fsetpos	reposition a stream
237ftell	reposition a stream
238funopen	open a stream
239fwopen	open a stream
240fwrite	binary stream input/output
241getc	get next character or word from input stream
242getchar	get next character or word from input stream
243gets	get a line from a stream
244getw	get next character or word from input stream
245mkstemp	create unique temporary file
246mktemp	create unique temporary file
247perror	system error messages
248printf	formatted output conversion
249putc	output a character or word to a stream
250putchar	output a character or word to a stream
251puts	output a line to a stream
252putw	output a character or word to a stream
253remove	remove directory entry
254rewind	reposition a stream
255scanf	input format conversion
256setbuf	stream buffering operations
257setbuffer	stream buffering operations
258setlinebuf	stream buffering operations
259setvbuf	stream buffering operations
260snprintf	formatted output conversion
261sprintf	formatted output conversion
262sscanf	input format conversion
263strerror	system error messages
264sys_errlist	system error messages
265sys_nerr	system error messages
266tempnam	temporary file routines
267tmpfile	temporary file routines
268tmpnam	temporary file routines
269ungetc	un-get character from input stream
270vasprintf	formatted output conversion with allocation
271vfprintf	formatted output conversion
272vfscanf	input format conversion
273vprintf	formatted output conversion
274vscanf	input format conversion
275vsnprintf	formatted output conversion
276vsprintf	formatted output conversion
277vsscanf	input format conversion
278.El
279.Sh SEE ALSO
280.Xr close 2 ,
281.Xr open 2 ,
282.Xr read 2 ,
283.Xr write 2
284.Sh STANDARDS
285The
286.Nm stdio
287library conforms to
288.St -ansiC .
289.Sh BUGS
290The standard buffered functions do not interact well with certain other
291library and system functions, especially
292.Xr vfork
293and
294.Xr abort .
295