master
1#include "stdio_impl.h"
2#include <limits.h>
3#include <errno.h>
4#include <stdint.h>
5#include <stdlib.h>
6#include <wchar.h>
7
8struct cookie {
9 wchar_t *ws;
10 size_t l;
11};
12
13static size_t sw_write(FILE *f, const unsigned char *s, size_t l)
14{
15 size_t l0 = l;
16 int i = 0;
17 struct cookie *c = f->cookie;
18 if (s!=f->wbase && sw_write(f, f->wbase, f->wpos-f->wbase)==-1)
19 return -1;
20 while (c->l && l && (i=mbtowc(c->ws, (void *)s, l))>=0) {
21 if (!i) i=1;
22 s+=i;
23 l-=i;
24 c->l--;
25 c->ws++;
26 }
27 *c->ws = 0;
28 if (i < 0) {
29 f->wpos = f->wbase = f->wend = 0;
30 f->flags |= F_ERR;
31 return i;
32 }
33 f->wend = f->buf + f->buf_size;
34 f->wpos = f->wbase = f->buf;
35 return l0;
36}
37
38int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_list ap)
39{
40 int r;
41 unsigned char buf[256];
42 struct cookie c = { s, n-1 };
43 FILE f = {
44 .lbf = EOF,
45 .write = sw_write,
46#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
47 .lock = -1,
48#endif
49 .buf = buf,
50 .buf_size = sizeof buf,
51 .cookie = &c,
52 };
53
54 if (!n) {
55 return -1;
56 }
57 r = vfwprintf(&f, fmt, ap);
58 sw_write(&f, 0, 0);
59 return r>=n ? -1 : r;
60}