1#include <wchar.h> 2 3size_t wcsnrtombs(char* restrict dst, const wchar_t** restrict wcs, size_t wn, size_t n, mbstate_t* restrict st) { 4 size_t l, cnt = 0, n2; 5 char *s, buf[256]; 6 const wchar_t* ws = *wcs; 7 8 if (!dst) 9 s = buf, n = sizeof buf; 10 else 11 s = dst; 12 13 while (ws && n && ((n2 = wn) >= n || n2 > 32)) { 14 if (n2 >= n) 15 n2 = n; 16 wn -= n2; 17 l = wcsrtombs(s, &ws, n2, 0); 18 if (!(l + 1)) { 19 cnt = l; 20 n = 0; 21 break; 22 } 23 if (s != buf) { 24 s += l; 25 n -= l; 26 } 27 cnt += l; 28 } 29 if (ws) 30 while (n && wn) { 31 l = wcrtomb(s, *ws, 0); 32 if ((l + 1) <= 1) { 33 if (!l) 34 ws = 0; 35 else 36 cnt = l; 37 break; 38 } 39 ws++; 40 wn--; 41 /* safe - this loop runs fewer than sizeof(buf) times */ 42 s += l; 43 n -= l; 44 cnt += l; 45 } 46 if (dst) 47 *wcs = ws; 48 return cnt; 49} 50