Skip to content
Snippets Groups Projects
Verified Commit f6ca7d7c authored by jD91mZM2's avatar jD91mZM2
Browse files

Add S_ISSOCK and fix str(c)spn

parent b20307dc
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,9 @@ ...@@ -5,8 +5,9 @@
#define S_ISCHR(mode) mode & S_IFMT == S_IFCHR #define S_ISCHR(mode) mode & S_IFMT == S_IFCHR
#define S_ISBLK(mode) mode & S_IFMT == S_IFBLK #define S_ISBLK(mode) mode & S_IFMT == S_IFBLK
#define S_ISREG(mode) mode & S_IFMT == S_IFREG #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_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_atime st_atim.tv_sec
#define st_mtime st_mtim.tv_sec #define st_mtime st_mtim.tv_sec
......
...@@ -893,6 +893,20 @@ pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *mut c_char, base ...@@ -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] #[no_mangle]
pub unsafe extern "C" fn system(command: *const c_char) -> c_int { pub unsafe extern "C" fn system(command: *const c_char) -> c_int {
let child_pid = unistd::fork(); let child_pid = unistd::fork();
......
...@@ -154,34 +154,39 @@ pub unsafe extern "C" fn strcpy(s1: *mut c_char, s2: *const c_char) -> *mut c_ch ...@@ -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) strncpy(s1, s2, usize::MAX)
} }
#[no_mangle] pub unsafe fn inner_strspn(s1: *const c_char, s2: *const c_char, cmp: bool) -> size_t {
pub unsafe extern "C" fn strcspn(s1: *const c_char, s2: *const c_char) -> size_t {
let s1 = s1 as *const u8; let s1 = s1 as *const u8;
let s2 = s2 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; let mut i = 0;
while *s2.offset(i) != 0 { while *s2.offset(i) != 0 {
byteset[(*s2.offset(i) as usize) / (8 * byteset.len())] |= byteset[(*s2.offset(i) as usize) / BITSIZE] |=
1 << (*s2.offset(i) as usize % (8 * byteset.len())); 1 << (*s2.offset(i) as usize & (BITSIZE-1));
i += 1; i += 1;
} }
i = 0; // reset i = 0; // reset
while *s1.offset(i) != 0 { while *s1.offset(i) != 0 &&
if byteset[(*s1.offset(i) as usize) / (8 * byteset.len())] (byteset[(*s1.offset(i) as usize) / BITSIZE] &
& 1 << (*s1.offset(i) as usize % (8 * byteset.len())) > 0 1 << (*s1.offset(i) as usize & (BITSIZE-1)) != 0) == cmp {
{
break;
}
i += 1; i += 1;
} }
i as size_t 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] #[no_mangle]
pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char { pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
strndup(s1, usize::MAX) strndup(s1, usize::MAX)
...@@ -318,30 +323,7 @@ pub unsafe extern "C" fn strsignal(sig: c_int) -> *mut c_char { ...@@ -318,30 +323,7 @@ pub unsafe extern "C" fn strsignal(sig: c_int) -> *mut c_char {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn strspn(s1: *const c_char, s2: *const c_char) -> size_t { pub unsafe extern "C" fn strspn(s1: *const c_char, s2: *const c_char) -> size_t {
let s1 = s1 as *const u8; inner_strspn(s1, s2, true)
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
} }
#[no_mangle] #[no_mangle]
......
...@@ -7,12 +7,14 @@ extern crate platform; ...@@ -7,12 +7,14 @@ extern crate platform;
use platform::types::*; use platform::types::*;
pub const S_IFMT: c_int = 0o0170000; 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_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_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_IFLNK: c_int = 0o120000;
pub const S_IFSOCK: c_int = 0o140000;
pub const S_IRWXU: c_int = 0o0700; pub const S_IRWXU: c_int = 0o0700;
pub const S_IRUSR: c_int = 0o0400; pub const S_IRUSR: c_int = 0o0400;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment