commit 9a60d2a80efcd85c85f90b1c0406c8fe3f324f5b
parent c4b7399c26bf3040f33b4760e162495d74d85d73
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Sun, 4 Jan 2026 20:29:39 +0100
libc/wchar: Add wctob()
Diffstat:
6 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk
@@ -146,6 +146,7 @@ COMMON_OBJS =\
wchar/wcsstr.$O\
wchar/wcstok.$O\
wchar/wcsxfrm.$O\
+ wchar/wctob.$O\
wchar/wcwidth.$O\
wchar/wmemchr.$O\
wchar/wmemcmp.$O\
diff --git a/src/libc/wchar/Makefile b/src/libc/wchar/Makefile
@@ -13,6 +13,7 @@ OBJS =\
wcrtomb.$O\
wcslen.$O\
wcsrtombs.$O\
+ wctob.$O\
wcwidth.$O\
putwc.$O\
_validutf8.$O\
diff --git a/src/libc/wchar/wctob.c b/src/libc/wchar/wctob.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <wchar.h>
+
+#undef wctob
+
+int
+wctob(wint_t wc)
+{
+ unsigned long w = wc;
+
+ if (wc == WEOF || w > 128)
+ return EOF;
+ return wc;
+}
diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore
@@ -64,3 +64,4 @@
0064-wcstok
test.log
0065-btowc
+0066-wctob
diff --git a/tests/libc/execute/0066-wctob.c b/tests/libc/execute/0066-wctob.c
@@ -0,0 +1,22 @@
+#include <assert.h>
+#include <stdio.h>
+#include <wchar.h>
+
+/*
+output:
+testing
+done
+end:
+*/
+
+int
+main(void)
+{
+ puts("testing");
+ assert(wctob(L'a') == 'a');
+ assert(wctob(WEOF) == -1);
+ assert(wctob(134) == -1);
+ puts("done");
+
+ return 0;
+}
diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
@@ -62,3 +62,4 @@
0063-wcspbrk
0064-wcstok
0065-btowc
+0066-wctob