Commit 9325183b authored by Jeremy Soller's avatar Jeremy Soller

Fix strcat

parent d66afa45
......@@ -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
}
......
......@@ -46,6 +46,7 @@ EXPECT_BINS=\
stdlib/strtoul \
stdlib/system \
string/mem \
string/strcat \
string/strchr \
string/strcpy \
string/strcspn \
......
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char dest1[12] = "hello";
printf("%s\n", strcat(dest1, " world")); // should be hello world
char dest2[12] = "hello";
printf("%s\n", strncat(dest2, " world foobar", 6)); // should be hello world
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment