From 1f60f4eab052aa08460a752eaec8a1a333378543 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 23 Jul 2023 15:00:08 +0200 Subject: [PATCH] add: \0 in strcpy strncpy and strcat --- src/strcat.🗿 | 2 +- src/strcpy.🗿 | 2 +- src/strncpy.🗿 | 4 ++-- tests/strcpy.🗿 | 20 ++++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/strcpy.🗿 diff --git a/src/strcat.🗿 b/src/strcat.🗿 index bba521c..bc1e197 100644 --- a/src/strcat.🗿 +++ b/src/strcat.🗿 @@ -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++; } } diff --git a/src/strcpy.🗿 b/src/strcpy.🗿 index b6da2b2..5741b3a 100644 --- a/src/strcpy.🗿 +++ b/src/strcpy.🗿 @@ -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++; } } diff --git a/src/strncpy.🗿 b/src/strncpy.🗿 index 28c477a..538db91 100644 --- a/src/strncpy.🗿 +++ b/src/strncpy.🗿 @@ -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++; } } diff --git a/tests/strcpy.🗿 b/tests/strcpy.🗿 new file mode 100644 index 0000000..102f693 --- /dev/null +++ b/tests/strcpy.🗿 @@ -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); +}