Remove c_str functions, replace with CStr

This commit is contained in:
Jeremy Soller
2018-09-24 21:08:29 -06:00
parent ef9fee5a2b
commit 7f14fcdee0
8 changed files with 29 additions and 62 deletions
+3 -2
View File
@@ -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;
+7 -5
View File
@@ -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()];
+3 -2
View File
@@ -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 {
+3 -1
View File
@@ -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")
}
+8 -1
View File
@@ -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]