diff options
Diffstat (limited to 'libc-top-half/musl/src/multibyte/wcsnrtombs.c')
-rw-r--r-- | libc-top-half/musl/src/multibyte/wcsnrtombs.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/multibyte/wcsnrtombs.c b/libc-top-half/musl/src/multibyte/wcsnrtombs.c new file mode 100644 index 0000000..95e25e7 --- /dev/null +++ b/libc-top-half/musl/src/multibyte/wcsnrtombs.c @@ -0,0 +1,35 @@ +#include <wchar.h> +#include <limits.h> +#include <string.h> + +size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, size_t n, mbstate_t *restrict st) +{ + const wchar_t *ws = *wcs; + size_t cnt = 0; + if (!dst) n=0; + while (ws && wn) { + char tmp[MB_LEN_MAX]; + size_t l = wcrtomb(n<MB_LEN_MAX ? tmp : dst, *ws, 0); + if (l==-1) { + cnt = -1; + break; + } + if (dst) { + if (n<MB_LEN_MAX) { + if (l>n) break; + memcpy(dst, tmp, l); + } + dst += l; + n -= l; + } + if (!*ws) { + ws = 0; + break; + } + ws++; + wn--; + cnt += l; + } + if (dst) *wcs = ws; + return cnt; +} |