From 556843458a3b38f1efe536fdb7ff3b0f65538e0f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 12 Aug 2020 16:09:32 -0400 Subject: fix to be solf-contained and more reliable --- src/string/strcmp.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/string/strcmp.c b/src/string/strcmp.c index fdceeb67..f8863fba 100644 --- a/src/string/strcmp.c +++ b/src/string/strcmp.c @@ -4,21 +4,32 @@ /** compare strings **/ int strcmp(const char *s1, const char *s2) { - size_t n1 = 0; - size_t n2 = 0; - ASSERT_NONNULL(s1); ASSERT_NONNULL(s2); - n1 = strlen(s1); - n2 = strlen(s2); + while (*s1 && *s2) { + if (*s1 != *s2) { + return *s1 - *s2; + } + s1++; + s2++; + } /* RETURN(NEGATIVE, ARGUMENT(s1) is less than ARGUMENT(s2)); RETURN(ZERO, ARGUMENT(s1) is equal to ARGUMENT(s2)); RETURN(POSITIVE, ARGUMENT(s1) is greater than ARGUMENT(s2)); */ - return strncmp(s1, s2, n1 < n2 ? n1 : n2); + + if (*s1) { + return -1; + } + + if (*s2) { + return 1; + } + + return 0; } /*** -- cgit v1.2.1