man-pages/man3/setbuf.3.html
2021-03-31 01:06:50 +01:00

291 lines
6.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SETBUF</TITLE>
</HEAD><BODY>
<H1>SETBUF</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>void setbuf(FILE *</B><I>stream</I><B>, char *</B><I>buf</I><B>);</B>
<B>void setbuffer(FILE *</B><I>stream</I><B>, char *</B><I>buf</I><B>, size_t </B><I>size</I><B>);</B>
<B>void setlinebuf(FILE *</B><I>stream</I><B>);</B>
<B>int setvbuf(FILE *</B><I>stream</I><B>, char *</B><I>buf</I><B>, int </B><I>mode</I><B>, size_t </B><I>size</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>setbuffer</B>(),
<B>setlinebuf</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BSD_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
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 <I>stdin</I>).
The function
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3)
may be used to force the block out early.
(See
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3).)
<P>
Normally all files are block buffered.
If a stream refers to a terminal (as
<I>stdout</I>
normally does), it is line buffered.
The standard error stream
<I>stderr</I>
is always unbuffered by default.
<P>
The
<B>setvbuf</B>()
function may be used on any open stream to change its buffer.
The
<I>mode</I>
argument must be one of the following three macros:
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2"><B>_IONBF</B>
<DD>
unbuffered
<DT id="3"><B>_IOLBF</B>
<DD>
line buffered
<DT id="4"><B>_IOFBF</B>
<DD>
fully buffered
</DL>
</DL>
<P>
Except for unbuffered files, the
<I>buf</I>
argument should point to a buffer at least
<I>size</I>
bytes long; this buffer will be used instead of the current buffer.
If the argument
<I>buf</I>
is NULL,
only the mode is affected; a new buffer will be allocated on the next read
or write operation.
The
<B>setvbuf</B>()
function may be used only after opening a stream and before any other
operations have been performed on it.
<P>
The other three calls are, in effect, simply aliases for calls to
<B>setvbuf</B>().
The
<B>setbuf</B>()
function is exactly equivalent to the call
<P>
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
<P>
The
<B>setbuffer</B>()
function is the same, except that the size of the buffer is up to the
caller, rather than being determined by the default
<B>BUFSIZ</B>.
The
<B>setlinebuf</B>()
function is exactly equivalent to the call:
<P>
setvbuf(stream, NULL, _IOLBF, 0);
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The function
<B>setvbuf</B>()
returns 0 on success.
It returns nonzero on failure
(<I>mode</I>
is invalid or the request cannot be honored).
It may set
<I>errno</I>
on failure.
<P>
The other functions do not return a value.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>setbuf</B>(),
<B>setbuffer</B>(),
<BR>
<B>setlinebuf</B>(),
<B>setvbuf</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
The
<B>setbuf</B>()
and
<B>setvbuf</B>()
functions conform to C89 and C99.
<A NAME="lbAH">&nbsp;</A>
<H2>BUGS</H2>
<P>
You must make sure that the space that
<I>buf</I>
points to still exists by the time
<I>stream</I>
is closed, which also happens at program termination.
For example, the following is invalid:
<P>
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[BUFSIZ];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setbuf(stdin,&nbsp;buf);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello,&nbsp;world!\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
}
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+stdbuf">stdbuf</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fread">fread</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+printf">printf</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+puts">puts</A></B>(3)
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="10"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAH">BUGS</A><DD>
<DT id="12"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="13"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:56 GMT, March 31, 2021
</BODY>
</HTML>