148351Scael.\" Copyright (c) 1980, 1991 Regents of the University of California. 248351Scael.\" All rights reserved. 320409Smckusick.\" 4*50319Sbostic.\" This code is derived from software contributed to Berkeley by 5*50319Sbostic.\" the American National Standards Committee X3, on Information 6*50319Sbostic.\" Processing Systems. 7*50319Sbostic.\" 848351Scael.\" %sccs.include.redist.man% 920409Smckusick.\" 10*50319Sbostic.\" @(#)setbuf.3 6.10 (Berkeley) 06/29/91 1148351Scael.\" 1248351Scael.Dd 1348351Scael.Dt SETBUF 3 1448351Scael.Os BSD 4 1548351Scael.Sh NAME 1648351Scael.Nm setbuf , 1748351Scael.Nm setbuffer , 1848351Scael.Nm setlinebuf , 1948351Scael.Nm setvbuf 2048351Scael.Nd stream buffering operations 2148351Scael.Sh SYNOPSIS 2248351Scael.Fd #include <stdio.h> 2348351Scael.Ft int 2448351Scael.Fn setbuf "FILE *stream" "char *buf" 2548351Scael.Ft int 2648351Scael.Fn setbuffer "FILE *stream" "char *buf" "size_t size" 2748351Scael.Ft int 2848351Scael.Fn setlinebuf "FILE *stream" 2948351Scael.Ft int 3048351Scael.Fn setvbuf "FILE *stream" "char *buf" "int mode" "size_t size" 3148351Scael.Sh DESCRIPTION 3220410SmckusickThe three types of buffering available are unbuffered, block buffered, 3320410Smckusickand line buffered. 3420410SmckusickWhen an output stream is unbuffered, information appears on the 3520410Smckusickdestination file or terminal as soon as written; 3620410Smckusickwhen it is block buffered many characters are saved up and written as a block; 3720410Smckusickwhen it is line buffered characters are saved up until a newline is 3846102Sbosticoutput or input is read from any stream attached to a terminal device 3946102Sbostic(typically stdin). 4048351ScaelThe function 4148351Scael.Xr fflush 3 4220410Smckusickmay be used to force the block out early. 4348351Scael(See 4448351Scael.Xr fclose 3 . ) 4520410SmckusickNormally all files are block buffered. 4648351ScaelWhen the first 4748351Scael.Tn I/O 4848351Scaeloperation occurs on a file, 4948351Scael.Xr malloc 3 5048351Scaelis called, 5148351Scaeland a buffer is obtained. 5246102SbosticIf a stream refers to a terminal 5346102Sbostic(as 5448351Scael.Em stdout 5546102Sbosticnormally does) it is line buffered. 5648351ScaelThe standard error stream 5748351Scael.Em stderr 5820410Smckusickis always unbuffered. 5948351Scael.Pp 6048351ScaelThe 6148351Scael.Fn setvbuf 6248351Scaelfunction 6346102Sbosticmay be used at any time on any open stream 6446102Sbosticto change its buffer. 6546102SbosticThe 6648351Scael.Fa mode 6746102Sbosticparameter must be one of the following three macros: 6848351Scael.Bl -tag -width _IOFBF -offset indent 6948351Scael.It Dv _IONBF 7046102Sbosticunbuffered 7148351Scael.It Dv _IOLBF 7246102Sbosticline buffered 7348351Scael.It Dv _IOFBF 7446102Sbosticfully buffered 7548351Scael.El 7648351Scael.Pp 7746102SbosticExcept for unbuffered files, the 7848351Scael.Fa buf 7946102Sbosticargument should point to a buffer at least 8048351Scael.Fa size 8146102Sbosticbytes long; 8246102Sbosticthis buffer will be used instead of the current buffer. 8348351ScaelIf the argument 8448351Scael.Fa buf 8546102Sbosticis NULL, 8646102Sbosticonly the mode is affected; 8746102Sbostica new buffer will be allocated on the next read or write operation. 8848351ScaelThe 8948351Scael.Fn setvbuf 9048351Scaelfunction 9146102Sbosticmay be used at any time, 9246102Sbosticbut can only change the mode of a stream 9346102Sbosticwhen it is not ``active'': 9448351Scaelthat is, before any 9548351Scael.Tn I/O , 9646102Sbosticor immediately after a call to 9748351Scael.Xr fflush . 9848351Scael.Pp 9946102SbosticThe other three calls are, in effect, simply aliases 10046102Sbosticfor calls to 10148351Scael.Fn setvbuf . 10248351ScaelThe 10348351Scael.Fn setbuf 10448351Scaelfunction 10546102Sbosticis exactly equivalent to the call 10648351Scael.Pp 10748351Scael.Dl setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ); 10848351Scael.Pp 10948351ScaelThe 11048351Scael.Fn setbuffer 11148351Scaelfunction 11246102Sbosticis the same, except that the size of the buffer is up to the caller, 11348351Scaelrather than being determined by the default 11448351Scael.Dv BUFSIZ . 11548351ScaelThe 11648351Scael.Fn setlinebuf 11748351Scaelfunction 11848351Scaelis exactly equivalent to the call: 11948351Scael.Pp 12048351Scael.Dl setvbuf(stream, (char *)NULL, _IOLBF, 0); 12148351Scael.Sh SEE ALSO 12248351Scael.Xr fopen 3 , 12348351Scael.Xr fclose 3 , 12448351Scael.Xr fread 3 , 12548351Scael.Xr malloc 3 , 12648351Scael.Xr puts 3 , 12748351Scael.Xr printf 3 12848351Scael.Sh STANDARDS 12948351ScaelThe 13048351Scael.Fn setbuf 13120410Smckusickand 13248351Scael.Fn setvbuf 13348351Scaelfunctions 13448351Scaelconform to 13548351Scael.St -ansiC . 13648351Scael.Sh BUGS 13720410SmckusickThe 13848351Scael.Fn setbuffer 13920410Smckusickand 14048351Scael.Fn setlinebuf 14148351Scaelfunctions are not portable to versions of 14248351Scael.Bx 14348351Scaelbefore 14448351Scael.Bx 4.2 . 14548351ScaelOn 14648351Scael.Bx 4.2 14748351Scaeland 14848351Scael.Bx 4.3 14948351Scaelsystems, 15048351Scael.Fn setbuf 15128038Skarelsalways uses a suboptimal buffer size and should be avoided. 152