xref: /freebsd/lib/libc/stdio/setbuf.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1980, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
558f0484fSRodney W. Grimes.\" the American National Standards Committee X3, on Information
658f0484fSRodney W. Grimes.\" Processing Systems.
758f0484fSRodney W. Grimes.\"
858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes.\" are met:
1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
32*e0f7c06dSGordon Bergling.Dd May 1, 2020
3358f0484fSRodney W. Grimes.Dt SETBUF 3
34a307d598SRuslan Ermilov.Os
3558f0484fSRodney W. Grimes.Sh NAME
3658f0484fSRodney W. Grimes.Nm setbuf ,
3758f0484fSRodney W. Grimes.Nm setbuffer ,
3858f0484fSRodney W. Grimes.Nm setlinebuf ,
3958f0484fSRodney W. Grimes.Nm setvbuf
4058f0484fSRodney W. Grimes.Nd stream buffering operations
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4432eef9aeSRuslan Ermilov.In stdio.h
4558f0484fSRodney W. Grimes.Ft void
463248d0a5SRobert Drehmel.Fn setbuf "FILE * restrict stream" "char * restrict buf"
4758f0484fSRodney W. Grimes.Ft void
4823f0c1fcSBruce Evans.Fn setbuffer "FILE *stream" "char *buf" "int size"
4958f0484fSRodney W. Grimes.Ft int
5058f0484fSRodney W. Grimes.Fn setlinebuf "FILE *stream"
5158f0484fSRodney W. Grimes.Ft int
523248d0a5SRobert Drehmel.Fn setvbuf "FILE * restrict stream" "char * restrict buf" "int mode" "size_t size"
5358f0484fSRodney W. Grimes.Sh DESCRIPTION
5458f0484fSRodney W. GrimesThe three types of buffering available are unbuffered, block buffered,
5558f0484fSRodney W. Grimesand line buffered.
5658f0484fSRodney W. GrimesWhen an output stream is unbuffered, information appears on the
5758f0484fSRodney W. Grimesdestination file or terminal as soon as written;
5858f0484fSRodney W. Grimeswhen it is block buffered many characters are saved up and written as a block;
5958f0484fSRodney W. Grimeswhen it is line buffered characters are saved up until a newline is
6058f0484fSRodney W. Grimesoutput or input is read from any stream attached to a terminal device
61ae828962SRuslan Ermilov(typically
62ae828962SRuslan Ermilov.Dv stdin ) .
6358f0484fSRodney W. GrimesThe function
6458f0484fSRodney W. Grimes.Xr fflush 3
6558f0484fSRodney W. Grimesmay be used to force the block out early.
6658f0484fSRodney W. Grimes(See
6758f0484fSRodney W. Grimes.Xr fclose 3 . )
6858f0484fSRodney W. Grimes.Pp
6958f0484fSRodney W. GrimesNormally all files are block buffered.
7058f0484fSRodney W. GrimesWhen the first
7158f0484fSRodney W. Grimes.Tn I/O
7258f0484fSRodney W. Grimesoperation occurs on a file,
7358f0484fSRodney W. Grimes.Xr malloc 3
7458f0484fSRodney W. Grimesis called,
7558f0484fSRodney W. Grimesand an optimally-sized buffer is obtained.
7658f0484fSRodney W. GrimesIf a stream refers to a terminal
7758f0484fSRodney W. Grimes(as
78ae828962SRuslan Ermilov.Dv stdout
7958f0484fSRodney W. Grimesnormally does) it is line buffered.
8058f0484fSRodney W. GrimesThe standard error stream
81ae828962SRuslan Ermilov.Dv stderr
8258f0484fSRodney W. Grimesis always unbuffered.
835a239e70SJeremie Le HenNote that these defaults may be altered using the
846486b015SJeremie Le Hen.Xr stdbuf 1
856486b015SJeremie Le Henutility.
8658f0484fSRodney W. Grimes.Pp
8758f0484fSRodney W. GrimesThe
8858f0484fSRodney W. Grimes.Fn setvbuf
8958f0484fSRodney W. Grimesfunction
9058f0484fSRodney W. Grimesmay be used to alter the buffering behavior of a stream.
9158f0484fSRodney W. GrimesThe
9258f0484fSRodney W. Grimes.Fa mode
932efeeba5SRuslan Ermilovargument must be one of the following three macros:
9458f0484fSRodney W. Grimes.Bl -tag -width _IOFBF -offset indent
9558f0484fSRodney W. Grimes.It Dv _IONBF
9658f0484fSRodney W. Grimesunbuffered
9758f0484fSRodney W. Grimes.It Dv _IOLBF
9858f0484fSRodney W. Grimesline buffered
9958f0484fSRodney W. Grimes.It Dv _IOFBF
10058f0484fSRodney W. Grimesfully buffered
10158f0484fSRodney W. Grimes.El
10258f0484fSRodney W. Grimes.Pp
10358f0484fSRodney W. GrimesThe
10458f0484fSRodney W. Grimes.Fa size
1052efeeba5SRuslan Ermilovargument may be given as zero
10658f0484fSRodney W. Grimesto obtain deferred optimal-size buffer allocation as usual.
10758f0484fSRodney W. GrimesIf it is not zero,
10858f0484fSRodney W. Grimesthen except for unbuffered files, the
10958f0484fSRodney W. Grimes.Fa buf
11058f0484fSRodney W. Grimesargument should point to a buffer at least
11158f0484fSRodney W. Grimes.Fa size
11258f0484fSRodney W. Grimesbytes long;
11358f0484fSRodney W. Grimesthis buffer will be used instead of the current buffer.
1143b28ee84SAlfred PerlsteinIf
1153b28ee84SAlfred Perlstein.Fa buf
1162d82ac31SRuslan Ermilovis not
1172d82ac31SRuslan Ermilov.Dv NULL ,
1182d82ac31SRuslan Ermilovit is the caller's responsibility to
1193b28ee84SAlfred Perlstein.Xr free 3
1203b28ee84SAlfred Perlsteinthis buffer after closing the stream.
12158f0484fSRodney W. Grimes(If the
12258f0484fSRodney W. Grimes.Fa size
12358f0484fSRodney W. Grimesargument
12458f0484fSRodney W. Grimesis not zero but
12558f0484fSRodney W. Grimes.Fa buf
12658f0484fSRodney W. Grimesis
12758f0484fSRodney W. Grimes.Dv NULL ,
12858f0484fSRodney W. Grimesa buffer of the given size will be allocated immediately,
12958f0484fSRodney W. Grimesand released on close.
13058f0484fSRodney W. GrimesThis is an extension to ANSI C;
13158f0484fSRodney W. Grimesportable code should use a size of 0 with any
13258f0484fSRodney W. Grimes.Dv NULL
13358f0484fSRodney W. Grimesbuffer.)
13458f0484fSRodney W. Grimes.Pp
13558f0484fSRodney W. GrimesThe
13658f0484fSRodney W. Grimes.Fn setvbuf
13758f0484fSRodney W. Grimesfunction may be used at any time,
13858f0484fSRodney W. Grimesbut may have peculiar side effects
13958f0484fSRodney W. Grimes(such as discarding input or flushing output)
14058f0484fSRodney W. Grimesif the stream is ``active''.
14158f0484fSRodney W. GrimesPortable applications should call it only once on any given stream,
14258f0484fSRodney W. Grimesand before any
14358f0484fSRodney W. Grimes.Tn I/O
14458f0484fSRodney W. Grimesis performed.
14558f0484fSRodney W. Grimes.Pp
14658f0484fSRodney W. GrimesThe other three calls are, in effect, simply aliases for calls to
14758f0484fSRodney W. Grimes.Fn setvbuf .
14858f0484fSRodney W. GrimesExcept for the lack of a return value, the
14958f0484fSRodney W. Grimes.Fn setbuf
15058f0484fSRodney W. Grimesfunction is exactly equivalent to the call
15158f0484fSRodney W. Grimes.Pp
15258f0484fSRodney W. Grimes.Dl "setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);"
15358f0484fSRodney W. Grimes.Pp
15458f0484fSRodney W. GrimesThe
15558f0484fSRodney W. Grimes.Fn setbuffer
15658f0484fSRodney W. Grimesfunction
15758f0484fSRodney W. Grimesis the same, except that the size of the buffer is up to the caller,
15858f0484fSRodney W. Grimesrather than being determined by the default
15958f0484fSRodney W. Grimes.Dv BUFSIZ .
16058f0484fSRodney W. GrimesThe
16158f0484fSRodney W. Grimes.Fn setlinebuf
16258f0484fSRodney W. Grimesfunction
16358f0484fSRodney W. Grimesis exactly equivalent to the call:
16458f0484fSRodney W. Grimes.Pp
16558f0484fSRodney W. Grimes.Dl "setvbuf(stream, (char *)NULL, _IOLBF, 0);"
16658f0484fSRodney W. Grimes.Sh RETURN VALUES
16758f0484fSRodney W. GrimesThe
16858f0484fSRodney W. Grimes.Fn setvbuf
16958f0484fSRodney W. Grimesfunction returns 0 on success, or
17058f0484fSRodney W. Grimes.Dv EOF
17158f0484fSRodney W. Grimesif the request cannot be honored
17258f0484fSRodney W. Grimes(note that the stream is still functional in this case).
17358f0484fSRodney W. Grimes.Pp
17458f0484fSRodney W. GrimesThe
17558f0484fSRodney W. Grimes.Fn setlinebuf
17658f0484fSRodney W. Grimesfunction returns what the equivalent
17758f0484fSRodney W. Grimes.Fn setvbuf
17858f0484fSRodney W. Grimeswould have returned.
17958f0484fSRodney W. Grimes.Sh SEE ALSO
1806486b015SJeremie Le Hen.Xr stdbuf 1 ,
18158f0484fSRodney W. Grimes.Xr fclose 3 ,
18275141cc9SWolfram Schneider.Xr fopen 3 ,
18358f0484fSRodney W. Grimes.Xr fread 3 ,
18458f0484fSRodney W. Grimes.Xr malloc 3 ,
18575141cc9SWolfram Schneider.Xr printf 3 ,
18675141cc9SWolfram Schneider.Xr puts 3
18758f0484fSRodney W. Grimes.Sh STANDARDS
18858f0484fSRodney W. GrimesThe
18958f0484fSRodney W. Grimes.Fn setbuf
19058f0484fSRodney W. Grimesand
19158f0484fSRodney W. Grimes.Fn setvbuf
19258f0484fSRodney W. Grimesfunctions
19358f0484fSRodney W. Grimesconform to
194588a200cSRuslan Ermilov.St -isoC .
195*e0f7c06dSGordon Bergling.Sh HISTORY
196*e0f7c06dSGordon BerglingThe
197*e0f7c06dSGordon Bergling.Fn setbuf
198*e0f7c06dSGordon Berglingfunction first appeared in
199*e0f7c06dSGordon Bergling.At v7 .
200*e0f7c06dSGordon BerglingThe
201*e0f7c06dSGordon Bergling.Fn setbuffer
202*e0f7c06dSGordon Berglingfunction first appeared in
203*e0f7c06dSGordon Bergling.Bx 4.1c .
204*e0f7c06dSGordon BerglingThe
205*e0f7c06dSGordon Bergling.Fn setlinebuf
206*e0f7c06dSGordon Berglingfunction first appeared in
207*e0f7c06dSGordon Bergling.Bx 4.2 .
208*e0f7c06dSGordon BerglingThe
209*e0f7c06dSGordon Bergling.Fn setvbuf
210*e0f7c06dSGordon Berglingfunction first appeared in
211*e0f7c06dSGordon Bergling.Bx 4.4 .
2125a9ecb5bSJilles Tjoelker.Sh BUGS
2135a9ecb5bSJilles Tjoelker.Fn setbuf
2145a9ecb5bSJilles Tjoelkerusually uses a suboptimal buffer size and should be avoided.
215