From 9f3aa6d4a88e5eebb7bc5ca9e3ded38b71dbd68e Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Mon, 19 Jul 2021 09:16:56 -0600
Subject: [PATCH] Define wctype_t and wctype

---
 include/bits/wctype.h           |  1 +
 include/stddef.h                |  3 ++-
 src/header/wctype/cbindgen.toml |  2 +-
 src/header/wctype/mod.rs        | 41 +++++++++++++++++++++++++++++++--
 src/platform/types.rs           |  2 +-
 5 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/include/bits/wctype.h b/include/bits/wctype.h
index fc2213533..6dc4a0de5 100644
--- a/include/bits/wctype.h
+++ b/include/bits/wctype.h
@@ -2,6 +2,7 @@
 #define _BITS_WCTYPE_H
 #include <stdint.h>
 
+#define __need_wctype_t
 #define __need_wint_t
 
 #endif /* _BITS_WCTYPE_H */
diff --git a/include/stddef.h b/include/stddef.h
index 1ee56ff6f..895f5c449 100644
--- a/include/stddef.h
+++ b/include/stddef.h
@@ -12,7 +12,8 @@ typedef __PTRDIFF_TYPE__ ptrdiff_t;
 #ifndef __cplusplus
 typedef int32_t wchar_t;
 #endif /* #ifndef __cplusplus */
-typedef int32_t wint_t;
+typedef uint32_t wctype_t;
+typedef uint32_t wint_t;
 
 
 typedef long unsigned int size_t;
diff --git a/src/header/wctype/cbindgen.toml b/src/header/wctype/cbindgen.toml
index 032cfcb0a..58fb8271b 100644
--- a/src/header/wctype/cbindgen.toml
+++ b/src/header/wctype/cbindgen.toml
@@ -1,4 +1,4 @@
-sys_includes = ["stddef.h", "stdint.h", "time.h", "stdio.h" ]
+sys_includes = ["wchar.h" ]
 include_guard = "_RELIBC_WCTYPE_H"
 header = "#include <bits/wctype.h>"
 language = "C"
diff --git a/src/header/wctype/mod.rs b/src/header/wctype/mod.rs
index b4b7b0eaa..d571aef16 100644
--- a/src/header/wctype/mod.rs
+++ b/src/header/wctype/mod.rs
@@ -1,11 +1,48 @@
-//! wchar implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/wchar.h.html
+//! wchar implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/wctype.h.html
 
-use crate::platform::types::*;
+use crate::{
+    c_str::CStr,
+    platform::types::*,
+};
 
 mod casecmp;
 use casecmp::casemap;
+
 pub const WEOF: wint_t = 0xFFFF_FFFFu32;
 
+pub const WCTYPE_ALNUM: wctype_t = 1;
+pub const WCTYPE_ALPHA: wctype_t = 2;
+pub const WCTYPE_BLANK: wctype_t = 3;
+pub const WCTYPE_CNTRL: wctype_t = 4;
+pub const WCTYPE_DIGIT: wctype_t = 5;
+pub const WCTYPE_GRAPH: wctype_t = 6;
+pub const WCTYPE_LOWER: wctype_t = 7;
+pub const WCTYPE_PRINT: wctype_t = 8;
+pub const WCTYPE_PUNCT: wctype_t = 9;
+pub const WCTYPE_SPACE: wctype_t = 10;
+pub const WCTYPE_UPPER: wctype_t = 11;
+pub const WCTYPE_XDIGIT: wctype_t = 12;
+
+#[no_mangle]
+pub unsafe extern "C" fn wctype(name: *const c_char) -> wctype_t {
+    let name_cstr = CStr::from_ptr(name);
+    match name_cstr.to_bytes() {
+        b"alnum" => WCTYPE_ALNUM,
+        b"alpha" => WCTYPE_ALPHA,
+        b"blank" => WCTYPE_BLANK,
+        b"cntrl" => WCTYPE_CNTRL,
+        b"digit" => WCTYPE_DIGIT,
+        b"graph" => WCTYPE_GRAPH,
+        b"lower" => WCTYPE_LOWER,
+        b"print" => WCTYPE_PRINT,
+        b"punct" => WCTYPE_PUNCT,
+        b"space" => WCTYPE_SPACE,
+        b"upper" => WCTYPE_UPPER,
+        b"xdigit" => WCTYPE_XDIGIT,
+        _ => 0
+    }
+}
+
 #[no_mangle]
 pub extern "C" fn towlower(wc: wint_t) -> wint_t {
     casemap(wc, 0)
diff --git a/src/platform/types.rs b/src/platform/types.rs
index 5d604d383..f48982939 100644
--- a/src/platform/types.rs
+++ b/src/platform/types.rs
@@ -45,8 +45,8 @@ pub type c_long = i64;
 pub type c_ulong = u64;
 
 pub type wchar_t = i32;
+pub type wctype_t = u32;
 pub type wint_t = u32;
-pub type wctype_t = i64;
 
 pub type regoff_t = size_t;
 pub type off_t = c_long;
-- 
GitLab