1 #include "stdio_impl.h"
2 #include "param_check.h"
3 
4 /* The behavior of this function is undefined except when it is the first
5  * operation on the stream, so the presence or absence of locking is not
6  * observable in a program whose behavior is defined. Thus no locking is
7  * performed here. No allocation of buffers is performed, but a buffer
8  * provided by the caller is used as long as it is suitably sized. */
9 
setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size)10 int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size)
11 {
12 	PARAM_CHECK(f);
13 	f->lbf = EOF;
14 
15 	if (type == _IONBF) {
16 		f->buf_size = 0;
17 		f->flags |= F_NOBUF;
18 	} else if (type == _IOLBF || type == _IOFBF) {
19 		if (buf && size >= UNGET) {
20 			f->buf = (void *)(buf + UNGET);
21 			f->buf_size = size - UNGET;
22 		}
23 		if (type == _IOLBF && f->buf_size)
24 			f->lbf = '\n';
25 		f->flags &= ~F_NOBUF;
26 	} else {
27 		return -1;
28 	}
29 
30 	f->flags |= F_SVB;
31 
32 	return 0;
33 }
34