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