Blame man3/setbuf.3

Packit 7cfc04
.\" Copyright (c) 1980, 1991 Regents of the University of California.
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This code is derived from software contributed to Berkeley by
Packit 7cfc04
.\" the American National Standards Committee X3, on Information
Packit 7cfc04
.\" Processing Systems.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"     @(#)setbuf.3	6.10 (Berkeley) 6/29/91
Packit 7cfc04
.\"
Packit 7cfc04
.\" Converted for Linux, Mon Nov 29 14:55:24 1993, faith@cs.unc.edu
Packit 7cfc04
.\" Added section to BUGS, Sun Mar 12 22:28:33 MET 1995,
Packit 7cfc04
.\"                   Thomas.Koenig@ciw.uni-karlsruhe.de
Packit 7cfc04
.\" Correction,  Sun, 11 Apr 1999 15:55:18,
Packit 7cfc04
.\"     Martin Vicente <martin@netadmin.dgac.fr>
Packit 7cfc04
.\" Correction,  2000-03-03, Andreas Jaeger <aj@suse.de>
Packit 7cfc04
.\" Added return value for setvbuf, aeb,
Packit 7cfc04
.\"
Packit 7cfc04
.TH SETBUF 3  2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
setbuf, setbuffer, setlinebuf, setvbuf \- stream buffering operations
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <stdio.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "void setbuf(FILE *" stream ", char *" buf );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "void setbuffer(FILE *" stream ", char *" buf ", size_t "  size );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "void setlinebuf(FILE *" stream );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int setvbuf(FILE *" stream ", char *" buf ", int " mode \
Packit 7cfc04
", size_t " size );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR setbuffer (),
Packit 7cfc04
.BR setlinebuf ():
Packit 7cfc04
    Since glibc 2.19:
Packit 7cfc04
        _DEFAULT_SOURCE
Packit 7cfc04
    Glibc 2.19 and earlier:
Packit 7cfc04
        _BSD_SOURCE
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The three types of buffering available are unbuffered, block buffered, and
Packit 7cfc04
line buffered.
Packit 7cfc04
When an output stream is unbuffered, information appears on
Packit 7cfc04
the destination file or terminal as soon as written; when it is block
Packit 7cfc04
buffered many characters are saved up and written as a block; when it is
Packit 7cfc04
line buffered characters are saved up until a newline is output or input is
Packit 7cfc04
read from any stream attached to a terminal device (typically \fIstdin\fP).
Packit 7cfc04
The function
Packit 7cfc04
.BR fflush (3)
Packit 7cfc04
may be used to force the block out early.
Packit 7cfc04
(See
Packit 7cfc04
.BR fclose (3).)
Packit 7cfc04
.PP
Packit 7cfc04
Normally all files are block buffered.
Packit 7cfc04
If a stream refers to a terminal (as
Packit 7cfc04
.I stdout
Packit 7cfc04
normally does), it is line buffered.
Packit 7cfc04
The standard error stream
Packit 7cfc04
.I stderr
Packit 7cfc04
is always unbuffered by default.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR setvbuf ()
Packit 7cfc04
function may be used on any open stream to change its buffer.
Packit 7cfc04
The
Packit 7cfc04
.I mode
Packit 7cfc04
argument must be one of the following three macros:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.B _IONBF
Packit 7cfc04
unbuffered
Packit 7cfc04
.TP
Packit 7cfc04
.B _IOLBF
Packit 7cfc04
line buffered
Packit 7cfc04
.TP
Packit 7cfc04
.B _IOFBF
Packit 7cfc04
fully buffered
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
Except for unbuffered files, the
Packit 7cfc04
.I buf
Packit 7cfc04
argument should point to a buffer at least
Packit 7cfc04
.I size
Packit 7cfc04
bytes long; this buffer will be used instead of the current buffer.
Packit 7cfc04
If the argument
Packit 7cfc04
.I buf
Packit 7cfc04
is NULL,
Packit 7cfc04
only the mode is affected; a new buffer will be allocated on the next read
Packit 7cfc04
or write operation.
Packit 7cfc04
The
Packit 7cfc04
.BR setvbuf ()
Packit 7cfc04
function may be used only after opening a stream and before any other
Packit 7cfc04
operations have been performed on it.
Packit 7cfc04
.PP
Packit 7cfc04
The other three calls are, in effect, simply aliases for calls to
Packit 7cfc04
.BR setvbuf ().
Packit 7cfc04
The
Packit 7cfc04
.BR setbuf ()
Packit 7cfc04
function is exactly equivalent to the call
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR setbuffer ()
Packit 7cfc04
function is the same, except that the size of the buffer is up to the
Packit 7cfc04
caller, rather than being determined by the default
Packit 7cfc04
.BR BUFSIZ .
Packit 7cfc04
The
Packit 7cfc04
.BR setlinebuf ()
Packit 7cfc04
function is exactly equivalent to the call:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
setvbuf(stream, NULL, _IOLBF, 0);
Packit 7cfc04
.in
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
The function
Packit 7cfc04
.BR setvbuf ()
Packit 7cfc04
returns 0 on success.
Packit 7cfc04
It returns nonzero on failure
Packit 7cfc04
.RI ( mode
Packit 7cfc04
is invalid or the request cannot be honored).
Packit 7cfc04
It may set
Packit 7cfc04
.I errno
Packit 7cfc04
on failure.
Packit 7cfc04
.PP
Packit 7cfc04
The other functions do not return a value.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw23 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR setbuf (),
Packit 7cfc04
.BR setbuffer (),
Packit 7cfc04
.br
Packit 7cfc04
.BR setlinebuf (),
Packit 7cfc04
.BR setvbuf ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
The
Packit 7cfc04
.BR setbuf ()
Packit 7cfc04
and
Packit 7cfc04
.BR setvbuf ()
Packit 7cfc04
functions conform to C89 and C99.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
.\" The
Packit 7cfc04
.\" .BR setbuffer ()
Packit 7cfc04
.\" and
Packit 7cfc04
.\" .BR setlinebuf ()
Packit 7cfc04
.\" functions are not portable to versions of BSD before 4.2BSD, and
Packit 7cfc04
.\" are available under Linux since libc 4.5.21.
Packit 7cfc04
.\" On 4.2BSD and 4.3BSD systems,
Packit 7cfc04
.\" .BR setbuf ()
Packit 7cfc04
.\" always uses a suboptimal buffer size and should be avoided.
Packit 7cfc04
.PP
Packit 7cfc04
You must make sure that the space that
Packit 7cfc04
.I buf
Packit 7cfc04
points to still exists by the time
Packit 7cfc04
.I stream
Packit 7cfc04
is closed, which also happens at program termination.
Packit 7cfc04
For example, the following is invalid:
Packit 7cfc04
.PP
Packit 7cfc04
.EX
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(void)
Packit 7cfc04
{
Packit 7cfc04
    char buf[BUFSIZ];
Packit 7cfc04
    setbuf(stdin, buf);
Packit 7cfc04
    printf("Hello, world!\\n");
Packit 7cfc04
    return 0;
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR stdbuf (1),
Packit 7cfc04
.BR fclose (3),
Packit 7cfc04
.BR fflush (3),
Packit 7cfc04
.BR fopen (3),
Packit 7cfc04
.BR fread (3),
Packit 7cfc04
.BR malloc (3),
Packit 7cfc04
.BR printf (3),
Packit 7cfc04
.BR puts (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.