1.\" $OpenBSD: setbuf.3,v 1.10 2003/06/02 20:18:37 millert Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the American National Standards Committee X3, on Information 8.\" Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.Dd June 4, 1993 35.Dt SETBUF 3 36.Os 37.Sh NAME 38.Nm setbuf , 39.Nm setbuffer , 40.Nm setlinebuf , 41.Nm setvbuf 42.Nd stream buffering operations 43.Sh SYNOPSIS 44.Fd #include <stdio.h> 45.Ft void 46.Fn setbuf "FILE *stream" "char *buf" 47.Ft void 48.Fn setbuffer "FILE *stream" "char *buf" "size_t size" 49.Ft int 50.Fn setlinebuf "FILE *stream" 51.Ft int 52.Fn setvbuf "FILE *stream" "char *buf" "int mode" "size_t size" 53.Sh DESCRIPTION 54The three types of stream buffering available are unbuffered, block buffered, 55and line buffered. 56When an output stream is unbuffered, information appears on the 57destination file or terminal as soon as written; 58when it is block buffered, many characters are saved up and written as a block; 59when line buffered, characters are saved up until a newline 60.Pq Ql \en 61is output or input is read from any stream attached to a terminal device 62(typically 63.Em stdin ) . 64.Pp 65The 66.Xr fflush 3 67function may be used to force the block out early. 68.Pp 69Normally, all files are block buffered. 70When the first 71.Tn I/O 72operation occurs on a file, 73.Xr malloc 3 74is called, 75and an optimally sized buffer is obtained. 76If a stream refers to a terminal 77(as 78.Em stdout 79normally does), it is line buffered. 80.Pp 81The standard error stream 82.Em stderr 83is initially unbuffered. 84.Pp 85The 86.Fn setvbuf 87function may be used to alter the buffering behavior of a stream. 88The 89.Fa mode 90parameter must be one of the following three macros: 91.Bl -tag -width _IOFBF -offset indent 92.It Dv _IONBF 93unbuffered 94.It Dv _IOLBF 95line buffered 96.It Dv _IOFBF 97fully buffered 98.El 99.Pp 100The 101.Fa size 102parameter may be given as zero 103to obtain deferred optimal-size buffer allocation as usual. 104If it is not zero, then except for unbuffered files, the 105.Fa buf 106argument should point to a buffer at least 107.Fa size 108bytes long; 109this buffer will be used instead of the current buffer. 110(If the 111.Fa size 112argument 113is not zero but 114.Fa buf 115is 116.Dv NULL , 117a buffer of the given size will be allocated immediately, 118and released on close. 119This is an extension to ANSI C; 120portable code should use a size of 0 with any 121.Dv NULL 122buffer.) 123.Pp 124The 125.Fn setvbuf 126function may be used at any time, 127but may have peculiar side effects 128(such as discarding input or flushing output) 129if the stream is 130.Dq active . 131Portable applications should call it only once on any given stream, 132and before any 133.Tn I/O 134is performed. 135.Pp 136The other three calls are, in effect, simply aliases for calls to 137.Fn setvbuf . 138Except for the lack of a return value, the 139.Fn setbuf 140function is exactly equivalent to the call 141.Pp 142.Dl "setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);" 143.Pp 144The 145.Fn setbuffer 146function is the same, except that the size of the buffer is up to the caller, 147rather than being determined by the default 148.Dv BUFSIZ . 149The 150.Fn setlinebuf 151function is exactly equivalent to the call: 152.Pp 153.Dl "setvbuf(stream, NULL, _IOLBF, 0);" 154.Sh RETURN VALUES 155The 156.Fn setvbuf 157function returns 0 on success, or 158.Dv EOF 159if the request cannot be honored 160(note that the stream is still functional in this case). 161.Pp 162The 163.Fn setlinebuf 164function returns what the equivalent 165.Fn setvbuf 166would have returned. 167.Sh SEE ALSO 168.Xr fclose 3 , 169.Xr fopen 3 , 170.Xr fread 3 , 171.Xr malloc 3 , 172.Xr printf 3 , 173.Xr puts 3 174.Sh STANDARDS 175The 176.Fn setbuf 177and 178.Fn setvbuf 179functions conform to 180.St -ansiC . 181.Sh BUGS 182The 183.Fn setbuffer 184and 185.Fn setlinebuf 186functions are not portable to versions of 187.Bx 188before 189.Bx 4.2 . 190On 191.Bx 4.2 192and 193.Bx 4.3 194systems, 195.Fn setbuf 196always uses a suboptimal buffer size and should be avoided. 197