diff options
Diffstat (limited to 'system/lib/libc/musl/src')
-rw-r--r-- | system/lib/libc/musl/src/stdio/fputws.c | 30 | ||||
-rw-r--r-- | system/lib/libc/musl/src/stdio/vswprintf.c | 17 |
2 files changed, 47 insertions, 0 deletions
diff --git a/system/lib/libc/musl/src/stdio/fputws.c b/system/lib/libc/musl/src/stdio/fputws.c new file mode 100644 index 00000000..70e004c9 --- /dev/null +++ b/system/lib/libc/musl/src/stdio/fputws.c @@ -0,0 +1,30 @@ +#include "stdio_impl.h" +#include <wchar.h> + +int fputws(const wchar_t *restrict ws, FILE *restrict f) +{ + unsigned char buf[BUFSIZ]; + size_t l=0; + + FLOCK(f); + +#if 0 // XXX EMSCRIPTEN + f->mode |= f->mode+1; +#endif + + while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1) +#if 0 // XXX EMSCRIPTEN + if (__fwritex(buf, l, f) < l) { +#else + if (fwrite(buf, 1, l, f) < l) { +#endif + FUNLOCK(f); + return -1; + } + + FUNLOCK(f); + + return l; /* 0 or -1 */ +} + +weak_alias(fputws, fputws_unlocked); diff --git a/system/lib/libc/musl/src/stdio/vswprintf.c b/system/lib/libc/musl/src/stdio/vswprintf.c index 7d237bae..e906f7ae 100644 --- a/system/lib/libc/musl/src/stdio/vswprintf.c +++ b/system/lib/libc/musl/src/stdio/vswprintf.c @@ -29,6 +29,7 @@ static size_t sw_write(FILE *f, const unsigned char *s, size_t l) int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_list ap) { +#if 0 // XXX EMSCRIPTEN int r; FILE f; unsigned char buf[256]; @@ -50,4 +51,20 @@ int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_lis r = vfwprintf(&f, fmt, ap); sw_write(&f, 0, 0); return r>=n ? -1 : r; +#else + // XXX EMSCRIPTEN: use memfs through libc fs + // we write to a file, which is in multibyte, then we read, then expand to widechar + #define TEMPFILE "emscripten.vswprintf.temp.buffer" + FILE *f = fopen(TEMPFILE, "wb"); + int r = vfwprintf(f, fmt, ap); + fclose(f); + f = fopen(TEMPFILE, "rb"); + char buffer[r+1]; + fread(buffer, 1, r, f); + fclose(f); + remove(TEMPFILE); + buffer[r] = 0; + r = mbstowcs(s, buffer, n); + return r>=n ? -1 : r; +#endif } |