add: \0 in strcpy strncpy and strcat

This commit is contained in:
starnakin 2023-07-23 15:00:08 +02:00
parent 357da752fc
commit 1f60f4eab0
4 changed files with 24 additions and 4 deletions

View File

@ -9,9 +9,9 @@ strcat(dst, src)
i++;
}
loop {
[dst + i + j] = [src + j];
if ([src + j] == 0)
return (dst);
[dst + i + j] = [src + j];
j++;
}
}

View File

@ -3,9 +3,9 @@ strcpy(dst, src)
local i = 0;
loop {
[dst + i] = [src + i];
if ([src + i] == 0)
return (dst);
[dst + i] = [src + i];
i++;
}
}

View File

@ -3,9 +3,9 @@ strncpy(dst, src, size)
local i = 0;
loop {
if ([src + i] == 0 | i == size)
return (dst);
[dst + i] = [src + i];
if ([src + i] == 0 | i + 1 >= size)
return (dst);
i++;
}
}

20
tests/strcpy.🗿 Normal file
View File

@ -0,0 +1,20 @@
main()
{
local str;
name = "strcpy";
str = galloc(10);
strcpy(str, "yo");
test_str(str, "yo", "");
strcpy(str, "");
test_str(str, "", "");
strcpy(str, "bonjorsss");
strcpy(str, "bonjour");
test_str(str, "bonjour", "set last \0");
free(str);
}