scc

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

wcrtomb.c (407B)


      1 #include <errno.h>
      2 #include <wchar.h>
      3 
      4 #include "../libc.h"
      5 
      6 #undef wcrtomb
      7 
      8 size_t
      9 wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict ps)
     10 {
     11 	int i, n;
     12 	unsigned long c = wc;
     13 
     14 	if (!s)
     15 		return 1;
     16 
     17 	if (!_validutf8(wc, &n)) {
     18 		errno = EILSEQ;
     19 		return -1;
     20 	}
     21 
     22 	n--;
     23 	*s = 0;
     24 	for (i = 0; i < n; i++) {
     25 		*s >>= 1;
     26 		*s |= 0x80;
     27 
     28 		s[n-i] = 0x80 | (c & 0x3f);
     29 		c >>= 6;
     30 	}
     31 	*s |= c;
     32 
     33 	return n+1;
     34 }