4.3BSD/usr/man/man3/setbuf.3s

.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\"
.\"	@(#)setbuf.3s	6.2 (Berkeley) 5/12/86
.\"
.TH SETBUF 3S  "May 12, 1986"
.UC 4
.SH NAME
setbuf, setbuffer, setlinebuf \- assign buffering to a stream
.SH SYNOPSIS
.B #include <stdio.h>
.PP
.B setbuf(stream, buf)
.br
.SM
.B FILE
.B *stream;
.br
.B char *buf;
.PP
.B setbuffer(stream, buf, size)
.br
.SM
.B FILE
.B *stream;
.br
.B char *buf;
.br
.B int size;
.PP
.B setlinebuf(stream)
.br
.SM
.B FILE
.B *stream;
.SH DESCRIPTION
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 newline is
encountered or input is read from stdin.
.I Fflush
(see 
.IR fclose (3S))
may be used to force the block out early.
Normally all files are block buffered.
A buffer is obtained from
.IR  malloc (3)
upon the first
.I getc
or
.IR  putc (3S)
on the file.
If the standard stream
.B stdout
refers to a terminal it is line buffered.
The standard stream
.B stderr
is always unbuffered.
.PP
.I Setbuf
is used after a stream has been opened but before it is read or written.
The character array
.I buf
is used instead of an automatically allocated buffer.  If
.I buf
is the constant pointer
.SM
.B NULL,
input/output will be completely unbuffered.
A manifest constant 
.SM
.B BUFSIZ
tells how big an array is needed:
.IP
.B char
buf[BUFSIZ];
.PP
.IR Setbuffer ,
an alternate form of 
.IR setbuf ,
is used after a stream has been opened but before it is read or written.
The character array
.I buf
whose size is determined by the 
.I size
argument is used instead of an automatically allocated buffer.  If
.I buf
is the constant pointer
.SM
.BR NULL ,
input/output will be completely unbuffered.
.PP
.I Setlinebuf
is used to change
.I stdout
or
.I stderr
from block buffered or unbuffered to line buffered.
Unlike
.I setbuf
and
.I setbuffer
it can be used at any time that the file descriptor is active.
.PP
A file can be changed from unbuffered or line buffered to block buffered
by using
.I freopen
(see
.IR fopen (3S)).
A file can be changed from block buffered or line buffered to unbuffered
by using
.I freopen
followed by
.I setbuf
with a buffer argument of 
.SM
.BR NULL .
.SH "SEE ALSO"
fopen(3S),
getc(3S),
putc(3S),
malloc(3),
fclose(3S),
puts(3S),
printf(3S),
fread(3S)
.SH BUGS
The standard error stream should be line buffered by default.
.LP
The
.I setbuffer
and
.I setlinebuf
functions are not portable to non-4.2BSD versions of UNIX.
On 4.2BSD and 4.3BSD systems,
.I setbuf
always uses a suboptimal buffer size and should be avoided.
.I Setbuffer
is not usually needed
as the default file I/O buffer sizes are optimal.