Fix strcat

This commit is contained in:
Jeremy Soller
2018-12-01 08:55:02 -07:00
parent d66afa4586
commit 9325183b21
5 changed files with 28 additions and 11 deletions
+13 -11
View File
@@ -147,11 +147,6 @@ pub unsafe extern "C" fn memset(s: *mut c_void, c: c_int, n: size_t) -> *mut c_v
s
}
#[no_mangle]
pub unsafe extern "C" fn strcat(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
strncat(s1, s2, usize::MAX)
}
#[no_mangle]
pub unsafe extern "C" fn strchr(mut s: *const c_char, c: c_int) -> *mut c_char {
let c = c as c_char;
@@ -283,18 +278,25 @@ pub unsafe extern "C" fn strnlen(s: *const c_char, size: size_t) -> size_t {
i as size_t
}
#[no_mangle]
pub unsafe extern "C" fn strcat(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
strncat(s1, s2, usize::MAX)
}
#[no_mangle]
pub unsafe extern "C" fn strncat(s1: *mut c_char, s2: *const c_char, n: size_t) -> *mut c_char {
let mut idx = strlen(s1 as *const _) as isize;
for i in 0..n as isize {
if *s2.offset(i) == 0 {
let len = strlen(s1 as *const c_char);
let mut i = 0;
while i < n {
let b = *s2.offset(i as isize);
if b == 0 {
break;
}
*s1.offset(idx) = *s2.offset(i);
idx += 1;
*s1.offset((len + i) as isize) = b;
i += 1;
}
*s1.offset(idx) = 0;
*s1.offset((len + i) as isize) = 0;
s1
}