scc

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

mbsrtowcs.c (453B)


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