2 * This code was written by Rich Felker in 2010; no copyright is claimed.
3 * This code is in the public domain. Attribution is appreciated but
15 size_t mbsnrtowcs(wchar_t *wcs, const char **src, size_t n, size_t wn, mbstate_t *st)
18 wchar_t *ws, wbuf[256];
21 if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
24 /* making sure output buffer size is at most n/4 will ensure
25 * that mbsrtowcs never reads more than n input bytes. thus
26 * we can use mbsrtowcs as long as it's practical.. */
28 while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) {
31 l = mbsrtowcs(ws, &s, n2, st);
43 if (s) while (wn && n) {
44 l = mbrtowc(ws, s, n, st);
50 /* have to roll back partial character */
55 /* safe - this loop runs fewer than sizeof(wbuf)/8 times */