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 * advertising materials, and 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]>);
32 int setvbuf(<[fp]>, <[buf]>, <[mode]>, <[size]>)
39 Use <<setvbuf>> to specify what kind of buffering you want for the
40 file or stream identified by <[fp]>, by using one of the following
41 values (from <<stdio.h>>) as the <[mode]> argument:
45 Do not use a buffer: send output directly to the host system for the
46 file or stream identified by <[fp]>.
49 Use full output buffering: output will be passed on to the host system
50 only when the buffer is full, or when an input operation intervenes.
53 Use line buffering: pass on output to the host system at every
54 newline, as well as when the buffer is full, or when an input
58 Use the <[size]> argument to specify how large a buffer you wish. You
59 can supply the buffer itself, if you wish, by passing a pointer to a
60 suitable area of memory as <[buf]>. Otherwise, you may pass <<NULL>>
61 as the <[buf]> argument, and <<setvbuf>> will allocate the buffer.
64 You may only use <<setvbuf>> before performing any file operation other
65 than opening the file.
67 If you supply a non-null <[buf]>, you must ensure that the associated
68 storage continues to be available until you close the stream
72 A <<0>> result indicates success, <<EOF>> failure (invalid <[mode]> or
73 <[size]> can cause failure).
76 Both ANSI C and the System V Interface Definition (Issue 2) require
77 <<setvbuf>>. However, they differ on the meaning of a <<NULL>> buffer
78 pointer: the SVID issue 2 specification says that a <<NULL>> buffer
79 pointer requests unbuffered output. For maximum portability, avoid
80 <<NULL>> buffer pointers.
82 Both specifications describe the result on failure only as a
85 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
86 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
95 * Set one of the three kinds of buffering, optionally including a buffer.
99 _DEFUN (setvbuf
, (fp
, buf
, mode
, size
),
100 register FILE * fp _AND
102 register int mode _AND
103 register size_t size
)
112 * Verify arguments. The `int' limit on `size' is due to this
113 * particular implementation.
116 if ((mode
!= _IOFBF
&& mode
!= _IOLBF
&& mode
!= _IONBF
) || (int)(_POINTER_INT
) size
< 0)
123 * Write current buffer, if any; drop read count, if any.
124 * Make sure putc() will not think fp is line buffered.
125 * Free old buffer if it was from malloc(). Clear line and
126 * non buffer flags, and clear malloc flag.
132 if (fp
->_flags
& __SMBF
)
133 _free_r (_REENT
, (void *) fp
->_bf
._base
);
134 fp
->_flags
&= ~(__SLBF
| __SNBF
| __SMBF
);
140 * Allocate buffer if needed. */
143 /* we need this here because malloc() may return a pointer
145 if (!size
) size
= BUFSIZ
;
146 if ((buf
= malloc (size
)) == NULL
)
149 /* Try another size... */
150 buf
= malloc (BUFSIZ
);
155 /* Can't allocate it, let's try another approach */
157 fp
->_flags
|= __SNBF
;
159 fp
->_bf
._base
= fp
->_p
= fp
->_nbuf
;
164 fp
->_flags
|= __SMBF
;
167 * Now put back whichever flag is needed, and fix _lbfsize
168 * if line buffered. Ensure output flush on exit if the
169 * stream will be buffered at all.
170 * If buf is NULL then make _lbfsize 0 to force the buffer
171 * to be flushed and hence malloced on first use
177 fp
->_flags
|= __SLBF
;
178 fp
->_lbfsize
= buf
? -size
: 0;
183 _REENT
->__cleanup
= _cleanup_r
;
184 fp
->_bf
._base
= fp
->_p
= (unsigned char *) buf
;
185 fp
->_bf
._size
= size
;
190 * Patch up write count if necessary.
193 if (fp
->_flags
& __SWR
)
194 fp
->_w
= fp
->_flags
& (__SLBF
| __SNBF
) ? 0 : size
;