xref: /freebsd/lib/libc/stdio/stdio.3 (revision 3236e08bf815575a0ea94ea683d969b7b15bfba4)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
3258f0484fSRodney W. Grimes.\"     @(#)stdio.3	8.7 (Berkeley) 4/19/94
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
3572c9dabbSTim J. Robbins.Dd January 10, 2003
3658f0484fSRodney W. Grimes.Dt STDIO 3
37a307d598SRuslan Ermilov.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm stdio
4058f0484fSRodney W. Grimes.Nd standard input/output library functions
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4432eef9aeSRuslan Ermilov.In stdio.h
45d0353b83SRuslan Ermilov.Vt FILE *stdin ;
46d0353b83SRuslan Ermilov.Vt FILE *stdout ;
47d0353b83SRuslan Ermilov.Vt FILE *stderr ;
4858f0484fSRodney W. Grimes.Sh DESCRIPTION
4958f0484fSRodney W. GrimesThe standard
5058f0484fSRodney W. Grimes.Tn I/O
5158f0484fSRodney W. Grimeslibrary provides a simple and efficient buffered stream
5258f0484fSRodney W. Grimes.Tn I/O
5358f0484fSRodney W. Grimesinterface.
54a5ed710cSMike PritchardInput and output is mapped into logical data streams
5558f0484fSRodney W. Grimesand the physical
5658f0484fSRodney W. Grimes.Tn I/O
57c6ff3a1bSSheldon Hearncharacteristics are concealed.
58c6ff3a1bSSheldon HearnThe functions and macros are listed
5958f0484fSRodney W. Grimesbelow; more information is available from the individual man pages.
6058f0484fSRodney W. Grimes.Pp
6158f0484fSRodney W. GrimesA stream is associated with an external file (which may be a physical
6258f0484fSRodney W. Grimesdevice) by
6358f0484fSRodney W. Grimes.Em opening
64c6ff3a1bSSheldon Hearna file, which may involve creating a new file.
65c6ff3a1bSSheldon HearnCreating an
6658f0484fSRodney W. Grimesexisting file causes its former contents to be discarded.
6758f0484fSRodney W. GrimesIf a file can support positioning requests (such as a disk file, as opposed
6858f0484fSRodney W. Grimesto a terminal) then a
6958f0484fSRodney W. Grimes.Em file position indicator
7058f0484fSRodney W. Grimesassociated with the stream is positioned at the start of the file (byte
71c6ff3a1bSSheldon Hearnzero), unless the file is opened with append mode.
72c6ff3a1bSSheldon HearnIf append mode
73b8e5e42dSStephen McKayis used, the position indicator will be placed at the end-of-file.
7458f0484fSRodney W. GrimesThe position indicator is maintained by subsequent reads, writes
75c6ff3a1bSSheldon Hearnand positioning requests.
76c6ff3a1bSSheldon HearnAll input occurs as if the characters
7758f0484fSRodney W. Grimeswere read by successive calls to the
7858f0484fSRodney W. Grimes.Xr fgetc 3
79a5ed710cSMike Pritchardfunction; all output takes place as if all characters were
80b8e5e42dSStephen McKaywritten by successive calls to the
8158f0484fSRodney W. Grimes.Xr fputc 3
8258f0484fSRodney W. Grimesfunction.
8358f0484fSRodney W. Grimes.Pp
8458f0484fSRodney W. GrimesA file is disassociated from a stream by
8558f0484fSRodney W. Grimes.Em closing
8658f0484fSRodney W. Grimesthe file.
87a5ed710cSMike PritchardOutput streams are flushed (any unwritten buffer contents are transferred
8858f0484fSRodney W. Grimesto the host environment) before the stream is disassociated from the file.
8958f0484fSRodney W. GrimesThe value of a pointer to a
9058f0484fSRodney W. Grimes.Dv FILE
91b8e5e42dSStephen McKayobject is indeterminate (garbage) after a file is closed.
9258f0484fSRodney W. Grimes.Pp
9358f0484fSRodney W. GrimesA file may be subsequently reopened, by the same or another program
9458f0484fSRodney W. Grimesexecution, and its contents reclaimed or modified (if it can be repositioned
9558f0484fSRodney W. Grimesat the start).  If the main function returns to its original caller, or
9658f0484fSRodney W. Grimesthe
9758f0484fSRodney W. Grimes.Xr exit 3
9858f0484fSRodney W. Grimesfunction is called, all open files are closed (hence all output
9958f0484fSRodney W. Grimesstreams are flushed) before program termination.  Other methods
100b8e5e42dSStephen McKayof program termination may not close files properly and hence
101b8e5e42dSStephen McKaybuffered output may be lost.  In particular,
102b8e5e42dSStephen McKay.Xr _exit 2
103b8e5e42dSStephen McKaydoes not flush stdio files.  Neither does an exit due to a signal.
104b8e5e42dSStephen McKayBuffers are flushed by
10558f0484fSRodney W. Grimes.Xr abort 3
106b8e5e42dSStephen McKayas required by POSIX, although previous implementations did not.
10758f0484fSRodney W. Grimes.Pp
108b8e5e42dSStephen McKayThis implementation makes no distinction between
10958f0484fSRodney W. Grimes.Dq text
11058f0484fSRodney W. Grimesand
11158f0484fSRodney W. Grimes.Dq binary
11258f0484fSRodney W. Grimesstreams.
11358f0484fSRodney W. GrimesIn effect, all streams are binary.
11458f0484fSRodney W. GrimesNo translation is performed and no extra padding appears on any stream.
11558f0484fSRodney W. Grimes.Pp
11658f0484fSRodney W. GrimesAt program startup, three streams are predefined and need not be
11758f0484fSRodney W. Grimesopened explicitly:
11858f0484fSRodney W. Grimes.Bl -bullet -compact -offset indent
11958f0484fSRodney W. Grimes.It
12058f0484fSRodney W. Grimes.Em standard input
12158f0484fSRodney W. Grimes(for reading conventional input),
12258f0484fSRodney W. Grimes.It
12358f0484fSRodney W. Grimes.Em standard output
124712dc76eSMike Pritchard(for writing conventional output), and
12558f0484fSRodney W. Grimes.It
12658f0484fSRodney W. Grimes.Em standard error
12758f0484fSRodney W. Grimes(for writing diagnostic output).
12858f0484fSRodney W. Grimes.El
12958f0484fSRodney W. GrimesThese streams are abbreviated
130ae828962SRuslan Ermilov.Dv stdin , stdout
13158f0484fSRodney W. Grimesand
132ae828962SRuslan Ermilov.Dv stderr .
13358f0484fSRodney W. GrimesInitially, the standard error stream
13458f0484fSRodney W. Grimesis unbuffered; the standard input and output streams are
13558f0484fSRodney W. Grimesfully buffered if and only if the streams do not refer to
13658f0484fSRodney W. Grimesan interactive or
13758f0484fSRodney W. Grimes.Dq terminal
13858f0484fSRodney W. Grimesdevice, as determined by the
13958f0484fSRodney W. Grimes.Xr isatty 3
14058f0484fSRodney W. Grimesfunction.
14158f0484fSRodney W. GrimesIn fact,
14258f0484fSRodney W. Grimes.Em all
14358f0484fSRodney W. Grimesfreshly-opened streams that refer to terminal devices
14458f0484fSRodney W. Grimesdefault to line buffering, and
14558f0484fSRodney W. Grimespending output to such streams is written automatically
146b8e5e42dSStephen McKaywhenever such an input stream is read.
14758f0484fSRodney W. GrimesNote that this applies only to
14858f0484fSRodney W. Grimes.Dq "true reads" ;
14958f0484fSRodney W. Grimesif the read request can be satisfied by existing buffered data,
15058f0484fSRodney W. Grimesno automatic flush will occur.
15158f0484fSRodney W. GrimesIn these cases,
15258f0484fSRodney W. Grimesor when a large amount of computation is done after printing
15358f0484fSRodney W. Grimespart of a line on an output terminal, it is necessary to
15458f0484fSRodney W. Grimes.Xr fflush 3
15558f0484fSRodney W. Grimesthe standard output before going off and computing so that the output
15658f0484fSRodney W. Grimeswill appear.
15758f0484fSRodney W. GrimesAlternatively, these defaults may be modified via the
15858f0484fSRodney W. Grimes.Xr setvbuf 3
15958f0484fSRodney W. Grimesfunction.
16058f0484fSRodney W. Grimes.Pp
16158f0484fSRodney W. GrimesThe
1623b8ecdbbSRuslan Ermilov.Nm
16358f0484fSRodney W. Grimeslibrary is a part of the library
164064f0074SMike Pritchard.Nm libc
165b8e5e42dSStephen McKayand routines are automatically loaded as needed by the C compiler.
16658f0484fSRodney W. GrimesThe
16758f0484fSRodney W. Grimes.Tn SYNOPSIS
16858f0484fSRodney W. Grimessections of the following manual pages indicate which include files
16958f0484fSRodney W. Grimesare to be used, what the compiler declaration for the function
17058f0484fSRodney W. Grimeslooks like and which external variables are of interest.
17158f0484fSRodney W. Grimes.Pp
17258f0484fSRodney W. GrimesThe following are defined as macros;
17358f0484fSRodney W. Grimesthese names may not be re-used
17458f0484fSRodney W. Grimeswithout first removing their current definitions with
175522ccf3fSRuslan Ermilov.Ic #undef :
17658f0484fSRodney W. Grimes.Dv BUFSIZ ,
17758f0484fSRodney W. Grimes.Dv EOF ,
17858f0484fSRodney W. Grimes.Dv FILENAME_MAX ,
17924653713SBruce Evans.Dv FOPEN_MAX ,
18058f0484fSRodney W. Grimes.Dv L_ctermid ,
181522ccf3fSRuslan Ermilov.Dv L_cuserid ,
18258f0484fSRodney W. Grimes.Dv L_tmpnam ,
18358f0484fSRodney W. Grimes.Dv NULL ,
184b8e5e42dSStephen McKay.Dv P_tmpdir ,
185b8e5e42dSStephen McKay.Dv SEEK_CUR ,
18658f0484fSRodney W. Grimes.Dv SEEK_END ,
18758f0484fSRodney W. Grimes.Dv SEEK_SET ,
18858f0484fSRodney W. Grimes.Dv TMP_MAX ,
1893236e08bSTim J. Robbins.Dv clearerr ,
19072c9dabbSTim J. Robbins.Dv clearerr_unlocked ,
1913236e08bSTim J. Robbins.Dv feof ,
19272c9dabbSTim J. Robbins.Dv feof_unlocked ,
1933236e08bSTim J. Robbins.Dv ferror ,
19472c9dabbSTim J. Robbins.Dv ferror_unlocked ,
1953236e08bSTim J. Robbins.Dv fileno ,
19672c9dabbSTim J. Robbins.Dv fileno_unlocked ,
197522ccf3fSRuslan Ermilov.Dv fropen ,
198522ccf3fSRuslan Ermilov.Dv fwopen ,
1993236e08bSTim J. Robbins.Dv getc ,
20072c9dabbSTim J. Robbins.Dv getc_unlocked ,
2013236e08bSTim J. Robbins.Dv getchar ,
20272c9dabbSTim J. Robbins.Dv getchar_unlocked ,
2033236e08bSTim J. Robbins.Dv putc ,
20472c9dabbSTim J. Robbins.Dv putc_unlocked ,
2053236e08bSTim J. Robbins.Dv putchar ,
20672c9dabbSTim J. Robbins.Dv putchar_unlocked ,
20758f0484fSRodney W. Grimes.Dv stderr ,
20872c9dabbSTim J. Robbins.Dv stdin
20958f0484fSRodney W. Grimesand
21072c9dabbSTim J. Robbins.Dv stdout .
21172c9dabbSTim J. RobbinsFunction versions of the macro functions
2123236e08bSTim J. Robbins.Dv clearerr ,
21372c9dabbSTim J. Robbins.Dv clearerr_unlocked ,
2143236e08bSTim J. Robbins.Dv feof ,
21572c9dabbSTim J. Robbins.Dv feof_unlocked ,
2163236e08bSTim J. Robbins.Dv ferror ,
21772c9dabbSTim J. Robbins.Dv ferror_unlocked ,
2183236e08bSTim J. Robbins.Dv fileno ,
21972c9dabbSTim J. Robbins.Dv fileno_unlocked ,
2203236e08bSTim J. Robbins.Dv getc ,
22172c9dabbSTim J. Robbins.Dv getc_unlocked ,
2223236e08bSTim J. Robbins.Dv getchar ,
22372c9dabbSTim J. Robbins.Dv getchar_unlocked ,
2243236e08bSTim J. Robbins.Dv putc ,
2253236e08bSTim J. Robbins.Dv putc_unlocked ,
2263236e08bSTim J. Robbins.Dv putchar ,
22772c9dabbSTim J. Robbinsand
228522ccf3fSRuslan Ermilov.Dv putchar_unlocked
229b8e5e42dSStephen McKayexist and will be used if the macro
23058f0484fSRodney W. Grimesdefinitions are explicitly removed.
23158f0484fSRodney W. Grimes.Sh SEE ALSO
23258f0484fSRodney W. Grimes.Xr close 2 ,
23375141cc9SWolfram Schneider.Xr open 2 ,
23458f0484fSRodney W. Grimes.Xr read 2 ,
23558f0484fSRodney W. Grimes.Xr write 2
23658f0484fSRodney W. Grimes.Sh BUGS
23758f0484fSRodney W. GrimesThe standard buffered functions do not interact well with certain other
23858f0484fSRodney W. Grimeslibrary and system functions, especially
239b8e5e42dSStephen McKay.Xr vfork 2 .
24058f0484fSRodney W. Grimes.Sh STANDARDS
24158f0484fSRodney W. GrimesThe
2423b8ecdbbSRuslan Ermilov.Nm
24358f0484fSRodney W. Grimeslibrary conforms to
244e74101e4STim J. Robbins.St -isoC-99 .
24558f0484fSRodney W. Grimes.Sh LIST OF FUNCTIONS
24658f0484fSRodney W. Grimes.Bl -column "Description"
24742635956SRuslan Ermilov.It Sy "Function	Description"
24842635956SRuslan Ermilov.It "asprintf	formatted output conversion"
24942635956SRuslan Ermilov.It "clearerr	check and reset stream status"
25042635956SRuslan Ermilov.It "fclose	close a stream"
25142635956SRuslan Ermilov.It "fdopen	stream open functions"
25242635956SRuslan Ermilov.It "feof	check and reset stream status"
25342635956SRuslan Ermilov.It "ferror	check and reset stream status"
25442635956SRuslan Ermilov.It "fflush	flush a stream"
25542635956SRuslan Ermilov.It "fgetc	get next character or word from input stream"
25642635956SRuslan Ermilov.It "fgetln	get a line from a stream"
25742635956SRuslan Ermilov.It "fgetpos	reposition a stream"
25842635956SRuslan Ermilov.It "fgets	get a line from a stream"
259e74101e4STim J. Robbins.It "fgetwc	get next wide character from input stream"
260e74101e4STim J. Robbins.It "fgetws	get a line of wide characters from a stream"
26142635956SRuslan Ermilov.It "fileno	check and reset stream status"
26242635956SRuslan Ermilov.It "fopen	stream open functions"
26342635956SRuslan Ermilov.It "fprintf	formatted output conversion"
26442635956SRuslan Ermilov.It "fpurge	flush a stream"
26542635956SRuslan Ermilov.It "fputc	output a character or word to a stream"
26642635956SRuslan Ermilov.It "fputs	output a line to a stream"
267e74101e4STim J. Robbins.It "fputwc	output a wide character to a stream"
268e74101e4STim J. Robbins.It "fputws	output a line of wide characters to a stream"
26942635956SRuslan Ermilov.It "fread	binary stream input/output"
27042635956SRuslan Ermilov.It "freopen	stream open functions"
27142635956SRuslan Ermilov.It "fropen	open a stream"
27242635956SRuslan Ermilov.It "fscanf	input format conversion"
27342635956SRuslan Ermilov.It "fseek	reposition a stream"
27442635956SRuslan Ermilov.It "fsetpos	reposition a stream"
27542635956SRuslan Ermilov.It "ftell	reposition a stream"
27642635956SRuslan Ermilov.It "funopen	open a stream"
277e74101e4STim J. Robbins.It "fwide	set/get orientation of stream"
27842635956SRuslan Ermilov.It "fwopen	open a stream"
279c5604d0aSTim J. Robbins.It "fwprintf	formatted wide character output conversion"
28042635956SRuslan Ermilov.It "fwrite	binary stream input/output"
28142635956SRuslan Ermilov.It "getc	get next character or word from input stream"
28242635956SRuslan Ermilov.It "getchar	get next character or word from input stream"
28342635956SRuslan Ermilov.It "gets	get a line from a stream"
28442635956SRuslan Ermilov.It "getw	get next character or word from input stream"
285e74101e4STim J. Robbins.It "getwc	get next wide character from input stream"
286e74101e4STim J. Robbins.It "getwchar	get next wide character from input stream"
2878f3e32c2SGregory Neil Shapiro.It "mkdtemp	create unique temporary directory"
28842635956SRuslan Ermilov.It "mkstemp	create unique temporary file"
28942635956SRuslan Ermilov.It "mktemp	create unique temporary file"
29042635956SRuslan Ermilov.It "perror	system error messages"
29142635956SRuslan Ermilov.It "printf	formatted output conversion"
29242635956SRuslan Ermilov.It "putc	output a character or word to a stream"
29342635956SRuslan Ermilov.It "putchar	output a character or word to a stream"
29442635956SRuslan Ermilov.It "puts	output a line to a stream"
29542635956SRuslan Ermilov.It "putw	output a character or word to a stream"
296e74101e4STim J. Robbins.It "putwc	output a wide character to a stream"
297e74101e4STim J. Robbins.It "putwchar	output a wide character to a stream"
29842635956SRuslan Ermilov.It "remove	remove directory entry"
29942635956SRuslan Ermilov.It "rewind	reposition a stream"
30042635956SRuslan Ermilov.It "scanf	input format conversion"
30142635956SRuslan Ermilov.It "setbuf	stream buffering operations"
30242635956SRuslan Ermilov.It "setbuffer	stream buffering operations"
30342635956SRuslan Ermilov.It "setlinebuf	stream buffering operations"
30442635956SRuslan Ermilov.It "setvbuf	stream buffering operations"
30542635956SRuslan Ermilov.It "snprintf	formatted output conversion"
30642635956SRuslan Ermilov.It "sprintf	formatted output conversion"
30742635956SRuslan Ermilov.It "sscanf	input format conversion"
30842635956SRuslan Ermilov.It "strerror	system error messages"
309c5604d0aSTim J. Robbins.It "swprintf	formatted wide character output conversion"
31042635956SRuslan Ermilov.It "sys_errlist	system error messages"
31142635956SRuslan Ermilov.It "sys_nerr	system error messages"
31242635956SRuslan Ermilov.It "tempnam	temporary file routines"
31342635956SRuslan Ermilov.It "tmpfile	temporary file routines"
31442635956SRuslan Ermilov.It "tmpnam	temporary file routines"
31542635956SRuslan Ermilov.It "ungetc	un-get character from input stream"
316e74101e4STim J. Robbins.It "ungetwc	un-get wide character from input stream"
31742635956SRuslan Ermilov.It "vasprintf	formatted output conversion"
31842635956SRuslan Ermilov.It "vfprintf	formatted output conversion"
31942635956SRuslan Ermilov.It "vfscanf	input format conversion"
320c5604d0aSTim J. Robbins.It "vfwprintf	formatted wide character output conversion"
32142635956SRuslan Ermilov.It "vprintf	formatted output conversion"
32242635956SRuslan Ermilov.It "vscanf	input format conversion"
32342635956SRuslan Ermilov.It "vsnprintf	formatted output conversion"
32442635956SRuslan Ermilov.It "vsprintf	formatted output conversion"
32542635956SRuslan Ermilov.It "vsscanf	input format conversion"
326c5604d0aSTim J. Robbins.It "vswprintf	formatted wide character output conversion"
327c5604d0aSTim J. Robbins.It "vwprintf	formatted wide character output conversion"
328c5604d0aSTim J. Robbins.It "wprintf	formatted wide character output conversion"
32958f0484fSRodney W. Grimes.El
330