xref: /freebsd/lib/libc/stdio/stdio.3 (revision 064f007493a990a098f3f0f5ae66732c5747c837)
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
3358f0484fSRodney W. Grimes.\"
3458f0484fSRodney W. Grimes.Dd April 19, 1994
3558f0484fSRodney W. Grimes.Dt STDIO 3
3658f0484fSRodney W. Grimes.Os BSD 4
3758f0484fSRodney W. Grimes.Sh NAME
3858f0484fSRodney W. Grimes.Nm stdio
3958f0484fSRodney W. Grimes.Nd standard input/output library functions
4058f0484fSRodney W. Grimes.Sh SYNOPSIS
4158f0484fSRodney W. Grimes.Fd #include <stdio.h>
4258f0484fSRodney W. Grimes.Fd FILE *stdin;
4358f0484fSRodney W. Grimes.Fd FILE *stdout;
4458f0484fSRodney W. Grimes.Fd FILE *stderr;
4558f0484fSRodney W. Grimes.Sh DESCRIPTION
4658f0484fSRodney W. GrimesThe standard
4758f0484fSRodney W. Grimes.Tn I/O
4858f0484fSRodney W. Grimeslibrary provides a simple and efficient buffered stream
4958f0484fSRodney W. Grimes.Tn I/O
5058f0484fSRodney W. Grimesinterface.
51a5ed710cSMike PritchardInput and output is mapped into logical data streams
5258f0484fSRodney W. Grimesand the physical
5358f0484fSRodney W. Grimes.Tn I/O
5458f0484fSRodney W. Grimescharacteristics are concealed. The functions and macros are listed
5558f0484fSRodney W. Grimesbelow; more information is available from the individual man pages.
5658f0484fSRodney W. Grimes.Pp
5758f0484fSRodney W. GrimesA stream is associated with an external file (which may be a physical
5858f0484fSRodney W. Grimesdevice) by
5958f0484fSRodney W. Grimes.Em opening
6058f0484fSRodney W. Grimesa file, which may involve creating a new file. Creating an
6158f0484fSRodney W. Grimesexisting file causes its former contents to be discarded.
6258f0484fSRodney W. GrimesIf a file can support positioning requests (such as a disk file, as opposed
6358f0484fSRodney W. Grimesto a terminal) then a
6458f0484fSRodney W. Grimes.Em file position indicator
6558f0484fSRodney W. Grimesassociated with the stream is positioned at the start of the file (byte
6658f0484fSRodney W. Grimeszero), unless the file is opened with appended mode. If append mode
6758f0484fSRodney W. Grimesis used, the position indicator will be placed the end-of-file.
6858f0484fSRodney W. GrimesThe position indicator is maintained by subsequent reads, writes
6958f0484fSRodney W. Grimesand positioning requests. All input occurs as if the characters
7058f0484fSRodney W. Grimeswere read by successive calls to the
7158f0484fSRodney W. Grimes.Xr fgetc 3
72a5ed710cSMike Pritchardfunction; all output takes place as if all characters were
7358f0484fSRodney W. Grimesread by successive calls to the
7458f0484fSRodney W. Grimes.Xr fputc 3
7558f0484fSRodney W. Grimesfunction.
7658f0484fSRodney W. Grimes.Pp
7758f0484fSRodney W. GrimesA file is disassociated from a stream by
7858f0484fSRodney W. Grimes.Em closing
7958f0484fSRodney W. Grimesthe file.
80a5ed710cSMike PritchardOutput streams are flushed (any unwritten buffer contents are transferred
8158f0484fSRodney W. Grimesto the host environment) before the stream is disassociated from the file.
8258f0484fSRodney W. GrimesThe value of a pointer to a
8358f0484fSRodney W. Grimes.Dv FILE
8458f0484fSRodney W. Grimesobject is indeterminate after a file is closed (garbage).
8558f0484fSRodney W. Grimes.Pp
8658f0484fSRodney W. GrimesA file may be subsequently reopened, by the same or another program
8758f0484fSRodney W. Grimesexecution, and its contents reclaimed or modified (if it can be repositioned
8858f0484fSRodney W. Grimesat the start).  If the main function returns to its original caller, or
8958f0484fSRodney W. Grimesthe
9058f0484fSRodney W. Grimes.Xr exit 3
9158f0484fSRodney W. Grimesfunction is called, all open files are closed (hence all output
9258f0484fSRodney W. Grimesstreams are flushed) before program termination.  Other methods
9358f0484fSRodney W. Grimesof program termination, such as
9458f0484fSRodney W. Grimes.Xr abort 3
9558f0484fSRodney W. Grimesdo not bother about closing files properly.
9658f0484fSRodney W. Grimes.Pp
9758f0484fSRodney W. GrimesThis implementation needs and makes
9858f0484fSRodney W. Grimesno distinction between
9958f0484fSRodney W. Grimes.Dq text
10058f0484fSRodney W. Grimesand
10158f0484fSRodney W. Grimes.Dq binary
10258f0484fSRodney W. Grimesstreams.
10358f0484fSRodney W. GrimesIn effect, all streams are binary.
10458f0484fSRodney W. GrimesNo translation is performed and no extra padding appears on any stream.
10558f0484fSRodney W. Grimes.Pp
10658f0484fSRodney W. GrimesAt program startup, three streams are predefined and need not be
10758f0484fSRodney W. Grimesopened explicitly:
10858f0484fSRodney W. Grimes.Bl -bullet -compact -offset indent
10958f0484fSRodney W. Grimes.It
11058f0484fSRodney W. Grimes.Em standard input
11158f0484fSRodney W. Grimes(for reading conventional input),
11258f0484fSRodney W. Grimes.It
11358f0484fSRodney W. Grimes.Em standard output
11458f0484fSRodney W. Grimes(for writing conventional input), and
11558f0484fSRodney W. Grimes.It
11658f0484fSRodney W. Grimes.Em standard error
11758f0484fSRodney W. Grimes(for writing diagnostic output).
11858f0484fSRodney W. Grimes.El
11958f0484fSRodney W. GrimesThese streams are abbreviated
12058f0484fSRodney W. Grimes.Em stdin , stdout
12158f0484fSRodney W. Grimesand
12258f0484fSRodney W. Grimes.Em stderr .
12358f0484fSRodney W. GrimesInitially, the standard error stream
12458f0484fSRodney W. Grimesis unbuffered; the standard input and output streams are
12558f0484fSRodney W. Grimesfully buffered if and only if the streams do not refer to
12658f0484fSRodney W. Grimesan interactive or
12758f0484fSRodney W. Grimes.Dq terminal
12858f0484fSRodney W. Grimesdevice, as determined by the
12958f0484fSRodney W. Grimes.Xr isatty 3
13058f0484fSRodney W. Grimesfunction.
13158f0484fSRodney W. GrimesIn fact,
13258f0484fSRodney W. Grimes.Em all
13358f0484fSRodney W. Grimesfreshly-opened streams that refer to terminal devices
13458f0484fSRodney W. Grimesdefault to line buffering, and
13558f0484fSRodney W. Grimespending output to such streams is written automatically
13658f0484fSRodney W. Grimeswhenever an such an input stream is read.
13758f0484fSRodney W. GrimesNote that this applies only to
13858f0484fSRodney W. Grimes.Dq "true reads" ;
13958f0484fSRodney W. Grimesif the read request can be satisfied by existing buffered data,
14058f0484fSRodney W. Grimesno automatic flush will occur.
14158f0484fSRodney W. GrimesIn these cases,
14258f0484fSRodney W. Grimesor when a large amount of computation is done after printing
14358f0484fSRodney W. Grimespart of a line on an output terminal, it is necessary to
14458f0484fSRodney W. Grimes.Xr fflush 3
14558f0484fSRodney W. Grimesthe standard output before going off and computing so that the output
14658f0484fSRodney W. Grimeswill appear.
14758f0484fSRodney W. GrimesAlternatively, these defaults may be modified via the
14858f0484fSRodney W. Grimes.Xr setvbuf 3
14958f0484fSRodney W. Grimesfunction.
15058f0484fSRodney W. Grimes.Pp
15158f0484fSRodney W. GrimesThe
15258f0484fSRodney W. Grimes.Nm stdio
15358f0484fSRodney W. Grimeslibrary is a part of the library
154064f0074SMike Pritchard.Nm libc
15558f0484fSRodney W. Grimesand routines are automatically loaded as needed by the compilers
15658f0484fSRodney W. Grimes.Xr cc 1
15758f0484fSRodney W. Grimesand
15858f0484fSRodney W. Grimes.Xr pc 1 .
15958f0484fSRodney W. GrimesThe
16058f0484fSRodney W. Grimes.Tn SYNOPSIS
16158f0484fSRodney W. Grimessections of the following manual pages indicate which include files
16258f0484fSRodney W. Grimesare to be used, what the compiler declaration for the function
16358f0484fSRodney W. Grimeslooks like and which external variables are of interest.
16458f0484fSRodney W. Grimes.Pp
16558f0484fSRodney W. GrimesThe following are defined as macros;
16658f0484fSRodney W. Grimesthese names may not be re-used
16758f0484fSRodney W. Grimeswithout first removing their current definitions with
16858f0484fSRodney W. Grimes.Dv #undef :
16958f0484fSRodney W. Grimes.Dv BUFSIZ ,
17058f0484fSRodney W. Grimes.Dv EOF ,
17158f0484fSRodney W. Grimes.Dv FILENAME_MAX ,
17258f0484fSRodney W. Grimes.DV FOPEN_MAX ,
17358f0484fSRodney W. Grimes.Dv L_cuserid ,
17458f0484fSRodney W. Grimes.Dv L_ctermid ,
17558f0484fSRodney W. Grimes.Dv L_tmpnam,
17658f0484fSRodney W. Grimes.Dv NULL ,
17758f0484fSRodney W. Grimes.Dv SEEK_END ,
17858f0484fSRodney W. Grimes.Dv SEEK_SET ,
17958f0484fSRodney W. Grimes.Dv SEE_CUR ,
18058f0484fSRodney W. Grimes.Dv TMP_MAX ,
18158f0484fSRodney W. Grimes.Dv clearerr ,
18258f0484fSRodney W. Grimes.Dv feof ,
18358f0484fSRodney W. Grimes.Dv ferror ,
18458f0484fSRodney W. Grimes.Dv fileno ,
18558f0484fSRodney W. Grimes.Dv freopen ,
18658f0484fSRodney W. Grimes.Dv fwopen ,
18758f0484fSRodney W. Grimes.Dv getc ,
18858f0484fSRodney W. Grimes.Dv getchar ,
18958f0484fSRodney W. Grimes.Dv putc ,
19058f0484fSRodney W. Grimes.Dv putchar ,
19158f0484fSRodney W. Grimes.Dv stderr ,
19258f0484fSRodney W. Grimes.Dv stdin ,
19358f0484fSRodney W. Grimes.Dv stdout .
19458f0484fSRodney W. GrimesFunction versions of the macro functions
195064f0074SMike Pritchard.Fn feof ,
196064f0074SMike Pritchard.Fn ferror ,
197064f0074SMike Pritchard.Fn clearerr ,
198064f0074SMike Pritchard.Fn fileno ,
199064f0074SMike Pritchard.Fn getc ,
200064f0074SMike Pritchard.Fn getchar ,
201064f0074SMike Pritchard.Fn putc ,
20258f0484fSRodney W. Grimesand
203064f0074SMike Pritchard.Fn putchar
20458f0484fSRodney W. Grimesexist and will be used if the macros
20558f0484fSRodney W. Grimesdefinitions are explicitly removed.
20658f0484fSRodney W. Grimes.Sh SEE ALSO
20758f0484fSRodney W. Grimes.Xr open 2 ,
20858f0484fSRodney W. Grimes.Xr close 2 ,
20958f0484fSRodney W. Grimes.Xr read 2 ,
21058f0484fSRodney W. Grimes.Xr write 2
21158f0484fSRodney W. Grimes.Sh BUGS
21258f0484fSRodney W. GrimesThe standard buffered functions do not interact well with certain other
21358f0484fSRodney W. Grimeslibrary and system functions, especially
214064f0074SMike Pritchard.Xr vfork 2
21558f0484fSRodney W. Grimesand
216064f0074SMike Pritchard.Xr abort 3 .
21758f0484fSRodney W. Grimes.Sh STANDARDS
21858f0484fSRodney W. GrimesThe
21958f0484fSRodney W. Grimes.Nm stdio
22058f0484fSRodney W. Grimeslibrary conforms to
22158f0484fSRodney W. Grimes.St -ansiC .
22258f0484fSRodney W. Grimes.Sh LIST OF FUNCTIONS
22358f0484fSRodney W. Grimes.Bl -column "Description"
22458f0484fSRodney W. Grimes.Sy Function	Description
22558f0484fSRodney W. Grimesclearerr	check and reset stream status
22658f0484fSRodney W. Grimesfclose	close a stream
22758f0484fSRodney W. Grimesfdopen	stream open functions
22858f0484fSRodney W. Grimesfeof	check and reset stream status
22958f0484fSRodney W. Grimesferror	check and reset stream status
23058f0484fSRodney W. Grimesfflush	flush a stream
23158f0484fSRodney W. Grimesfgetc	get next character or word from input stream
23258f0484fSRodney W. Grimesfgetline	get a line from a stream
23358f0484fSRodney W. Grimesfgetpos	reposition a stream
23458f0484fSRodney W. Grimesfgets	get a line from a stream
23558f0484fSRodney W. Grimesfileno	check and reset stream status
23658f0484fSRodney W. Grimesfopen	stream open functions
23758f0484fSRodney W. Grimesfprintf	formatted output conversion
23858f0484fSRodney W. Grimesfpurge	flush a stream
23958f0484fSRodney W. Grimesfputc	output a character or word to a stream
24058f0484fSRodney W. Grimesfputs	output a line to a stream
24158f0484fSRodney W. Grimesfread	binary stream input/output
24258f0484fSRodney W. Grimesfreopen	stream open functions
24358f0484fSRodney W. Grimesfropen	open a stream
24458f0484fSRodney W. Grimesfscanf	input format conversion
24558f0484fSRodney W. Grimesfseek	reposition a stream
24658f0484fSRodney W. Grimesfsetpos	reposition a stream
24758f0484fSRodney W. Grimesftell	reposition a stream
24858f0484fSRodney W. Grimesfunopen	open a stream
24958f0484fSRodney W. Grimesfwopen	open a stream
25058f0484fSRodney W. Grimesfwrite	binary stream input/output
25158f0484fSRodney W. Grimesgetc	get next character or word from input stream
25258f0484fSRodney W. Grimesgetchar	get next character or word from input stream
25358f0484fSRodney W. Grimesgets	get a line from a stream
25458f0484fSRodney W. Grimesgetw	get next character or word from input stream
25558f0484fSRodney W. Grimesmkstemp	create unique temporary file
25658f0484fSRodney W. Grimesmktemp	create unique temporary file
25758f0484fSRodney W. Grimesperror	system error messages
25858f0484fSRodney W. Grimesprintf	formatted output conversion
25958f0484fSRodney W. Grimesputc	output a character or word to a stream
26058f0484fSRodney W. Grimesputchar	output a character or word to a stream
26158f0484fSRodney W. Grimesputs	output a line to a stream
26258f0484fSRodney W. Grimesputw	output a character or word to a stream
26358f0484fSRodney W. Grimesremove	remove directory entry
26458f0484fSRodney W. Grimesrewind	reposition a stream
26558f0484fSRodney W. Grimesscanf	input format conversion
26658f0484fSRodney W. Grimessetbuf	stream buffering operations
26758f0484fSRodney W. Grimessetbuffer	stream buffering operations
26858f0484fSRodney W. Grimessetlinebuf	stream buffering operations
26958f0484fSRodney W. Grimessetvbuf	stream buffering operations
27058f0484fSRodney W. Grimessnprintf	formatted output conversion
27158f0484fSRodney W. Grimessprintf	formatted output conversion
27258f0484fSRodney W. Grimessscanf	input format conversion
27358f0484fSRodney W. Grimesstrerror	system error messages
27458f0484fSRodney W. Grimessys_errlist	system error messages
27558f0484fSRodney W. Grimessys_nerr	system error messages
27658f0484fSRodney W. Grimestempnam	temporary file routines
27758f0484fSRodney W. Grimestmpfile	temporary file routines
27858f0484fSRodney W. Grimestmpnam	temporary file routines
27958f0484fSRodney W. Grimesungetc	un-get character from input stream
28058f0484fSRodney W. Grimesvfprintf	formatted output conversion
28158f0484fSRodney W. Grimesvfscanf	input format conversion
28258f0484fSRodney W. Grimesvprintf	formatted output conversion
28358f0484fSRodney W. Grimesvscanf	input format conversion
28458f0484fSRodney W. Grimesvsnprintf	formatted output conversion
28558f0484fSRodney W. Grimesvsprintf	formatted output conversion
28658f0484fSRodney W. Grimesvsscanf	input format conversion
28758f0484fSRodney W. Grimes.El
288