ref: 313e86a65b33fb0c9f4c05789e2e93de19fb61bc
parent: 7db7502fee313e42ccf0506a0f0a65db26aa883a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Mar 16 08:10:43 EDT 2022
libc: Add mbtowc()
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -11,7 +11,7 @@
#define _ATEXIT_MAX 32
-#define MB_CUR_MAX 1
+#define MB_CUR_MAX 6
#define RAND_MAX 32767
typedef struct {
--- a/src/libc/stdlib/Makefile
+++ b/src/libc/stdlib/Makefile
@@ -18,6 +18,7 @@
labs.$O\
llabs.$O\
malloc.$O\
+ mbtowc.$O\
qsort.$O\
rand.$O\
realloc.$O\
--- /dev/null
+++ b/src/libc/stdlib/mbtowc.c
@@ -1,0 +1,34 @@
+#include <stdlib.h>
+
+#undef mbtowc
+
+int
+mbtowc(wchar_t *restrict pwc, const char *restrict s, size_t n)
+{
+ const char *t = s;
+ unsigned long wc;
+ unsigned c;
+ size_t len;
+
+ if (s == NULL)
+ return 0;
+
+ wc = c = *t++;
+ for (len = 0; n > 0 && c & 0x80; --n, ++len)
+ c <<= 1;
+ if (n == 0 || len == 1 || len == 8)
+ return -1;
+ if (len == 0)
+ goto return_code;
+
+ for (wc = (c & 0xFF) >> len; len--; wc |= c & 0x3F) {
+ if ((c = *t++) & 0xC0 != 0x80)
+ return -1;
+ wc <<= 6;
+ }
+
+return_code:
+ if (pwc)
+ *pwc = wc;
+ return (*s) ? t - s : 0;
+}