OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / setbuf.3
1 .\" Copyright (c) 1980, 1991 Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
7 .\"
8 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\"    must display the following acknowledgement:
19 .\"     This product includes software developed by the University of
20 .\"     California, Berkeley and its contributors.
21 .\" 4. Neither the name of the University nor the names of its contributors
22 .\"    may be used to endorse or promote products derived from this software
23 .\"    without specific prior written permission.
24 .\"
25 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" SUCH DAMAGE.
36 .\" %%%LICENSE_END
37 .\"
38 .\"     @(#)setbuf.3    6.10 (Berkeley) 6/29/91
39 .\"
40 .\" Converted for Linux, Mon Nov 29 14:55:24 1993, faith@cs.unc.edu
41 .\" Added section to BUGS, Sun Mar 12 22:28:33 MET 1995,
42 .\"                   Thomas.Koenig@ciw.uni-karlsruhe.de
43 .\" Correction,  Sun, 11 Apr 1999 15:55:18,
44 .\"     Martin Vicente <martin@netadmin.dgac.fr>
45 .\" Correction,  2000-03-03, Andreas Jaeger <aj@suse.de>
46 .\" Added return value for setvbuf, aeb,
47 .\"
48 .TH SETBUF 3  2014-08-19 "Linux" "Linux Programmer's Manual"
49 .SH NAME
50 setbuf, setbuffer, setlinebuf, setvbuf \- stream buffering operations
51 .SH SYNOPSIS
52 .nf
53 .B #include <stdio.h>
54
55 .BI "void setbuf(FILE *" stream ", char *" buf );
56
57 .BI "void setbuffer(FILE *" stream ", char *" buf ", size_t "  size );
58
59 .BI "void setlinebuf(FILE *" stream );
60
61 .BI "int setvbuf(FILE *" stream ", char *" buf ", int " mode \
62 ", size_t " size );
63 .fi
64 .sp
65 .in -4n
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
68 .in
69 .sp
70 .BR setbuffer (),
71 .BR setlinebuf ():
72 _BSD_SOURCE
73 .SH DESCRIPTION
74 The three types of buffering available are unbuffered, block buffered, and
75 line buffered.
76 When an output stream is unbuffered, information appears on
77 the destination file or terminal as soon as written; when it is block
78 buffered many characters are saved up and written as a block; when it is
79 line buffered characters are saved up until a newline is output or input is
80 read from any stream attached to a terminal device (typically \fIstdin\fP).
81 The function
82 .BR fflush (3)
83 may be used to force the block out early.
84 (See
85 .BR fclose (3).)
86 Normally all files are block buffered.
87 When the first I/O operation occurs on a file,
88 .BR malloc (3)
89 is called, and a buffer is obtained.
90 If a stream refers to a terminal (as
91 .I stdout
92 normally does), it is line buffered.
93 The standard error stream
94 .I stderr
95 is always unbuffered by default.
96 .PP
97 The
98 .BR setvbuf ()
99 function may be used on any open stream to change its buffer.
100 The
101 .I mode
102 argument must be one of the following three macros:
103 .RS
104 .TP
105 .B _IONBF
106 unbuffered
107 .TP
108 .B _IOLBF
109 line buffered
110 .TP
111 .B _IOFBF
112 fully buffered
113 .RE
114 .PP
115 Except for unbuffered files, the
116 .I buf
117 argument should point to a buffer at least
118 .I size
119 bytes long; this buffer will be used instead of the current buffer.
120 If the argument
121 .I buf
122 is NULL,
123 only the mode is affected; a new buffer will be allocated on the next read
124 or write operation.
125 The
126 .BR setvbuf ()
127 function may be used only after opening a stream and before any other
128 operations have been performed on it.
129 .PP
130 The other three calls are, in effect, simply aliases for calls to
131 .BR setvbuf ().
132 The
133 .BR setbuf ()
134 function is exactly equivalent to the call
135 .PP
136 .in +4n
137 setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
138 .in
139 .PP
140 The
141 .BR setbuffer ()
142 function is the same, except that the size of the buffer is up to the
143 caller, rather than being determined by the default
144 .BR BUFSIZ .
145 The
146 .BR setlinebuf ()
147 function is exactly equivalent to the call:
148 .PP
149 .in +4n
150 setvbuf(stream, NULL, _IOLBF, 0);
151 .in
152 .SH RETURN VALUE
153 The function
154 .BR setvbuf ()
155 returns 0 on success.
156 It returns nonzero on failure
157 .RI ( mode
158 is invalid or the request cannot be honored).
159 It may set
160 .I errno
161 on failure.
162
163 The other functions do not return a value.
164 .SH ATTRIBUTES
165 .SS Multithreading (see pthreads(7))
166 The
167 .BR setbuf (),
168 .BR setbuffer (),
169 .BR setlinebuf (),
170 and
171 .BR setvbuf ()
172 functions are thread-safe.
173 .SH CONFORMING TO
174 The
175 .BR setbuf ()
176 and
177 .BR setvbuf ()
178 functions conform to C89 and C99.
179 .SH BUGS
180 .\" The
181 .\" .BR setbuffer ()
182 .\" and
183 .\" .BR setlinebuf ()
184 .\" functions are not portable to versions of BSD before 4.2BSD, and
185 .\" are available under Linux since libc 4.5.21.
186 .\" On 4.2BSD and 4.3BSD systems,
187 .\" .BR setbuf ()
188 .\" always uses a suboptimal buffer size and should be avoided.
189 .P
190 You must make sure that the space that
191 .I buf
192 points to still exists by the time
193 .I stream
194 is closed, which also happens at program termination.
195 For example, the following is invalid:
196 .nf
197 .sp
198 #include <stdio.h>
199
200 int
201 main(void)
202 {
203     char buf[BUFSIZ];
204     setbuf(stdin, buf);
205     printf("Hello, world!\\n");
206     return 0;
207 }
208 .fi
209 .SH SEE ALSO
210 .BR fclose (3),
211 .BR fflush (3),
212 .BR fopen (3),
213 .BR fread (3),
214 .BR malloc (3),
215 .BR printf (3),
216 .BR puts (3)
217 .SH COLOPHON
218 This page is part of release 3.79 of the Linux
219 .I man-pages
220 project.
221 A description of the project,
222 information about reporting bugs,
223 and the latest version of this page,
224 can be found at
225 \%http://www.kernel.org/doc/man\-pages/.