remove long-obsolete clang workarounds from mips* syscall_arch.h files
[musl.git] / src / stdio / open_wmemstream.c
blob4d90cd971964f5f91f6dca091e2f2a0d82d4b934
1 #include "stdio_impl.h"
2 #include <wchar.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <string.h>
7 struct cookie {
8 wchar_t **bufp;
9 size_t *sizep;
10 size_t pos;
11 wchar_t *buf;
12 size_t len;
13 size_t space;
14 mbstate_t mbs;
17 static off_t wms_seek(FILE *f, off_t off, int whence)
19 ssize_t base;
20 struct cookie *c = f->cookie;
21 if (whence>2U) {
22 fail:
23 errno = EINVAL;
24 return -1;
26 base = (size_t [3]){0, c->pos, c->len}[whence];
27 if (off < -base || off > SSIZE_MAX/4-base) goto fail;
28 memset(&c->mbs, 0, sizeof c->mbs);
29 return c->pos = base+off;
32 static size_t wms_write(FILE *f, const unsigned char *buf, size_t len)
34 struct cookie *c = f->cookie;
35 size_t len2;
36 wchar_t *newbuf;
37 if (len + c->pos >= c->space) {
38 len2 = 2*c->space+1 | c->pos+len+1;
39 if (len2 > SSIZE_MAX/4) return 0;
40 newbuf = realloc(c->buf, len2*4);
41 if (!newbuf) return 0;
42 *c->bufp = c->buf = newbuf;
43 memset(c->buf + c->space, 0, 4*(len2 - c->space));
44 c->space = len2;
47 len2 = mbsnrtowcs(c->buf+c->pos, (void *)&buf, len, c->space-c->pos, &c->mbs);
48 if (len2 == -1) return 0;
49 c->pos += len2;
50 if (c->pos >= c->len) c->len = c->pos;
51 *c->sizep = c->pos;
52 return len;
55 static int wms_close(FILE *f)
57 return 0;
60 FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
62 FILE *f;
63 struct cookie *c;
64 wchar_t *buf;
66 if (!(f=malloc(sizeof *f + sizeof *c))) return 0;
67 if (!(buf=malloc(sizeof *buf))) {
68 free(f);
69 return 0;
71 memset(f, 0, sizeof *f + sizeof *c);
72 f->cookie = c = (void *)(f+1);
74 c->bufp = bufp;
75 c->sizep = sizep;
76 c->pos = c->len = c->space = *sizep = 0;
77 c->buf = *bufp = buf;
78 *buf = 0;
80 f->flags = F_NORD;
81 f->fd = -1;
82 f->buf = (void *)(c+1);
83 f->buf_size = 0;
84 f->lbf = EOF;
85 f->write = wms_write;
86 f->seek = wms_seek;
87 f->close = wms_close;
89 if (!libc.threaded) f->lock = -1;
91 return __ofl_add(f);