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