Remove c_str functions, replace with CStr
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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()];
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user