1*61180Sbostic.\" Copyright (c) 1980, 1991, 1993 2*61180Sbostic.\" The Regents of the University of California. All rights reserved. 320409Smckusick.\" 450319Sbostic.\" This code is derived from software contributed to Berkeley by 550319Sbostic.\" the American National Standards Committee X3, on Information 650319Sbostic.\" Processing Systems. 750319Sbostic.\" 848351Scael.\" %sccs.include.redist.man% 920409Smckusick.\" 10*61180Sbostic.\" @(#)setbuf.3 8.1 (Berkeley) 06/04/93 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> 2356989Storek.Ft void 2448351Scael.Fn setbuf "FILE *stream" "char *buf" 2556989Storek.Ft void 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 . ) 4556989Storek.Pp 4620410SmckusickNormally all files are block buffered. 4748351ScaelWhen the first 4848351Scael.Tn I/O 4948351Scaeloperation occurs on a file, 5048351Scael.Xr malloc 3 5148351Scaelis called, 5256989Storekand an optimally-sized buffer is obtained. 5346102SbosticIf a stream refers to a terminal 5446102Sbostic(as 5548351Scael.Em stdout 5646102Sbosticnormally does) it is line buffered. 5748351ScaelThe standard error stream 5848351Scael.Em stderr 5920410Smckusickis always unbuffered. 6048351Scael.Pp 6148351ScaelThe 6248351Scael.Fn setvbuf 6348351Scaelfunction 6456989Storekmay be used to alter the buffering behavior of a stream. 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 7756989StorekThe 7856989Storek.Fa size 7956989Storekparameter may be given as zero 8056989Storekto obtain deferred optimal-size buffer allocation as usual. 8156989StorekIf it is not zero, 8256989Storekthen except for unbuffered files, the 8348351Scael.Fa buf 8446102Sbosticargument should point to a buffer at least 8548351Scael.Fa size 8646102Sbosticbytes long; 8746102Sbosticthis buffer will be used instead of the current buffer. 8856989Storek(If the 8956989Storek.Fa size 9056989Storekargument 9156989Storekis not zero but 9248351Scael.Fa buf 9356989Storekis 9456989Storek.Dv NULL , 9556989Storeka buffer of the given size will be allocated immediately, 9656989Storekand released on close. 9756989StorekThis is an extension to ANSI C; 9856989Storekportable code should use a size of 0 with any 9956989Storek.Dv NULL 10056989Storekbuffer.) 10156989Storek.Pp 10248351ScaelThe 10348351Scael.Fn setvbuf 10456989Storekfunction may be used at any time, 10556989Storekbut may have peculiar side effects 10656989Storek(such as discarding input or flushing output) 10756989Storekif the stream is ``active''. 10856989StorekPortable applications should call it only once on any given stream, 10956989Storekand before any 11056989Storek.Tn I/O 11156989Storekis performed. 11248351Scael.Pp 11356989StorekThe other three calls are, in effect, simply aliases for calls to 11448351Scael.Fn setvbuf . 11556989StorekExcept for the lack of a return value, the 11648351Scael.Fn setbuf 11756989Storekfunction is exactly equivalent to the call 11848351Scael.Pp 11956989Storek.Dl "setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);" 12048351Scael.Pp 12148351ScaelThe 12248351Scael.Fn setbuffer 12348351Scaelfunction 12446102Sbosticis the same, except that the size of the buffer is up to the caller, 12548351Scaelrather than being determined by the default 12648351Scael.Dv BUFSIZ . 12748351ScaelThe 12848351Scael.Fn setlinebuf 12948351Scaelfunction 13048351Scaelis exactly equivalent to the call: 13148351Scael.Pp 13256989Storek.Dl "setvbuf(stream, (char *)NULL, _IOLBF, 0);" 13356989Storek.Sh RETURN VALUES 13456989StorekThe 13556989Storek.Fn setvbuf 13656989Storekfunction returns 0 on success, or 13756989Storek.Dv EOF 13856989Storekif the request cannot be honored 13956989Storek(note that the stream is still functional in this case). 14056989Storek.Pp 14156989StorekThe 14256989Storek.Fn setlinebuf 14356989Storekfunction returns what the equivalent 14456989Storek.Fn setvbuf 14556989Storekwould have returned. 14648351Scael.Sh SEE ALSO 14748351Scael.Xr fopen 3 , 14848351Scael.Xr fclose 3 , 14948351Scael.Xr fread 3 , 15048351Scael.Xr malloc 3 , 15148351Scael.Xr puts 3 , 15248351Scael.Xr printf 3 15348351Scael.Sh STANDARDS 15448351ScaelThe 15548351Scael.Fn setbuf 15620410Smckusickand 15748351Scael.Fn setvbuf 15848351Scaelfunctions 15948351Scaelconform to 16048351Scael.St -ansiC . 16148351Scael.Sh BUGS 16220410SmckusickThe 16348351Scael.Fn setbuffer 16420410Smckusickand 16548351Scael.Fn setlinebuf 16648351Scaelfunctions are not portable to versions of 16748351Scael.Bx 16848351Scaelbefore 16948351Scael.Bx 4.2 . 17048351ScaelOn 17148351Scael.Bx 4.2 17248351Scaeland 17348351Scael.Bx 4.3 17448351Scaelsystems, 17548351Scael.Fn setbuf 17628038Skarelsalways uses a suboptimal buffer size and should be avoided. 177