'\"macro stdmacro
.if n .pH g3.setbuffer @(#)setbuffer	40.8 of 9/18/89
.\" @(#)setbuffer.3s 1.5 88/12/15 SMI; from UCB 4.3 BSD and S5
.\" Copyright (c) 1988 Sun Microsystems, Inc. - All Rights Reserved.
.\"
.nr X
.if \nX=0 .ds x} setbuffer 3S "BSD Compatibility Package" "\&"
.if \nX=1 .ds x} setbuffer 3S "BSD Compatibility Package"
.if \nX=2 .ds x} setbuffer 3S "" "\&"
.if \nX=3 .ds x} setbuffer "" "" "\&"
.TH \*(x}
.SH NAME
\f4setbuffer\f1, \f4setlinebuf\f1 \- assign buffering to a stream
.SH SYNOPSIS
.nf
\f4cc \f1[ \f2flag\f1... ] \f2file\f1 ... \f4\-lucb\f1
.P
\f4#include <stdio.h>\f1
.P
\f4setbuffer(stream, buf, size)\f1
\f4FILE *stream;\f1
\f4char *buf;\f1
\f4int size;\f1
.P
\f4setlinebuf(stream)\f1
\f4FILE *stream;\f1
.fi
.SH DESCRIPTION
.P
The three types of buffering available are unbuffered, block buffered,
and line buffered.
When an output stream is unbuffered, information appears on the
destination file or terminal as soon as written;
when it is block buffered many characters are saved up and written as a block;
when it is line buffered characters are saved up until a
.SM NEWLINE
is encountered or input is read from any line buffered input stream.
\f4fflush\f1
(see
\f4fclose\f1(3S))
may be used to force the block out early.
Normally all files are block buffered.
A buffer is obtained from
\f4malloc\f1(3C)
upon the first
\f4getc\f1
or
\f4putc\f1(3S)
on the file.
.P
By default, output to a terminal is line buffered, except for output to the
standard stream
\f4stderr\f1
which is unbuffered, and all other input/output is fully buffered.
.P
\f4setbuffer\f1
can be used after a stream has been opened but before it is read or written.
It uses the character array
.I buf
whose size is determined by the
.I size
argument instead of an automatically allocated buffer.  If
.I buf
is the
\f4NULL\fP
pointer, input/output will be completely unbuffered.
A manifest constant
\f4BUFSIZ\*S\f1,
defined in the
\f4<stdio.h>\f1
header file,
tells how big an array is needed:
.IP
\f4char\f1
\f4buf[BUFSIZ];\f1
.P
\f4setlinebuf\f1
is used to change the buffering on a stream
from block buffered or unbuffered to line buffered.
Unlike
\f4setbuffer\f1,
it can be used at any time that the file descriptor is active.
.P
A file can be changed from unbuffered or line buffered to block buffered
by using
\f4freopen\f1
(see
\f4fopen\f1(3S)).
A file can be changed from block buffered or line buffered to unbuffered
by using
\f4freopen\f1
followed by
\f4setbuffer\f1
with a buffer argument of
\f4NULL\fP.
.SH "SEE ALSO"
\f4setbuf\fP(3S)
.P
\f4fclose\fP(3S),
\f4fopen\fP(3S),
\f4fread\fP(3S),
\f4getc\fP(3S),
\f4malloc\fP(3C),
\f4printf\fP(3S),
\f4putc\fP(3S),
\f4puts\fP(3S),
\f4setbuf\fP(3S)
in the \f2Programmer's Reference Manual\f1.
.SH NOTE
.P
A common source of error is allocating buffer space
as an automatic variable in a code block, and then
failing to close the stream in the same block.
