xref: /csrg-svn/lib/libc/stdio/setbuf.3 (revision 56989)
148351Scael.\" Copyright (c) 1980, 1991 Regents of the University of California.
248351Scael.\" All rights reserved.
320409Smckusick.\"
450319Sbostic.\" This code is derived from software contributed to Berkeley by
550319Sbostic.\" the American National Standards Committee X3, on Information
650319Sbostic.\" Processing Systems.
750319Sbostic.\"
848351Scael.\" %sccs.include.redist.man%
920409Smckusick.\"
10*56989Storek.\"     @(#)setbuf.3	6.11 (Berkeley) 12/04/92
1148351Scael.\"
1248351Scael.Dd
1348351Scael.Dt SETBUF 3
1448351Scael.Os BSD 4
1548351Scael.Sh NAME
1648351Scael.Nm setbuf ,
1748351Scael.Nm setbuffer ,
1848351Scael.Nm setlinebuf ,
1948351Scael.Nm setvbuf
2048351Scael.Nd stream buffering operations
2148351Scael.Sh SYNOPSIS
2248351Scael.Fd #include <stdio.h>
23*56989Storek.Ft void
2448351Scael.Fn setbuf "FILE *stream" "char *buf"
25*56989Storek.Ft void
2648351Scael.Fn setbuffer "FILE *stream" "char *buf" "size_t size"
2748351Scael.Ft int
2848351Scael.Fn setlinebuf "FILE *stream"
2948351Scael.Ft int
3048351Scael.Fn setvbuf "FILE *stream" "char *buf" "int mode" "size_t size"
3148351Scael.Sh DESCRIPTION
3220410SmckusickThe three types of buffering available are unbuffered, block buffered,
3320410Smckusickand line buffered.
3420410SmckusickWhen an output stream is unbuffered, information appears on the
3520410Smckusickdestination file or terminal as soon as written;
3620410Smckusickwhen it is block buffered many characters are saved up and written as a block;
3720410Smckusickwhen it is line buffered characters are saved up until a newline is
3846102Sbosticoutput or input is read from any stream attached to a terminal device
3946102Sbostic(typically stdin).
4048351ScaelThe function
4148351Scael.Xr fflush 3
4220410Smckusickmay be used to force the block out early.
4348351Scael(See
4448351Scael.Xr fclose 3 . )
45*56989Storek.Pp
4620410SmckusickNormally all files are block buffered.
4748351ScaelWhen the first
4848351Scael.Tn I/O
4948351Scaeloperation occurs on a file,
5048351Scael.Xr malloc 3
5148351Scaelis called,
52*56989Storekand an optimally-sized buffer is obtained.
5346102SbosticIf a stream refers to a terminal
5446102Sbostic(as
5548351Scael.Em stdout
5646102Sbosticnormally does) it is line buffered.
5748351ScaelThe standard error stream
5848351Scael.Em stderr
5920410Smckusickis always unbuffered.
6048351Scael.Pp
6148351ScaelThe
6248351Scael.Fn setvbuf
6348351Scaelfunction
64*56989Storekmay be used to alter the buffering behavior of a stream.
6546102SbosticThe
6648351Scael.Fa mode
6746102Sbosticparameter must be one of the following three macros:
6848351Scael.Bl -tag -width _IOFBF -offset indent
6948351Scael.It Dv _IONBF
7046102Sbosticunbuffered
7148351Scael.It Dv _IOLBF
7246102Sbosticline buffered
7348351Scael.It Dv _IOFBF
7446102Sbosticfully buffered
7548351Scael.El
7648351Scael.Pp
77*56989StorekThe
78*56989Storek.Fa size
79*56989Storekparameter may be given as zero
80*56989Storekto obtain deferred optimal-size buffer allocation as usual.
81*56989StorekIf it is not zero,
82*56989Storekthen except for unbuffered files, the
8348351Scael.Fa buf
8446102Sbosticargument should point to a buffer at least
8548351Scael.Fa size
8646102Sbosticbytes long;
8746102Sbosticthis buffer will be used instead of the current buffer.
88*56989Storek(If the
89*56989Storek.Fa size
90*56989Storekargument
91*56989Storekis not zero but
9248351Scael.Fa buf
93*56989Storekis
94*56989Storek.Dv NULL ,
95*56989Storeka buffer of the given size will be allocated immediately,
96*56989Storekand released on close.
97*56989StorekThis is an extension to ANSI C;
98*56989Storekportable code should use a size of 0 with any
99*56989Storek.Dv NULL
100*56989Storekbuffer.)
101*56989Storek.Pp
10248351ScaelThe
10348351Scael.Fn setvbuf
104*56989Storekfunction may be used at any time,
105*56989Storekbut may have peculiar side effects
106*56989Storek(such as discarding input or flushing output)
107*56989Storekif the stream is ``active''.
108*56989StorekPortable applications should call it only once on any given stream,
109*56989Storekand before any
110*56989Storek.Tn I/O
111*56989Storekis performed.
11248351Scael.Pp
113*56989StorekThe other three calls are, in effect, simply aliases for calls to
11448351Scael.Fn setvbuf .
115*56989StorekExcept for the lack of a return value, the
11648351Scael.Fn setbuf
117*56989Storekfunction is exactly equivalent to the call
11848351Scael.Pp
119*56989Storek.Dl "setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);"
12048351Scael.Pp
12148351ScaelThe
12248351Scael.Fn setbuffer
12348351Scaelfunction
12446102Sbosticis the same, except that the size of the buffer is up to the caller,
12548351Scaelrather than being determined by the default
12648351Scael.Dv BUFSIZ .
12748351ScaelThe
12848351Scael.Fn setlinebuf
12948351Scaelfunction
13048351Scaelis exactly equivalent to the call:
13148351Scael.Pp
132*56989Storek.Dl "setvbuf(stream, (char *)NULL, _IOLBF, 0);"
133*56989Storek.Sh RETURN VALUES
134*56989StorekThe
135*56989Storek.Fn setvbuf
136*56989Storekfunction returns 0 on success, or
137*56989Storek.Dv EOF
138*56989Storekif the request cannot be honored
139*56989Storek(note that the stream is still functional in this case).
140*56989Storek.Pp
141*56989StorekThe
142*56989Storek.Fn setlinebuf
143*56989Storekfunction returns what the equivalent
144*56989Storek.Fn setvbuf
145*56989Storekwould have returned.
14648351Scael.Sh SEE ALSO
14748351Scael.Xr fopen 3 ,
14848351Scael.Xr fclose 3 ,
14948351Scael.Xr fread 3 ,
15048351Scael.Xr malloc 3 ,
15148351Scael.Xr puts 3 ,
15248351Scael.Xr printf 3
15348351Scael.Sh STANDARDS
15448351ScaelThe
15548351Scael.Fn setbuf
15620410Smckusickand
15748351Scael.Fn setvbuf
15848351Scaelfunctions
15948351Scaelconform to
16048351Scael.St -ansiC .
16148351Scael.Sh BUGS
16220410SmckusickThe
16348351Scael.Fn setbuffer
16420410Smckusickand
16548351Scael.Fn setlinebuf
16648351Scaelfunctions are not portable to versions of
16748351Scael.Bx
16848351Scaelbefore
16948351Scael.Bx 4.2 .
17048351ScaelOn
17148351Scael.Bx 4.2
17248351Scaeland
17348351Scael.Bx 4.3
17448351Scaelsystems,
17548351Scael.Fn setbuf
17628038Skarelsalways uses a suboptimal buffer size and should be avoided.
177