scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

wcsrtombs.c (514B)


      1 #include <string.h>
      2 #include <limits.h>
      3 #include <wchar.h>
      4 
      5 #undef wcsrtombs
      6 
      7 size_t
      8 wcsrtombs(char *restrict dest, const wchar_t **restrict src,
      9           size_t len, mbstate_t *restrict ps)
     10 {
     11 	size_t cnt, n;
     12 	wchar_t wc;
     13 	char buf[MB_LEN_MAX];
     14 
     15 	for (n = 0; ; n += cnt) {
     16 		wc = **src;
     17 		cnt = wcrtomb(buf, wc, ps);
     18 		if (cnt == (size_t) -1)
     19 			return -1;
     20 
     21 		if (dest) {
     22 			if (n+cnt > len)
     23 				return n;
     24 			memcpy(dest+n, buf, cnt);
     25 		}
     26 		*src += 1;
     27 
     28 		if (wc == L'\0')
     29 			break;
     30 	}
     31 	*src = NULL;
     32 
     33 	return n;
     34 }