1.\" $NetBSD: stdio.3,v 1.19 2003/08/07 16:43:32 agc 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.\" @(#)stdio.3 8.7 (Berkeley) 4/19/94 31.\" 32.Dd January 28, 2003 33.Dt STDIO 3 34.Os 35.Sh NAME 36.Nm stdio 37.Nd standard input/output library functions 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In stdio.h 42.Fd FILE *stdin; 43.Fd FILE *stdout; 44.Fd FILE *stderr; 45.Sh DESCRIPTION 46The standard 47.Tn I/O 48library provides a simple and efficient buffered stream 49.Tn I/O 50interface. 51Input and output is mapped into logical data streams 52and the physical 53.Tn I/O 54characteristics are concealed. 55The functions and macros are listed below; 56more information is available from the individual man pages. 57.Pp 58A stream is associated with an external file (which may be a physical 59device) by 60.Em opening 61a file, which may involve creating a new file. 62Creating an existing file causes its former contents to be discarded. 63If a file can support positioning requests (such as a disk file, as opposed 64to a terminal) then a 65.Em file position indicator 66associated with the stream is positioned at the start of the file (byte 67zero), unless the file is opened with append mode. 68If append mode 69is used, the position indicator will be placed the end-of-file. 70The position indicator is maintained by subsequent reads, writes 71and positioning requests. 72All input occurs as if the characters 73were read by successive calls to the 74.Xr fgetc 3 75function; all output takes place as if all characters were 76read by successive calls to the 77.Xr fputc 3 78function. 79.Pp 80A file is disassociated from a stream by 81.Em closing 82the file. 83Output streams are flushed (any unwritten buffer contents are transferred 84to the host environment) before the stream is disassociated from the file. 85The value of a pointer to a 86.Dv FILE 87object is indeterminate after a file is closed (garbage). 88.Pp 89A file may be subsequently reopened, by the same or another program 90execution, and its contents reclaimed or modified (if it can be repositioned 91at the start). 92If the main function returns to its original caller, or the 93.Xr exit 3 94function is called, all open files are closed (hence all output 95streams are flushed) before program termination. 96Other methods of program termination, such as 97.Xr abort 3 98do not bother about closing files properly. 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.Bl -bullet -compact -offset indent 112.It 113.Em standard input 114(for reading conventional input), 115.It 116.Em standard output 117(for writing conventional output), and 118.It 119.Em standard error 120(for writing diagnostic output). 121.El 122These streams are abbreviated 123.Em stdin , stdout 124and 125.Em stderr . 126Initially, the standard error stream 127is unbuffered; the standard input and output streams are 128fully buffered if and only if the streams do not refer to 129an interactive or 130.Dq terminal 131device, as determined by the 132.Xr isatty 3 133function. 134In fact, 135.Em all 136freshly-opened streams that refer to terminal devices 137default to line buffering, and 138pending output to such streams is written automatically 139whenever an such an input stream is read. 140Note that this applies only to 141.Dq "true reads" ; 142if the read request can be satisfied by existing buffered data, 143no automatic flush will occur. 144In these cases, 145or when a large amount of computation is done after printing 146part of a line on an output terminal, it is necessary to 147.Xr fflush 3 148the standard output before going off and computing so that the output 149will appear. 150Alternatively, these defaults may be modified via the 151.Xr setvbuf 3 152function. 153.Pp 154The 155.Nm 156library is a part of the library 157.Pa libc.a 158and routines are automatically loaded as needed by compilers such 159as 160.Xr cc 1 . 161The 162.Tn SYNOPSIS 163sections of the following manual pages indicate which include files 164are to be used, what the compiler declaration for the function 165looks like and which external variables are of interest. 166.Pp 167In multi-threaded applications, operations on streams perform implicit 168locking, except for the 169.Fa getc_unlocked , 170.Fa getchar_unlocked , 171.Fa putc_unlocked , 172and 173.Fa putchar_unlocked 174functions. 175Explicit control of stream locking is available through the 176.Fa flockfile , 177.Fa ftrylockfile , 178and 179.Fa funlockfile 180functions . 181.Pp 182The following are defined as macros; these names may not be re-used 183without first removing their current definitions with 184.Dv #undef : 185.Dv BUFSIZ , 186.Dv EOF , 187.Dv FILENAME_MAX , 188.Dv FOPEN_MAX , 189.Dv L_cuserid , 190.Dv L_ctermid , 191.Dv L_tmpnam , 192.Dv NULL , 193.Dv SEEK_END , 194.Dv SEEK_SET , 195.Dv SEE_CUR , 196.Dv TMP_MAX , 197.Fn clearerr , 198.Fn feof , 199.Fn ferror , 200.Fn fileno , 201.Fn freopen , 202.Fn fwopen , 203.Fn getc , 204.Fn getc_unlocked , 205.Fn getchar , 206.Fn getchar_unlocked , 207.Fn putc , 208.Fn putc_unlocked , 209.Fn putchar , 210.Fn putchar_unlocked , 211.Dv stderr , 212.Dv stdin , 213.Dv stdout . 214Function versions of the macro functions 215.Fn feof , 216.Fn ferror , 217.Fn clearerr , 218.Fn fileno , 219.Fn getc , 220.Fn getc_unlocked , 221.Fn getchar , 222.Fn getchar_unlocked , 223.Fn putc , 224.Fn putc_unlocked , 225.Fn putchar , 226and 227.Fn putchar_unlocked 228exist and will be used if the macros definitions are explicitly removed. 229.Sh SEE ALSO 230.Xr close 2 , 231.Xr open 2 , 232.Xr read 2 , 233.Xr write 2 234.Sh STANDARDS 235The 236.Nm 237library conforms to 238.St -ansiC . 239.Sh LIST OF FUNCTIONS 240.Bl -column "Functionxxxxxxxx" "Description" 241.Sy Function Description 242clearerr check and reset stream status 243fclose close a stream 244fdopen stream open functions 245feof check and reset stream status 246ferror check and reset stream status 247fflush flush a stream 248fgetc get next character or word from input stream 249fgetln get a line from a stream 250fgetpos reposition a stream 251fgets get a line from a stream 252fgetwc get next wide character from input stream 253fileno check and reset stream status 254flockfile lock a stream 255fopen stream open functions 256fprintf formatted output conversion 257fpurge flush a stream 258fputc output a character or word to a stream 259fputs output a line to a stream 260fputwc output a wide character to a stream 261fread binary stream input/output 262freopen stream open functions 263fropen open a stream 264fscanf input format conversion 265fseek reposition a stream 266fsetpos reposition a stream 267ftell reposition a stream 268ftrylockfile lock a stream (non-blocking) 269funlockfile unlock a stream 270funopen open a stream 271fwide set/get orientation of a stream 272fwopen open a stream 273fwrite binary stream input/output 274getc get next character or word from input stream 275getc_unlocked get next character or word from input stream 276 (no implicit locking) 277getchar get next character or word from input stream 278getchar_unlocked get next character or word from input stream 279 (no implicit locking) 280gets get a line from a stream 281getw get next character or word from input stream 282getwc get next wide character from input stream 283getwchar get next wide character from input stream 284mkstemp create unique temporary file 285mktemp create unique temporary file 286perror system error messages 287printf formatted output conversion 288putc output a character or word to a stream 289putc_unlocked output a character or word to a stream 290 (no implicit locking) 291putchar output a character or word to a stream 292putchar_unlocked output a character or word to a stream 293 (no implicit locking) 294puts output a line to a stream 295putw output a character or word to a stream 296putwc output a wide character to a stream 297putwchar output a wide character to a stream 298remove remove directory entry 299rewind reposition a stream 300scanf input format conversion 301setbuf stream buffering operations 302setbuffer stream buffering operations 303setlinebuf stream buffering operations 304setvbuf stream buffering operations 305snprintf formatted output conversion 306sprintf formatted output conversion 307sscanf input format conversion 308strerror system error messages 309sys_errlist system error messages 310sys_nerr system error messages 311tempnam temporary file routines 312tmpfile temporary file routines 313tmpnam temporary file routines 314ungetc un-get character from input stream 315ungetwc un-get wide character from input stream 316vfprintf formatted output conversion 317vfscanf input format conversion 318vprintf formatted output conversion 319vscanf input format conversion 320vsnprintf formatted output conversion 321vsprintf formatted output conversion 322vsscanf input format conversion 323.El 324.Sh BUGS 325The standard buffered functions do not interact well with certain other 326library and system functions, especially 327.Xr vfork 2 328and 329.Xr abort 3 . 330