2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<setvbuf>>---specify file or stream buffering
27 int setvbuf(FILE *<[fp]>, char *<[buf]>,
28 int <[mode]>, size_t <[size]>);
31 Use <<setvbuf>> to specify what kind of buffering you want for the
32 file or stream identified by <[fp]>, by using one of the following
33 values (from <<stdio.h>>) as the <[mode]> argument:
37 Do not use a buffer: send output directly to the host system for the
38 file or stream identified by <[fp]>.
41 Use full output buffering: output will be passed on to the host system
42 only when the buffer is full, or when an input operation intervenes.
45 Use line buffering: pass on output to the host system at every
46 newline, as well as when the buffer is full, or when an input
50 Use the <[size]> argument to specify how large a buffer you wish. You
51 can supply the buffer itself, if you wish, by passing a pointer to a
52 suitable area of memory as <[buf]>. Otherwise, you may pass <<NULL>>
53 as the <[buf]> argument, and <<setvbuf>> will allocate the buffer.
56 You may only use <<setvbuf>> before performing any file operation other
57 than opening the file.
59 If you supply a non-null <[buf]>, you must ensure that the associated
60 storage continues to be available until you close the stream
64 A <<0>> result indicates success, <<EOF>> failure (invalid <[mode]> or
65 <[size]> can cause failure).
68 Both ANSI C and the System V Interface Definition (Issue 2) require
69 <<setvbuf>>. However, they differ on the meaning of a <<NULL>> buffer
70 pointer: the SVID issue 2 specification says that a <<NULL>> buffer
71 pointer requests unbuffered output. For maximum portability, avoid
72 <<NULL>> buffer pointers.
74 Both specifications describe the result on failure only as a
77 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
78 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
87 * Set one of the three kinds of buffering, optionally including a buffer.
91 setvbuf (register FILE * fp
,
97 struct _reent
*reent
= _REENT
;
101 CHECK_INIT (reent
, fp
);
104 * Verify arguments. The `int' limit on `size' is due to this
105 * particular implementation. Note, buf and size are ignored
106 * when setting _IONBF.
109 if ((mode
!= _IOFBF
&& mode
!= _IOLBF
) || (int)(_POINTER_INT
) size
< 0)
114 * Write current buffer, if any; drop read count, if any.
115 * Make sure putc() will not think fp is line buffered.
116 * Free old buffer if it was from malloc(). Clear line and
117 * non buffer flags, and clear malloc flag.
119 _newlib_flockfile_start (fp
);
120 _fflush_r (reent
, fp
);
123 fp
->_r
= fp
->_lbfsize
= 0;
124 if (fp
->_flags
& __SMBF
)
125 _free_r (reent
, (void *) fp
->_bf
._base
);
126 fp
->_flags
&= ~(__SLBF
| __SNBF
| __SMBF
| __SOPT
| __SNPT
| __SEOF
);
132 * Find optimal I/O size for seek optimization. This also returns
133 * a `tty flag' to suggest that we check isatty(fd), but we do not
134 * care since our caller told us how to buffer.
136 fp
->_flags
|= __swhatbuf_r (reent
, fp
, &iosize
, &ttyflag
);
143 /* Allocate buffer if needed. */
146 if ((buf
= malloc (size
)) == NULL
)
149 * Unable to honor user's request. We will return
150 * failure, but try again with file system size.
161 /* No luck; switch to unbuffered I/O. */
163 fp
->_flags
|= __SNBF
;
165 fp
->_bf
._base
= fp
->_p
= fp
->_nbuf
;
167 _newlib_flockfile_exit (fp
);
170 fp
->_flags
|= __SMBF
;
174 * We're committed to buffering from here, so make sure we've
175 * registered to flush buffers on exit.
177 if (!_REENT_CLEANUP(reent
))
180 #ifdef _FSEEK_OPTIMIZATION
182 * Kill any seek optimization if the buffer is not the
185 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
188 fp
->_flags
|= __SNPT
;
192 * Fix up the FILE fields, and set __cleanup for output flush on
193 * exit (since we are buffered in some way).
196 fp
->_flags
|= __SLBF
;
197 fp
->_bf
._base
= fp
->_p
= (unsigned char *) buf
;
198 fp
->_bf
._size
= size
;
199 /* fp->_lbfsize is still 0 */
200 if (fp
->_flags
& __SWR
)
203 * Begin or continue writing: see __swsetup(). Note
204 * that __SNBF is impossible (it was handled earlier).
206 if (fp
->_flags
& __SLBF
)
209 fp
->_lbfsize
= -fp
->_bf
._size
;
216 /* begin/continue reading, or stay in intermediate state */
220 _newlib_flockfile_end (fp
);