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