2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (c) 1980, 1991 Regents of the University of California.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to Berkeley by
|
|
|
|
.\" the American National Standards Committee X3, on Information
|
|
|
|
.\" Processing Systems.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" @(#)setbuf.3 6.10 (Berkeley) 6/29/91
|
|
|
|
.\"
|
|
|
|
.\" Converted for Linux, Mon Nov 29 14:55:24 1993, faith@cs.unc.edu
|
|
|
|
.\" Added section to BUGS, Sun Mar 12 22:28:33 MET 1995,
|
|
|
|
.\" Thomas.Koenig@ciw.uni-karlsruhe.de
|
|
|
|
.\" Correction, Sun, 11 Apr 1999 15:55:18,
|
|
|
|
.\" Martin Vicente <martin@netadmin.dgac.fr>
|
|
|
|
.\" Correction, 2000-03-03, Andreas Jaeger <aj@suse.de>
|
2007-04-12 22:42:49 +00:00
|
|
|
.\" Added return value for setvbuf, aeb,
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2008-06-26 14:02:30 +00:00
|
|
|
.TH SETBUF 3 2008-06-26 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
setbuf, setbuffer, setlinebuf, setvbuf \- stream buffering operations
|
|
|
|
.SH SYNOPSIS
|
2008-01-11 13:59:47 +00:00
|
|
|
.nf
|
2004-11-03 13:51:07 +00:00
|
|
|
.B #include <stdio.h>
|
2008-01-11 13:59:47 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void setbuf(FILE *" stream ", char *" buf );
|
2008-01-11 13:59:47 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void setbuffer(FILE *" stream ", char *" buf ", size_t " size );
|
2008-01-11 13:59:47 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void setlinebuf(FILE *" stream );
|
2008-01-11 13:59:47 +00:00
|
|
|
|
|
|
|
.BI "int setvbuf(FILE *" stream ", char *" buf ", int " mode \
|
|
|
|
", size_t " size );
|
|
|
|
.fi
|
2007-07-08 12:11:40 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
2007-07-08 14:52:30 +00:00
|
|
|
.BR setbuffer (),
|
2007-07-08 12:11:40 +00:00
|
|
|
.BR setlinebuf ():
|
|
|
|
_BSD_SOURCE
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
|
|
|
The three types of buffering available are unbuffered, block buffered, and
|
2007-04-12 22:42:49 +00:00
|
|
|
line buffered.
|
|
|
|
When an output stream is unbuffered, information appears on
|
2004-11-03 13:51:07 +00:00
|
|
|
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
|
2007-06-17 14:12:43 +00:00
|
|
|
read from any stream attached to a terminal device (typically \fIstdin\fP).
|
2007-04-12 22:42:49 +00:00
|
|
|
The function
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR fflush (3)
|
|
|
|
may be used to force the block out early.
|
2007-04-12 22:42:49 +00:00
|
|
|
(See
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR fclose (3).)
|
2007-04-12 22:42:49 +00:00
|
|
|
Normally all files are block buffered.
|
|
|
|
When the first I/O operation occurs on a file,
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR malloc (3)
|
2007-04-12 22:42:49 +00:00
|
|
|
is called, and a buffer is obtained.
|
|
|
|
If a stream refers to a terminal (as
|
2004-11-03 13:51:07 +00:00
|
|
|
.I stdout
|
2007-04-12 22:42:49 +00:00
|
|
|
normally does) it is line buffered.
|
|
|
|
The standard error stream
|
2004-11-03 13:51:07 +00:00
|
|
|
.I stderr
|
|
|
|
is always unbuffered by default.
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setvbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function may be used on any open stream to change its buffer.
|
|
|
|
The
|
|
|
|
.I mode
|
2008-07-10 20:53:08 +00:00
|
|
|
argument must be one of the following three macros:
|
2004-11-03 13:51:07 +00:00
|
|
|
.RS
|
|
|
|
.TP
|
|
|
|
.B _IONBF
|
|
|
|
unbuffered
|
|
|
|
.TP
|
|
|
|
.B _IOLBF
|
|
|
|
line buffered
|
|
|
|
.TP
|
|
|
|
.B _IOFBF
|
|
|
|
fully buffered
|
|
|
|
.RE
|
|
|
|
.PP
|
|
|
|
Except for unbuffered files, the
|
|
|
|
.I buf
|
|
|
|
argument should point to a buffer at least
|
|
|
|
.I size
|
2007-04-12 22:42:49 +00:00
|
|
|
bytes long; this buffer will be used instead of the current buffer.
|
|
|
|
If the argument
|
2004-11-03 13:51:07 +00:00
|
|
|
.I buf
|
2005-11-02 13:55:25 +00:00
|
|
|
is NULL,
|
2004-11-03 13:51:07 +00:00
|
|
|
only the mode is affected; a new buffer will be allocated on the next read
|
2007-04-12 22:42:49 +00:00
|
|
|
or write operation.
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setvbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function may only be used after opening a stream and before any other
|
|
|
|
operations have been performed on it.
|
|
|
|
.PP
|
|
|
|
The other three calls are, in effect, simply aliases for calls to
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setvbuf ().
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function is exactly equivalent to the call
|
|
|
|
.PP
|
2007-12-19 07:19:23 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
|
2007-12-19 07:19:23 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setbuffer ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function is the same, except that the size of the buffer is up to the
|
|
|
|
caller, rather than being determined by the default
|
|
|
|
.BR BUFSIZ .
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setlinebuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function is exactly equivalent to the call:
|
|
|
|
.PP
|
2007-12-19 07:19:23 +00:00
|
|
|
.in +4n
|
2007-05-21 08:47:38 +00:00
|
|
|
setvbuf(stream, (char *) NULL, _IOLBF, 0);
|
2007-12-19 07:19:23 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
|
|
|
The function
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setvbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns 0 on success.
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
It returns nonzero on failure
|
2008-06-26 14:02:30 +00:00
|
|
|
.RI ( mode
|
|
|
|
is invalid or the request cannot be honored).
|
2007-04-12 22:42:49 +00:00
|
|
|
It may set
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
|
|
|
on failure.
|
2008-06-26 14:02:30 +00:00
|
|
|
|
|
|
|
The other functions do not return a value.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setvbuf ()
|
2006-08-03 13:57:30 +00:00
|
|
|
functions conform to C89 and C99.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setbuffer ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setlinebuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
functions are not portable to versions of BSD before 4.2BSD, and
|
2007-04-12 22:42:49 +00:00
|
|
|
are available under Linux since libc 4.5.21.
|
|
|
|
On 4.2BSD and 4.3BSD systems,
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR setbuf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
always uses a suboptimal buffer size and should be avoided.
|
|
|
|
.P
|
2007-10-08 18:32:31 +00:00
|
|
|
You must make sure that the space that
|
2004-11-03 13:51:07 +00:00
|
|
|
.I buf
|
2007-10-08 18:32:31 +00:00
|
|
|
points to still exists by the time
|
2004-11-03 13:51:07 +00:00
|
|
|
.I stream
|
|
|
|
is closed, which also happens at program termination.
|
2008-03-10 10:04:03 +00:00
|
|
|
For example, the following is invalid:
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
|
|
|
.sp
|
|
|
|
#include <stdio.h>
|
2007-04-05 12:36:57 +00:00
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
int
|
2007-04-05 12:36:57 +00:00
|
|
|
main(void)
|
2004-11-03 13:51:07 +00:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
setbuf(stdin, buf);
|
|
|
|
printf("Hello, world!\\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR fclose (3),
|
|
|
|
.BR fflush (3),
|
|
|
|
.BR fopen (3),
|
|
|
|
.BR fread (3),
|
|
|
|
.BR malloc (3),
|
|
|
|
.BR printf (3),
|
|
|
|
.BR puts (3)
|