drop use of pthread_once in mutexattr kernel support tests
[musl] / src / stdio / fmemopen.c
index fb2656e..343e3e3 100644 (file)
@@ -1,7 +1,10 @@
 #include "stdio_impl.h"
 #include <errno.h>
 #include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
 #include <inttypes.h>
+#include "libc.h"
 
 struct cookie {
        size_t pos, len, size;
@@ -81,7 +84,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
        struct mem_FILE *f;
        int plus = !!strchr(mode, '+');
        
-       if (!size || !strchr("rwa", *mode)) {
+       if (!strchr("rwa", *mode)) {
                errno = EINVAL;
                return 0;
        }
@@ -93,18 +96,17 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
 
        f = malloc(sizeof *f + (buf?0:size));
        if (!f) return 0;
-       memset(&f->f, 0, sizeof f->f);
+       memset(f, 0, offsetof(struct mem_FILE, buf));
        f->f.cookie = &f->c;
        f->f.fd = -1;
        f->f.lbf = EOF;
        f->f.buf = f->buf + UNGET;
        f->f.buf_size = sizeof f->buf - UNGET;
        if (!buf) {
-               buf = f->buf2;;
+               buf = f->buf2;
                memset(buf, 0, size);
        }
 
-       memset(&f->c, 0, sizeof f->c);
        f->c.buf = buf;
        f->c.size = size;
        f->c.mode = *mode;
@@ -112,6 +114,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
        if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD;
        if (*mode == 'r') f->c.len = size;
        else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size);
+       else if (plus) *f->c.buf = 0;
 
        f->f.read = mread;
        f->f.write = mwrite;