xref: /openbsd/lib/libc/stdio/stdio.3 (revision 274d7c50)
1.\"	$OpenBSD: stdio.3,v 1.31 2014/07/03 06:08:06 jmc 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 $Mdocdate: July 3 2014 $
31.Dt STDIO 3
32.Os
33.Sh NAME
34.Nm stdio
35.Nd standard input/output library functions
36.Sh SYNOPSIS
37.In stdio.h
38.Pp
39.Fd FILE *stdin;
40.Fd FILE *stdout;
41.Fd FILE *stderr;
42.Sh DESCRIPTION
43The standard I/O library
44provides a simple and efficient buffered stream I/O interface.
45Input and output is mapped into logical data streams and the physical I/O
46characteristics are concealed.
47The functions and macros are listed below;
48more information is available from the individual man pages.
49.Pp
50A stream is associated with an external file (which may be a physical
51device) by
52.Dq opening
53a file, which may involve creating a new file.
54Creating an existing file causes its former contents to be discarded.
55If a file can support positioning requests (such as a disk file, as opposed
56to a terminal) then a
57.Dq file position indicator
58associated with the stream is positioned at the start of the file (byte
59zero), unless the file is opened with append mode.
60If append mode
61is used, the position indicator will be placed at the end-of-file.
62The position indicator is maintained by subsequent reads, writes,
63and positioning requests.
64All input occurs as if the characters
65were read by successive calls to the
66.Xr fgetc 3
67function; all output takes place as if all characters were
68written by successive calls to the
69.Xr fputc 3
70function.
71.Pp
72A file is disassociated from a stream by
73.Dq closing
74it.
75Output streams are flushed (any unwritten buffer contents are transferred
76to the host environment) before the stream is disassociated from the file.
77The value of a pointer to a
78.Dv FILE
79object is indeterminate (garbage) after a file is closed.
80.Pp
81A file may be subsequently reopened, by the same or another program
82execution, and its contents reclaimed or modified (if it can be repositioned
83at the start).
84If the main function returns to its original caller, or the
85.Xr exit 3
86function is called, all open files are closed (hence all output
87streams are flushed) before program termination.
88Other methods of program termination may not close files properly and hence
89buffered output may be lost.
90In particular,
91.Xr _exit 2
92does not flush
93.Nm
94files.
95Neither does an exit due to a signal.
96Buffers are flushed by
97.Xr abort 3 ,
98as required by POSIX, although in previous implementations they were not.
99.Pp
100This implementation needs and makes
101no distinction between
102.Dq text
103and
104.Dq binary
105streams.
106In effect, all streams are binary.
107No translation is performed and no extra padding appears on any stream.
108.Pp
109At program startup, three streams are predefined and need not be
110opened explicitly:
111.Pp
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
123.Pp
124These streams are abbreviated
125.Em stdin ,
126.Em stdout ,
127and
128.Em stderr .
129Initially, the standard error stream
130is unbuffered; the standard input and output streams are
131fully buffered if and only if the streams do not refer to
132an interactive or
133.Dq terminal
134device, as determined by the
135.Xr isatty 3
136function.
137In fact,
138.Em all
139freshly opened streams that refer to terminal devices
140default to line buffering, and
141pending output to such streams is written automatically
142whenever such an input stream is read.
143Note that this applies only to
144.Dq "true reads" ;
145if the read request can be satisfied by existing buffered data,
146no automatic flush will occur.
147In these cases,
148or when a large amount of computation is done after printing
149part of a line on an output terminal, it is necessary to
150.Xr fflush 3
151the standard output so that the output will appear immediately.
152Alternatively, these defaults may be modified via the
153.Xr setvbuf 3
154function.
155.Pp
156The
157.Nm stdio
158library is a part of the library libc
159and routines are automatically loaded as needed by the compiler.
160The SYNOPSIS
161sections of the following manual pages indicate which include files
162are to be used, what the compiler declaration for the function
163looks like, and which external variables are of interest.
164.Pp
165The following are defined as macros;
166these names may not be re-used
167without first removing their current definitions with
168.Dv #undef :
169.Dv BUFSIZ ,
170.Dv EOF ,
171.Dv FILENAME_MAX ,
172.Dv FOPEN_MAX ,
173.Dv L_ctermid ,
174.Dv L_tmpnam ,
175.Dv NULL ,
176.Dv SEEK_END ,
177.Dv SEEK_SET ,
178.Dv SEEK_CUR ,
179.Dv TMP_MAX ,
180.Dv clearerr ,
181.Dv feof ,
182.Dv ferror ,
183.Dv fileno ,
184.Dv freopen ,
185.Dv fwopen ,
186.Dv getc ,
187.Dv getchar ,
188.Dv putc ,
189.Dv putchar ,
190.Dv stderr ,
191.Dv stdin ,
192.Dv stdout .
193Function versions of the macro functions
194.Xr feof 3 ,
195.Xr ferror 3 ,
196.Xr clearerr 3 ,
197.Xr fileno 3 ,
198.Xr getc 3 ,
199.Xr getchar 3 ,
200.Xr putc 3 ,
201and
202.Xr putchar 3
203exist and will be used if the macro
204definitions are explicitly removed.
205.Sh LIST OF FUNCTIONS
206.Bl -column "sys_errlist" "Description"
207.It Sy Function Ta Sy Description
208.It asprintf Ta "formatted output conversion with allocation"
209.It clearerr Ta "check and reset stream status"
210.It dprintf Ta "formatted output conversion"
211.It fclose Ta "close a stream"
212.It fdopen Ta "stream open functions"
213.It feof Ta "check and reset stream status"
214.It ferror Ta "check and reset stream status"
215.It fflush Ta "flush a stream"
216.It fgetc Ta "get next character or word from input stream"
217.It fgetln Ta "get a line from a stream"
218.It fgetpos Ta "reposition a stream"
219.It fgets Ta "get a line from a stream"
220.It fgetwc Ta "get next wide character from input stream"
221.It fgetws Ta "get a line of wide characters from a stream"
222.It fileno Ta "get a stream's underlying file descriptor"
223.It fopen Ta "stream open functions"
224.It fprintf Ta "formatted output conversion"
225.It fpurge Ta "flush a stream"
226.It fputc Ta "output a character or word to a stream"
227.It fputs Ta "output a line to a stream"
228.It fputwc Ta "output a wide character to a stream"
229.It fputws Ta "output a line of wide characters to a stream"
230.It fread Ta "binary stream input/output"
231.It freopen Ta "stream open functions"
232.It fropen Ta "open a stream"
233.It fscanf Ta "input format conversion"
234.It fseek Ta "reposition a stream"
235.It fsetpos Ta "reposition a stream"
236.It ftell Ta "reposition a stream"
237.It funopen Ta "open a stream"
238.It fwide Ta "set/get orientation of stream"
239.It fwopen Ta "open a stream"
240.It fwprintf Ta "formatted wide character output conversion"
241.It fwrite Ta "binary stream input/output"
242.It getc Ta "get next character or word from input stream"
243.It getchar Ta "get next character or word from input stream"
244.It getdelim Ta "read a delimited record from a stream"
245.It getline Ta "read a delimited record from a stream"
246.It getw Ta "get next character or word from input stream"
247.It getwc Ta "get next wide character from input stream"
248.It getwchar Ta "get next wide character from input stream"
249.It mkdtemp Ta "create unique temporary directory"
250.It mkstemp Ta "create unique temporary file"
251.It mktemp Ta "create unique temporary file"
252.It perror Ta "system error messages"
253.It printf Ta "formatted output conversion"
254.It putc Ta "output a character or word to a stream"
255.It putchar Ta "output a character or word to a stream"
256.It puts Ta "output a line to a stream"
257.It putw Ta "output a character or word to a stream"
258.It putwc Ta "output a wide character to a stream"
259.It putwchar Ta "output a wide character to a stream"
260.It remove Ta "remove directory entry"
261.It rewind Ta "reposition a stream"
262.It scanf Ta "input format conversion"
263.It setbuf Ta "stream buffering operations"
264.It setbuffer Ta "stream buffering operations"
265.It setlinebuf Ta "stream buffering operations"
266.It setvbuf Ta "stream buffering operations"
267.It snprintf Ta "formatted output conversion"
268.It sprintf Ta "formatted output conversion"
269.It sscanf Ta "input format conversion"
270.It strerror Ta "system error messages"
271.It swprintf Ta "formatted wide character output conversion"
272.It sys_errlist Ta "system error messages"
273.It sys_nerr Ta "system error messages"
274.It tempnam Ta "temporary file routines"
275.It tmpfile Ta "temporary file routines"
276.It tmpnam Ta "temporary file routines"
277.It ungetc Ta "un-get character from input stream"
278.It ungetwc Ta "un-get wide character from input stream"
279.It vasprintf Ta "formatted output conversion with allocation"
280.It vdprintf Ta "formatted output conversion"
281.It vfprintf Ta "formatted output conversion"
282.It vfscanf Ta "input format conversion"
283.It vfwprintf Ta "formatted wide character output conversion"
284.It vprintf Ta "formatted output conversion"
285.It vscanf Ta "input format conversion"
286.It vsnprintf Ta "formatted output conversion"
287.It vsprintf Ta "formatted output conversion"
288.It vsscanf Ta "input format conversion"
289.It vswprintf Ta "formatted wide character output conversion"
290.It vwprintf Ta "formatted wide character output conversion"
291.It wprintf Ta "formatted wide character output conversion"
292.El
293.Sh SEE ALSO
294.Xr close 2 ,
295.Xr open 2 ,
296.Xr read 2 ,
297.Xr write 2
298.Sh STANDARDS
299The
300.Nm stdio
301library conforms to
302.St -isoC-99 .
303.Sh BUGS
304The standard buffered functions do not interact well with certain other
305library and system functions, especially
306.Xr vfork 2
307and
308.Xr abort 3 .
309