Improve
This commit is contained in:
parent
b462a19bd4
commit
32adedb63b
@ -23,16 +23,17 @@ unsafe extern "C" fn fclose(stream: *const ()) -> i32 {
|
|||||||
|
|
||||||
// We cannot just use the ROM function since it needs to allocate memory
|
// We cannot just use the ROM function since it needs to allocate memory
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
unsafe extern "C" fn strdup(str: *const i8) -> *const u8 {
|
unsafe extern "C" fn strdup(str: *const core::ffi::c_char) -> *const core::ffi::c_char {
|
||||||
trace!("strdup {:?}", str);
|
trace!("strdup {:?}", str);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let s = core::ffi::CStr::from_ptr(str);
|
let s = core::ffi::CStr::from_ptr(str);
|
||||||
let s = s.to_str().unwrap();
|
let len = s.count_bytes() + 1;
|
||||||
|
let p = malloc(len);
|
||||||
let p = malloc(s.len() + 1);
|
if !p.is_null() {
|
||||||
core::ptr::copy_nonoverlapping(str, p as *mut i8, s.len() + 1);
|
core::ptr::copy_nonoverlapping(str, p as *mut i8, len);
|
||||||
p as *const u8
|
}
|
||||||
|
p.cast()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user