1.\" $OpenBSD: setbuf.3,v 1.12 2007/05/31 19:19:31 jmc 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 $Mdocdate: May 31 2007 $ 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.Pp 92.Bl -tag -width _IOFBF -offset indent -compact 93.It Dv _IONBF 94unbuffered 95.It Dv _IOLBF 96line buffered 97.It Dv _IOFBF 98fully buffered 99.El 100.Pp 101The 102.Fa size 103parameter may be given as zero 104to obtain deferred optimal-size buffer allocation as usual. 105If it is not zero, then except for unbuffered files, the 106.Fa buf 107argument should point to a buffer at least 108.Fa size 109bytes long; 110this buffer will be used instead of the current buffer. 111(If the 112.Fa size 113argument 114is not zero but 115.Fa buf 116is 117.Dv NULL , 118a buffer of the given size will be allocated immediately, 119and released on close. 120This is an extension to ANSI C; 121portable code should use a size of 0 with any 122.Dv NULL 123buffer.) 124.Pp 125The 126.Fn setvbuf 127function may be used at any time, 128but may have peculiar side effects 129(such as discarding input or flushing output) 130if the stream is 131.Dq active . 132Portable applications should call it only once on any given stream, 133and before any 134.Tn I/O 135is performed. 136.Pp 137The other three calls are, in effect, simply aliases for calls to 138.Fn setvbuf . 139Except for the lack of a return value, the 140.Fn setbuf 141function is exactly equivalent to the call 142.Pp 143.Dl "setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);" 144.Pp 145The 146.Fn setbuffer 147function is the same, except that the size of the buffer is up to the caller, 148rather than being determined by the default 149.Dv BUFSIZ . 150The 151.Fn setlinebuf 152function is exactly equivalent to the call: 153.Pp 154.Dl "setvbuf(stream, NULL, _IOLBF, 0);" 155.Sh RETURN VALUES 156The 157.Fn setvbuf 158function returns 0 on success, or 159.Dv EOF 160if the request cannot be honored 161(note that the stream is still functional in this case). 162.Pp 163The 164.Fn setlinebuf 165function returns what the equivalent 166.Fn setvbuf 167would have returned. 168.Sh SEE ALSO 169.Xr fclose 3 , 170.Xr fopen 3 , 171.Xr fread 3 , 172.Xr malloc 3 , 173.Xr printf 3 , 174.Xr puts 3 175.Sh STANDARDS 176The 177.Fn setbuf 178and 179.Fn setvbuf 180functions conform to 181.St -ansiC . 182.Sh BUGS 183The 184.Fn setbuffer 185and 186.Fn setlinebuf 187functions are not portable to versions of 188.Bx 189before 190.Bx 4.2 . 191On 192.Bx 4.2 193and 194.Bx 4.3 195systems, 196.Fn setbuf 197always uses a suboptimal buffer size and should be avoided. 198