xref: /freebsd/lib/libc/stdio/stdio.3 (revision 3b8ecdbbbec3a89d56cbdf4b23bb8ac3454065a8)
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.\"
3558f0484fSRodney W. Grimes.Dd April 19, 1994
3658f0484fSRodney W. Grimes.Dt STDIO 3
3758f0484fSRodney W. Grimes.Os BSD 4
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
4458f0484fSRodney W. Grimes.Fd #include <stdio.h>
4558f0484fSRodney W. Grimes.Fd FILE *stdin;
4658f0484fSRodney W. Grimes.Fd FILE *stdout;
4758f0484fSRodney W. Grimes.Fd 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
13058f0484fSRodney W. Grimes.Em stdin , stdout
13158f0484fSRodney W. Grimesand
13258f0484fSRodney W. Grimes.Em 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
17558f0484fSRodney W. Grimes.Dv #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_cuserid ,
18158f0484fSRodney W. Grimes.Dv L_ctermid ,
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 ,
18958f0484fSRodney W. Grimes.Dv clearerr ,
19058f0484fSRodney W. Grimes.Dv feof ,
19158f0484fSRodney W. Grimes.Dv ferror ,
19258f0484fSRodney W. Grimes.Dv fileno ,
193b8e5e42dSStephen McKay.Dv fropen ,
19458f0484fSRodney W. Grimes.Dv fwopen ,
19558f0484fSRodney W. Grimes.Dv getc ,
19658f0484fSRodney W. Grimes.Dv getchar ,
19758f0484fSRodney W. Grimes.Dv putc ,
19858f0484fSRodney W. Grimes.Dv putchar ,
19958f0484fSRodney W. Grimes.Dv stderr ,
20058f0484fSRodney W. Grimes.Dv stdin ,
201b8e5e42dSStephen McKay.Dv stdout ,
202b8e5e42dSStephen McKay.Dv vfscanf .
20358f0484fSRodney W. GrimesFunction versions of the macro functions
204b8e5e42dSStephen McKay.Fn clearerr ,
205064f0074SMike Pritchard.Fn feof ,
206064f0074SMike Pritchard.Fn ferror ,
207064f0074SMike Pritchard.Fn fileno ,
208064f0074SMike Pritchard.Fn getc ,
209064f0074SMike Pritchard.Fn getchar ,
210064f0074SMike Pritchard.Fn putc ,
21158f0484fSRodney W. Grimesand
212064f0074SMike Pritchard.Fn putchar
213b8e5e42dSStephen McKayexist and will be used if the macro
21458f0484fSRodney W. Grimesdefinitions are explicitly removed.
21558f0484fSRodney W. Grimes.Sh SEE ALSO
21658f0484fSRodney W. Grimes.Xr close 2 ,
21775141cc9SWolfram Schneider.Xr open 2 ,
21858f0484fSRodney W. Grimes.Xr read 2 ,
21958f0484fSRodney W. Grimes.Xr write 2
22058f0484fSRodney W. Grimes.Sh BUGS
22158f0484fSRodney W. GrimesThe standard buffered functions do not interact well with certain other
22258f0484fSRodney W. Grimeslibrary and system functions, especially
223b8e5e42dSStephen McKay.Xr vfork 2 .
22458f0484fSRodney W. Grimes.Sh STANDARDS
22558f0484fSRodney W. GrimesThe
2263b8ecdbbSRuslan Ermilov.Nm
22758f0484fSRodney W. Grimeslibrary conforms to
22858f0484fSRodney W. Grimes.St -ansiC .
22958f0484fSRodney W. Grimes.Sh LIST OF FUNCTIONS
23058f0484fSRodney W. Grimes.Bl -column "Description"
23158f0484fSRodney W. Grimes.Sy Function	Description
232b8e5e42dSStephen McKayasprintf	formatted output conversion
23358f0484fSRodney W. Grimesclearerr	check and reset stream status
23458f0484fSRodney W. Grimesfclose	close a stream
23558f0484fSRodney W. Grimesfdopen	stream open functions
23658f0484fSRodney W. Grimesfeof	check and reset stream status
23758f0484fSRodney W. Grimesferror	check and reset stream status
23858f0484fSRodney W. Grimesfflush	flush a stream
23958f0484fSRodney W. Grimesfgetc	get next character or word from input stream
240712dc76eSMike Pritchardfgetln	get a line from a stream
24158f0484fSRodney W. Grimesfgetpos	reposition a stream
24258f0484fSRodney W. Grimesfgets	get a line from a stream
24358f0484fSRodney W. Grimesfileno	check and reset stream status
24458f0484fSRodney W. Grimesfopen	stream open functions
24558f0484fSRodney W. Grimesfprintf	formatted output conversion
24658f0484fSRodney W. Grimesfpurge	flush a stream
24758f0484fSRodney W. Grimesfputc	output a character or word to a stream
24858f0484fSRodney W. Grimesfputs	output a line to a stream
24958f0484fSRodney W. Grimesfread	binary stream input/output
25058f0484fSRodney W. Grimesfreopen	stream open functions
25158f0484fSRodney W. Grimesfropen	open a stream
25258f0484fSRodney W. Grimesfscanf	input format conversion
25358f0484fSRodney W. Grimesfseek	reposition a stream
25458f0484fSRodney W. Grimesfsetpos	reposition a stream
25558f0484fSRodney W. Grimesftell	reposition a stream
25658f0484fSRodney W. Grimesfunopen	open a stream
25758f0484fSRodney W. Grimesfwopen	open a stream
25858f0484fSRodney W. Grimesfwrite	binary stream input/output
25958f0484fSRodney W. Grimesgetc	get next character or word from input stream
26058f0484fSRodney W. Grimesgetchar	get next character or word from input stream
26158f0484fSRodney W. Grimesgets	get a line from a stream
26258f0484fSRodney W. Grimesgetw	get next character or word from input stream
263b8e5e42dSStephen McKaymkdtemp	create unique temporary file
26458f0484fSRodney W. Grimesmkstemp	create unique temporary file
26558f0484fSRodney W. Grimesmktemp	create unique temporary file
26658f0484fSRodney W. Grimesperror	system error messages
26758f0484fSRodney W. Grimesprintf	formatted output conversion
26858f0484fSRodney W. Grimesputc	output a character or word to a stream
26958f0484fSRodney W. Grimesputchar	output a character or word to a stream
27058f0484fSRodney W. Grimesputs	output a line to a stream
27158f0484fSRodney W. Grimesputw	output a character or word to a stream
27258f0484fSRodney W. Grimesremove	remove directory entry
27358f0484fSRodney W. Grimesrewind	reposition a stream
27458f0484fSRodney W. Grimesscanf	input format conversion
27558f0484fSRodney W. Grimessetbuf	stream buffering operations
27658f0484fSRodney W. Grimessetbuffer	stream buffering operations
27758f0484fSRodney W. Grimessetlinebuf	stream buffering operations
27858f0484fSRodney W. Grimessetvbuf	stream buffering operations
27958f0484fSRodney W. Grimessnprintf	formatted output conversion
28058f0484fSRodney W. Grimessprintf	formatted output conversion
28158f0484fSRodney W. Grimessscanf	input format conversion
28258f0484fSRodney W. Grimesstrerror	system error messages
28358f0484fSRodney W. Grimessys_errlist	system error messages
28458f0484fSRodney W. Grimessys_nerr	system error messages
28558f0484fSRodney W. Grimestempnam	temporary file routines
28658f0484fSRodney W. Grimestmpfile	temporary file routines
28758f0484fSRodney W. Grimestmpnam	temporary file routines
28858f0484fSRodney W. Grimesungetc	un-get character from input stream
289b8e5e42dSStephen McKayvasprintf	formatted output conversion
29058f0484fSRodney W. Grimesvfprintf	formatted output conversion
29158f0484fSRodney W. Grimesvfscanf	input format conversion
29258f0484fSRodney W. Grimesvprintf	formatted output conversion
29358f0484fSRodney W. Grimesvscanf	input format conversion
29458f0484fSRodney W. Grimesvsnprintf	formatted output conversion
29558f0484fSRodney W. Grimesvsprintf	formatted output conversion
29658f0484fSRodney W. Grimesvsscanf	input format conversion
29758f0484fSRodney W. Grimes.El
298