X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmultibyte%2Fwcsnrtombs.c;h=676932b5dcaa0d93da7fabded9c006dd57e19d90;hb=64d0e86576ef1d33e996a926d6a02d38fb88a768;hp=a2e308b3be62e0eeb720a2ae19c4d16d14abb7aa;hpb=400c5e5c8307a2ebe44ef1f203f5a15669f20347;p=musl diff --git a/src/multibyte/wcsnrtombs.c b/src/multibyte/wcsnrtombs.c index a2e308b3..676932b5 100644 --- a/src/multibyte/wcsnrtombs.c +++ b/src/multibyte/wcsnrtombs.c @@ -1,28 +1,18 @@ -/* - * This code was written by Rich Felker in 2010; no copyright is claimed. - * This code is in the public domain. Attribution is appreciated but - * unnecessary. - */ - -#include -#include #include -#include - -#include "internal.h" size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, size_t n, mbstate_t *restrict st) { size_t l, cnt=0, n2; char *s, buf[256]; const wchar_t *ws = *wcs; + const wchar_t *tmp_ws; if (!dst) s = buf, n = sizeof buf; else s = dst; while ( ws && n && ( (n2=wn)>=n || n2>32 ) ) { if (n2>=n) n2=n; - wn -= n2; + tmp_ws = ws; l = wcsrtombs(s, &ws, n2, 0); if (!(l+1)) { cnt = l; @@ -33,6 +23,7 @@ size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, s s += l; n -= l; } + wn = ws ? wn - (ws - tmp_ws) : 0; cnt += l; } if (ws) while (n && wn) { @@ -45,7 +36,7 @@ size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, s ws++; wn--; /* safe - this loop runs fewer than sizeof(buf) times */ s+=l; n-=l; - cnt++; + cnt += l; } if (dst) *wcs = ws; return cnt;