X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Fsetvbuf.c;h=523dddc8ba28f53a7b3fcc4e3e5323de092acc22;hb=0e5234807dcdc76c43f9313c6ba9e2b7da408d8c;hp=2985d3f1b6b8bcfef43e8bc6308168ce5caa53d0;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/stdio/setvbuf.c b/src/stdio/setvbuf.c index 2985d3f1..523dddc8 100644 --- a/src/stdio/setvbuf.c +++ b/src/stdio/setvbuf.c @@ -1,22 +1,29 @@ #include "stdio_impl.h" -/* This function makes no attempt to protect the user from his/her own - * stupidity. If called any time but when then ISO C standard specifically - * allows it, all hell can and will break loose, especially with threads! - * - * This implementation ignores all arguments except the buffering type, - * and uses the existing buffer allocated alongside the FILE object. - * In the case of stderr where the preexisting buffer is length 1, it - * is not possible to set line buffering or full buffering. */ +/* The behavior of this function is undefined except when it is the first + * operation on the stream, so the presence or absence of locking is not + * observable in a program whose behavior is defined. Thus no locking is + * performed here. No allocation of buffers is performed, but a buffer + * provided by the caller is used as long as it is suitably sized. */ -int setvbuf(FILE *f, char *buf, int type, size_t size) +int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) { f->lbf = EOF; - if (type == _IONBF) - f->buf_size = 1; - else if (type == _IOLBF) - f->lbf = '\n'; + if (type == _IONBF) { + f->buf_size = 0; + } else if (type == _IOLBF || type == _IOFBF) { + if (buf && size >= UNGET) { + f->buf = (void *)(buf + UNGET); + f->buf_size = size - UNGET; + } + if (type == _IOLBF && f->buf_size) + f->lbf = '\n'; + } else { + return -1; + } + + f->flags |= F_SVB; return 0; }