xref: /original-bsd/lib/libc/stdio/setbuf.3 (revision 62cd422e)
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.3 6.6 (Berkeley) 01/20/91

SETBUF 3
C 4
NAME
setbuf, setbuffer, setlinebuf, setvbuf - assign buffering to a stream
SYNOPSIS
#include <stdio.h>

int
setbuf(FILE *stream, char *buf);

int
setbuffer(FILE *stream, char *buf, size_t size);

int
setlinebuf(FILE *stream);

int
setvbuf(FILE *stream, char *buf, int mode, size_t size);
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 output or input is read from any stream attached to a terminal device (typically stdin). Fflush (see fclose (3)) may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from malloc (3) upon the first read or write operation on the file. If a stream refers to a terminal (as stdout normally does) it is line buffered. The standard stream stderr is always unbuffered.

Setvbuf may be used at any time on any open stream to change its buffer. The mode parameter must be one of the following three macros:

8 _IONBF unbuffered

.ns

8 _IOLBF line buffered

.ns

8 _IOFBF fully buffered

Except for unbuffered files, the buf argument should point to a buffer at least size bytes long; this buffer will be used instead of the current buffer. If buf is NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation. Setvbuf may be used at any time, but can only change the mode of a stream when it is not ``active'': that is, before any I/O, or immediately after a call to fflush .

The other three calls are, in effect, simply aliases for calls to setvbuf . Setbuf is exactly equivalent to the call setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ); Setbuffer is the same, except that the size of the buffer is up to the caller, rather than being determined by the manifest constant BUFSIZ . Setlinebuf is exactly equivalent to the call setvbuf(stream, (char *)NULL, _IOLBF, 0);

"SEE ALSO"
fopen(3), fclose(3), fread(3), malloc(3), puts(3), printf(3)
STANDARDS
Setbuf and setvbuf onform to ANSI X3.159-1989 (``ANSI C'').
BUGS
The setbuffer and setlinebuf functions are not portable to non-4.2BSD versions of UNIX. On 4.2BSD and 4.3BSD systems, setbuf always uses a suboptimal buffer size and should be avoided.