X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmultibyte%2Fwcsnrtombs.c;h=676932b5dcaa0d93da7fabded9c006dd57e19d90;hb=11ddc314b57196519316103b02acffe10299dad3;hp=7eb05d459ab826fbba890d52796302d799b1b251;hpb=571744447c23f91feb6439948f3a619aca850dfb;p=musl diff --git a/src/multibyte/wcsnrtombs.c b/src/multibyte/wcsnrtombs.c index 7eb05d45..676932b5 100644 --- a/src/multibyte/wcsnrtombs.c +++ b/src/multibyte/wcsnrtombs.c @@ -1,9 +1,3 @@ -/* - * 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 size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, size_t n, mbstate_t *restrict st) @@ -11,13 +5,14 @@ size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, s 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; @@ -28,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) { @@ -40,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;