summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018-04-18 15:29:18 -0400
committerRich Felker <dalias@aerifal.cx>2018-04-18 15:30:18 -0400
commit0b80a7b0404b6e49b0b724e3e3fe0ed5af3b08ef (patch)
treeacbe584d5efe01a2d054557760c9becf42b5cd6a /src
parent6019459251c58426808f207d18014b6b1f250a59 (diff)
downloadmusl-0b80a7b0404b6e49b0b724e3e3fe0ed5af3b08ef.tar.gz
add support for caller-provided buffers to setvbuf
Diffstat (limited to 'src')
-rw-r--r--src/stdio/setvbuf.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/stdio/setvbuf.c b/src/stdio/setvbuf.c
index 541a125f..0a0c139f 100644
--- a/src/stdio/setvbuf.c
+++ b/src/stdio/setvbuf.c
@@ -1,22 +1,25 @@
#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 lockign 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 *restrict f, char *restrict buf, int type, size_t size)
{
f->lbf = EOF;
- if (type == _IONBF)
+ if (type == _IONBF) {
f->buf_size = 0;
- else if (type == _IOLBF)
- f->lbf = '\n';
+ } else {
+ if (buf && size >= UNGET) {
+ f->buf = (void *)buf;
+ f->buf_size = size - UNGET;
+ }
+ if (type == _IOLBF && f->buf_size)
+ f->lbf = '\n';
+ }
f->flags |= F_SVB;