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