diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index bd5448c40447927fe524ee495cf56296ca0950ab..19e3a0f14d13e680a988f87404a230c3d417f39c 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -3,12 +3,12 @@ use core::str::FromStr; use core::{ptr, slice, str}; +use c_str::CStr; use header::errno::*; use header::netinet_in::{in_addr, in_addr_t, INADDR_NONE}; use header::sys_socket::constants::*; use header::sys_socket::socklen_t; use platform; -use platform::c_str; use platform::types::*; #[no_mangle] @@ -59,7 +59,8 @@ pub unsafe extern "C" fn inet_pton(domain: c_int, src: *const c_char, dest: *mut &mut (*(dest as *mut in_addr)).s_addr as *mut _ as *mut u8, 4, ); - let mut octets = str::from_utf8_unchecked(c_str(src)).split('.'); + let src_cstr = CStr::from_ptr(src); + let mut octets = str::from_utf8_unchecked(src_cstr.to_bytes()).split('.'); for i in 0..4 { if let Some(n) = octets.next().and_then(|x| u8::from_str(x).ok()) { s_addr[i] = n; diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs index e099d15e349c396e74f8af91a7db10cbde2961a4..d5e199e74ad2907df26c4f8667aebb3ef2c0ace2 100644 --- a/src/header/netdb/mod.rs +++ b/src/header/netdb/mod.rs @@ -11,10 +11,9 @@ use alloc::string::ToString; use alloc::vec::IntoIter; use alloc::{String, Vec}; -use c_str::CString; +use c_str::{CStr, CString}; use platform; -use platform::c_str; use platform::rlb::{Line, RawLineBuffer}; use platform::types::*; use platform::{Pal, Sys}; @@ -490,7 +489,8 @@ pub unsafe extern "C" fn gethostbyaddr( #[no_mangle] pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { // check if some idiot gave us an address instead of a name - let mut octets = str::from_utf8_unchecked(c_str(name)).split('.'); + let name_cstr = CStr::from_ptr(name); + let mut octets = str::from_utf8_unchecked(name_cstr.to_bytes()).split('.'); let mut s_addr = [0u8; 4]; let mut is_addr = true; for i in 0..4 { @@ -538,7 +538,9 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { } } - let mut host = match lookup_host(str::from_utf8_unchecked(c_str(name))) { + let name_cstr = CStr::from_ptr(name); + + let mut host = match lookup_host(str::from_utf8_unchecked(name_cstr.to_bytes())) { Ok(lookuphost) => lookuphost, Err(e) => { platform::errno = e; @@ -553,7 +555,7 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { } }; - let host_name: Vec<u8> = c_str(name).to_vec(); + let host_name: Vec<u8> = name_cstr.to_bytes().to_vec(); HOST_NAME = Some(host_name); _HOST_ADDR_LIST = mem::transmute::<u32, [u8; 4]>(host_addr.s_addr); HOST_ADDR_LIST = [_HOST_ADDR_LIST.as_mut_ptr() as *mut c_char, ptr::null_mut()]; diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 6ba42ef7869d2f6ff6ee7a583db013b3662baf11..6a8867d727db16ffb138b1ddb58b27de34721442 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -15,7 +15,7 @@ use header::stdlib::mkstemp; use header::string::strlen; use platform; use platform::types::*; -use platform::{c_str, errno, ReadByte, WriteByte}; +use platform::{errno, ReadByte, WriteByte}; use platform::{Pal, Sys}; mod printf; @@ -739,7 +739,8 @@ pub extern "C" fn pclose(_stream: &mut FILE) -> c_int { #[no_mangle] pub unsafe extern "C" fn perror(s: *const c_char) { - let s_str = str::from_utf8_unchecked(c_str(s)); + let s_cstr = CStr::from_ptr(s); + let s_str = str::from_utf8_unchecked(s_cstr.to_bytes()); let mut w = platform::FileWriter(2); if errno >= 0 && errno < STR_ERROR.len() as c_int { diff --git a/src/header/stdio/printf.rs b/src/header/stdio/printf.rs index 55b0c6b6155dc72c5041b28d95cd67fc15f168f2..e10ed03b3f3ede4269dfb7c89aede0a3943ee7ae 100644 --- a/src/header/stdio/printf.rs +++ b/src/header/stdio/printf.rs @@ -1,6 +1,7 @@ use core::fmt::Write as CoreWrite; use core::{ptr, slice, str}; +use c_str::CStr; use platform::types::*; use platform::{self, WriteByte}; use va_list::VaList; @@ -62,7 +63,8 @@ pub unsafe fn printf<W: WriteByte>(w: W, format: *const c_char, mut ap: VaList) found_percent = false; if a != ptr::null() { - w.write_str(str::from_utf8_unchecked(platform::c_str(a))) + let a_cstr = CStr::from_ptr(a); + w.write_str(str::from_utf8_unchecked(a_cstr.to_bytes())) } else { w.write_str("NULL") } diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs index a4a76cbb9cfdc6f9ab1ad9164e97fec08fcfeff4..fd2a3b91187265df9ff6bf6d2691ef9edd0f05cc 100644 --- a/src/header/string/mod.rs +++ b/src/header/string/mod.rs @@ -246,7 +246,14 @@ pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t { #[no_mangle] pub unsafe extern "C" fn strnlen(s: *const c_char, size: usize) -> size_t { - platform::c_str_n(s, size).len() as size_t + let mut i = 0; + while i < size { + if *s.offset(i as isize) == 0 { + break; + } + i += 1; + } + i as size_t } #[no_mangle] diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 643d58978fe09277ab37bc16f356a5e541f52d7b..08a6f4d1dc50097d2ec99fb0c0fb911e18fae335 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -141,8 +141,9 @@ impl Pal for Sys { } fn fstat(fildes: c_int, buf: *mut stat) -> c_int { - let empty_cstr: *const c_char = unsafe { super::cstr_from_bytes_with_nul_unchecked(b"\0") }; - e(unsafe { syscall!(NEWFSTATAT, fildes, empty_cstr, buf, AT_EMPTY_PATH) }) as c_int + let empty = b"\0"; + let empty_ptr = empty.as_ptr() as *const c_char; + e(unsafe { syscall!(NEWFSTATAT, fildes, empty_ptr, buf, AT_EMPTY_PATH) }) as c_int } fn fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int { diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 9334c7a65703e8c34e595ba8a452eea8751d11fd..e492bc6338a900586da913e823e3ae4f501de243 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -46,53 +46,6 @@ pub static mut environ: *mut *mut c_char = ptr::null_mut(); #[allow(non_upper_case_globals)] pub static mut inner_environ: Vec<*mut c_char> = Vec::new(); -pub unsafe fn c_str_mut<'a>(s: *mut c_char) -> &'a mut [u8] { - use core::usize; - - c_str_n_mut(s, usize::MAX) -} - -pub unsafe fn c_str_n_mut<'a>(s: *mut c_char, n: usize) -> &'a mut [u8] { - assert!(s != ptr::null_mut()); - use core::slice; - - let mut size = 0; - - for _ in 0..n { - if *s.offset(size) == 0 { - break; - } - size += 1; - } - - slice::from_raw_parts_mut(s as *mut u8, size as usize) -} -pub unsafe fn c_str<'a>(s: *const c_char) -> &'a [u8] { - use core::usize; - - c_str_n(s, usize::MAX) -} - -pub unsafe fn c_str_n<'a>(s: *const c_char, n: usize) -> &'a [u8] { - assert!(s != ptr::null()); - use core::slice; - - let mut size = 0; - - for _ in 0..n { - if *s.offset(size) == 0 { - break; - } - size += 1; - } - - slice::from_raw_parts(s as *const u8, size as usize) -} - -pub unsafe fn cstr_from_bytes_with_nul_unchecked(bytes: &[u8]) -> *const c_char { - bytes.as_ptr() as *const c_char -} - // NOTE: defined here rather than in string because memcpy() is useful in multiple crates pub unsafe fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_void { let mut i = 0; diff --git a/tests/Makefile b/tests/Makefile index 0ef4b99aaf1f75dba1d3a6bb35c00e27db3a3822..ab914b7798a27b164e823cc06924171fd5d50977 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -76,12 +76,10 @@ BINS=\ $(EXPECT_BINS) \ dirent \ pwd \ - resource/getrusage \ stdlib/alloc \ stdlib/bsearch \ stdlib/mktemp \ time/gettimeofday \ - time/times \ unistd/chdir \ unistd/getcwd \ unistd/gethostname \ @@ -89,6 +87,8 @@ BINS=\ unistd/link \ unistd/setid \ unistd/stat +# resource/getrusage +# time/times .PHONY: all $(BINS) clean run expected verify