1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd March 3, 2009 29.Dt STDIO 3 30.Os 31.Sh NAME 32.Nm stdio 33.Nd standard input/output library functions 34.Sh LIBRARY 35.Lb libc 36.Sh SYNOPSIS 37.In stdio.h 38.Vt FILE *stdin ; 39.Vt FILE *stdout ; 40.Vt FILE *stderr ; 41.Sh DESCRIPTION 42The standard 43.Tn I/O 44library provides a simple and efficient buffered stream 45.Tn I/O 46interface. 47Input and output is mapped into logical data streams 48and the physical 49.Tn I/O 50characteristics are concealed. 51The functions and macros are listed 52below; more information is available from the individual man pages. 53.Pp 54A stream is associated with an external file (which may be a physical 55device) by 56.Em opening 57a file, which may involve creating a new file. 58Creating an 59existing file causes its former contents to be discarded. 60If a file can support positioning requests (such as a disk file, as opposed 61to a terminal) then a 62.Em file position indicator 63associated with the stream is positioned at the start of the file (byte 64zero), unless the file is opened with append mode. 65If append mode 66is used, the position indicator will be placed at the end-of-file. 67The position indicator is maintained by subsequent reads, writes 68and positioning requests. 69All input occurs as if the characters 70were read by successive calls to the 71.Xr fgetc 3 72function; all output takes place as if all characters were 73written by successive calls to the 74.Xr fputc 3 75function. 76.Pp 77A file is disassociated from a stream by 78.Em closing 79the file. 80Output streams are flushed (any unwritten buffer contents are transferred 81to the host environment) before the stream is disassociated from the file. 82The value of a pointer to a 83.Dv FILE 84object is indeterminate (garbage) after a file is closed. 85.Pp 86A file may be subsequently reopened, by the same or another program 87execution, and its contents reclaimed or modified (if it can be repositioned 88at the start). 89If the main function returns to its original caller, or 90the 91.Xr exit 3 92function is called, all open files are closed (hence all output 93streams are flushed) before program termination. 94Other methods 95of program termination may not close files properly and hence 96buffered output may be lost. 97In particular, 98.Xr _exit 2 99does not flush stdio files. 100Neither does an exit due to a signal. 101Buffers are flushed by 102.Xr abort 3 103as required by POSIX, although previous implementations did not. 104.Pp 105This implementation makes no distinction between 106.Dq text 107and 108.Dq binary 109streams. 110In effect, all streams are binary. 111No translation is performed and no extra padding appears on any stream. 112.Pp 113At program startup, three streams are predefined and need not be 114opened explicitly: 115.Bl -bullet -compact -offset indent 116.It 117.Em standard input 118(for reading conventional input), 119.It 120.Em standard output 121(for writing conventional output), and 122.It 123.Em standard error 124(for writing diagnostic output). 125.El 126These streams are abbreviated 127.Dv stdin , stdout 128and 129.Dv stderr . 130Initially, the standard error stream 131is unbuffered; the standard input and output streams are 132fully buffered if and only if the streams do not refer to 133an interactive or 134.Dq terminal 135device, as determined by the 136.Xr isatty 3 137function. 138In fact, 139.Em all 140freshly-opened streams that refer to terminal devices 141default to line buffering, and 142pending output to such streams is written automatically 143whenever such an input stream is read. 144Note that this applies only to 145.Dq "true reads" ; 146if the read request can be satisfied by existing buffered data, 147no automatic flush will occur. 148In these cases, 149or when a large amount of computation is done after printing 150part of a line on an output terminal, it is necessary to 151.Xr fflush 3 152the standard output before going off and computing so that the output 153will appear. 154Alternatively, these defaults may be modified via the 155.Xr setvbuf 3 156function. 157.Pp 158The 159.Nm 160library is a part of the library 161.Nm libc 162and routines are automatically loaded as needed by the C compiler. 163The 164.Tn SYNOPSIS 165sections of the following manual pages indicate which include files 166are to be used, what the compiler declaration for the function 167looks like and which external variables are of interest. 168.Pp 169The following are defined as macros; 170these names may not be re-used 171without first removing their current definitions with 172.Ic #undef : 173.Dv BUFSIZ , 174.Dv EOF , 175.Dv FILENAME_MAX , 176.Dv FOPEN_MAX , 177.Dv L_ctermid , 178.Dv L_cuserid , 179.Dv L_tmpnam , 180.Dv NULL , 181.Dv P_tmpdir , 182.Dv SEEK_CUR , 183.Dv SEEK_END , 184.Dv SEEK_SET , 185.Dv TMP_MAX , 186.Dv clearerr , 187.Dv clearerr_unlocked , 188.Dv feof , 189.Dv feof_unlocked , 190.Dv ferror , 191.Dv ferror_unlocked , 192.Dv fileno , 193.Dv fileno_unlocked , 194.Dv fropen , 195.Dv fwopen , 196.Dv getc , 197.Dv getc_unlocked , 198.Dv getchar , 199.Dv getchar_unlocked , 200.Dv putc , 201.Dv putc_unlocked , 202.Dv putchar , 203.Dv putchar_unlocked , 204.Dv stderr , 205.Dv stdin 206and 207.Dv stdout . 208Function versions of the macro functions 209.Dv clearerr , 210.Dv clearerr_unlocked , 211.Dv feof , 212.Dv feof_unlocked , 213.Dv ferror , 214.Dv ferror_unlocked , 215.Dv fileno , 216.Dv fileno_unlocked , 217.Dv getc , 218.Dv getc_unlocked , 219.Dv getchar , 220.Dv getchar_unlocked , 221.Dv putc , 222.Dv putc_unlocked , 223.Dv putchar , 224and 225.Dv putchar_unlocked 226exist and will be used if the macro 227definitions are explicitly removed. 228.Sh SEE ALSO 229.Xr close 2 , 230.Xr open 2 , 231.Xr read 2 , 232.Xr write 2 233.Sh STANDARDS 234The 235.Nm 236library conforms to 237.St -isoC-99 . 238.Sh LIST OF FUNCTIONS 239.Bl -column "Description" 240.It Sy "Function Description" 241.It "asprintf formatted output conversion" 242.It "clearerr check and reset stream status" 243.It "dprintf formatted output conversion" 244.It "fclose close a stream" 245.It "fdopen stream open functions" 246.It "feof check and reset stream status" 247.It "ferror check and reset stream status" 248.It "fflush flush a stream" 249.It "fgetc get next character or word from input stream" 250.It "fgetln get a line from a stream" 251.It "fgetpos reposition a stream" 252.It "fgets get a line from a stream" 253.It "fgetwc get next wide character from input stream" 254.It "fgetws get a line of wide characters from a stream" 255.It "fileno check and reset stream status" 256.It "fopen stream open functions" 257.It "fprintf formatted output conversion" 258.It "fpurge flush a stream" 259.It "fputc output a character or word to a stream" 260.It "fputs output a line to a stream" 261.It "fputwc output a wide character to a stream" 262.It "fputws output a line of wide characters to a stream" 263.It "fread binary stream input/output" 264.It "freopen stream open functions" 265.It "fropen open a stream" 266.It "fscanf input format conversion" 267.It "fseek reposition a stream" 268.It "fsetpos reposition a stream" 269.It "ftell reposition a stream" 270.It "funopen open a stream" 271.It "fwide set/get orientation of stream" 272.It "fwopen open a stream" 273.It "fwprintf formatted wide character output conversion" 274.It "fwrite binary stream input/output" 275.It "getc get next character or word from input stream" 276.It "getchar get next character or word from input stream" 277.It "getdelim get a line from a stream" 278.It "getline get a line from a stream" 279.It "getw get next character or word from input stream" 280.It "getwc get next wide character from input stream" 281.It "getwchar get next wide character from input stream" 282.It "mkdtemp create unique temporary directory" 283.It "mkstemp create unique temporary file" 284.It "mktemp create unique temporary file" 285.It "perror system error messages" 286.It "printf formatted output conversion" 287.It "putc output a character or word to a stream" 288.It "putchar output a character or word to a stream" 289.It "puts output a line to a stream" 290.It "putw output a character or word to a stream" 291.It "putwc output a wide character to a stream" 292.It "putwchar output a wide character to a stream" 293.It "remove remove directory entry" 294.It "rewind reposition a stream" 295.It "scanf input format conversion" 296.It "setbuf stream buffering operations" 297.It "setbuffer stream buffering operations" 298.It "setlinebuf stream buffering operations" 299.It "setvbuf stream buffering operations" 300.It "snprintf formatted output conversion" 301.It "sprintf formatted output conversion" 302.It "sscanf input format conversion" 303.It "strerror system error messages" 304.It "swprintf formatted wide character output conversion" 305.It "sys_errlist system error messages" 306.It "sys_nerr system error messages" 307.It "tempnam temporary file routines" 308.It "tmpfile temporary file routines" 309.It "tmpnam temporary file routines" 310.It "ungetc un-get character from input stream" 311.It "ungetwc un-get wide character from input stream" 312.It "vasprintf formatted output conversion" 313.It "vdprintf formatted output conversion" 314.It "vfprintf formatted output conversion" 315.It "vfscanf input format conversion" 316.It "vfwprintf formatted wide character output conversion" 317.It "vprintf formatted output conversion" 318.It "vscanf input format conversion" 319.It "vsnprintf formatted output conversion" 320.It "vsprintf formatted output conversion" 321.It "vsscanf input format conversion" 322.It "vswprintf formatted wide character output conversion" 323.It "vwprintf formatted wide character output conversion" 324.It "wprintf formatted wide character output conversion" 325.El 326.Sh BUGS 327The standard buffered functions do not interact well with certain other 328library and system functions, especially 329.Xr vfork 2 . 330