Browse Source

Add S_ISSOCK and fix str(c)spn

jD91mZM2 6 years ago
parent
commit
f6ca7d7c2d
4 changed files with 39 additions and 40 deletions
  1. 2 1
      include/bits/sys/stat.h
  2. 14 0
      src/stdlib/src/lib.rs
  3. 18 36
      src/string/src/lib.rs
  4. 5 3
      src/sys_stat/src/lib.rs

+ 2 - 1
include/bits/sys/stat.h

@@ -5,8 +5,9 @@
 #define S_ISCHR(mode) mode & S_IFMT == S_IFCHR
 #define S_ISBLK(mode) mode & S_IFMT == S_IFBLK
 #define S_ISREG(mode) mode & S_IFMT == S_IFREG
-#define S_ISIFO(mode) mode & S_IFMT == S_IFIFO
+#define S_ISFIFO(mode) mode & S_IFMT == S_IFIFO
 #define S_ISLNK(mode) mode & S_IFMT == S_IFLNK
+#define S_ISSOCK(mode) mode & S_IFMT == S_IFSOCK
 
 #define st_atime st_atim.tv_sec
 #define st_mtime st_mtim.tv_sec

+ 14 - 0
src/stdlib/src/lib.rs

@@ -893,6 +893,20 @@ pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *mut c_char, base
     )
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn strtoull(
+    s: *const c_char,
+    endptr: *mut *mut c_char,
+    base: c_int,
+) -> c_ulong {
+    strtoul(s, endptr, base)
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn strtoll(s: *const c_char, endptr: *mut *mut c_char, base: c_int) -> c_long {
+    strtol(s, endptr, base)
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn system(command: *const c_char) -> c_int {
     let child_pid = unistd::fork();

+ 18 - 36
src/string/src/lib.rs

@@ -154,34 +154,39 @@ pub unsafe extern "C" fn strcpy(s1: *mut c_char, s2: *const c_char) -> *mut c_ch
     strncpy(s1, s2, usize::MAX)
 }
 
-#[no_mangle]
-pub unsafe extern "C" fn strcspn(s1: *const c_char, s2: *const c_char) -> size_t {
+pub unsafe fn inner_strspn(s1: *const c_char, s2: *const c_char, cmp: bool) -> size_t {
     let s1 = s1 as *const u8;
     let s2 = s2 as *const u8;
 
-    // The below logic is effectively ripped from the musl implementation
+    // The below logic is effectively ripped from the musl implementation. It
+    // works by placing each byte as it's own bit in an array of numbers. Each
+    // number can hold up to 8 * mem::size_of::<usize>() bits. We need 256 bits
+    // in total, to fit one byte.
 
-    let mut byteset = [0usize; 32 / mem::size_of::<usize>()];
+    const BITSIZE: usize = 8 * mem::size_of::<usize>();
+    let mut byteset = [0usize; 256 / BITSIZE];
 
     let mut i = 0;
     while *s2.offset(i) != 0 {
-        byteset[(*s2.offset(i) as usize) / (8 * byteset.len())] |=
-            1 << (*s2.offset(i) as usize % (8 * byteset.len()));
+        byteset[(*s2.offset(i) as usize) / BITSIZE] |=
+            1 << (*s2.offset(i) as usize & (BITSIZE-1));
         i += 1;
     }
 
     i = 0; // reset
-    while *s1.offset(i) != 0 {
-        if byteset[(*s1.offset(i) as usize) / (8 * byteset.len())]
-            & 1 << (*s1.offset(i) as usize % (8 * byteset.len())) > 0
-        {
-            break;
-        }
+    while *s1.offset(i) != 0 &&
+            (byteset[(*s1.offset(i) as usize) / BITSIZE] &
+            1 << (*s1.offset(i) as usize & (BITSIZE-1)) != 0) == cmp {
         i += 1;
     }
     i as size_t
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn strcspn(s1: *const c_char, s2: *const c_char) -> size_t {
+    inner_strspn(s1, s2, false)
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
     strndup(s1, usize::MAX)
@@ -318,30 +323,7 @@ pub unsafe extern "C" fn strsignal(sig: c_int) -> *mut c_char {
 
 #[no_mangle]
 pub unsafe extern "C" fn strspn(s1: *const c_char, s2: *const c_char) -> size_t {
-    let s1 = s1 as *const u8;
-    let s2 = s2 as *const u8;
-
-    // The below logic is effectively ripped from the musl implementation
-
-    let mut byteset = [0usize; 32 / mem::size_of::<usize>()];
-
-    let mut i = 0;
-    while *s2.offset(i) != 0 {
-        byteset[(*s2.offset(i) as usize) / (8 * byteset.len())] |=
-            1 << (*s2.offset(i) as usize % (8 * byteset.len()));
-        i += 1;
-    }
-
-    i = 0; // reset
-    while *s1.offset(i) != 0 {
-        if byteset[(*s1.offset(i) as usize) / (8 * byteset.len())]
-            & 1 << (*s1.offset(i) as usize % (8 * byteset.len())) < 1
-        {
-            break;
-        }
-        i += 1;
-    }
-    i as size_t
+    inner_strspn(s1, s2, true)
 }
 
 #[no_mangle]

+ 5 - 3
src/sys_stat/src/lib.rs

@@ -7,12 +7,14 @@ extern crate platform;
 use platform::types::*;
 
 pub const S_IFMT: c_int = 0o0170000;
-pub const S_IFBLK: c_int = 0o060000;
+
+pub const S_IFDIR: c_int = 0o040000;
 pub const S_IFCHR: c_int = 0o020000;
-pub const S_IFIFO: c_int = 0o010000;
+pub const S_IFBLK: c_int = 0o060000;
 pub const S_IFREG: c_int = 0o100000;
-pub const S_IFDIR: c_int = 0o040000;
+pub const S_IFIFO: c_int = 0o010000;
 pub const S_IFLNK: c_int = 0o120000;
+pub const S_IFSOCK: c_int = 0o140000;
 
 pub const S_IRWXU: c_int = 0o0700;
 pub const S_IRUSR: c_int = 0o0400;