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