rewrite popen to use posix_spawn instead of fragile vfork hacks
[musl] / src / stdio / vswprintf.c
1 #include "stdio_impl.h"
2 #include <limits.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <wchar.h>
7
8 struct cookie {
9         wchar_t *ws;
10         size_t l;
11 };
12
13 static 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                 s+=i;
22                 l-=i;
23                 c->l--;
24                 c->ws++;
25         }
26         *c->ws = 0;
27         return i<0 ? i : l0;
28 }
29
30 int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_list ap)
31 {
32         int r;
33         FILE f;
34         unsigned char buf[256];
35         struct cookie c = { s, n-1 };
36
37         memset(&f, 0, sizeof(FILE));
38         f.lbf = EOF;
39         f.write = sw_write;
40         f.buf_size = sizeof buf;
41         f.buf = buf;
42         f.lock = -1;
43         f.cookie = &c;
44         if (!n) {
45                 return -1;
46         } else if (n > INT_MAX) {
47                 errno = EOVERFLOW;
48                 return -1;
49         }
50         r = vfwprintf(&f, fmt, ap);
51         sw_write(&f, 0, 0);
52         return r>=n ? -1 : r;
53 }