From efde8c6c9eea3e78b1da80761c390a2afa066c3b Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Fri, 2 Nov 2018 18:09:28 +0100 Subject: [PATCH] [liba] Add strncmp --- liba/include/string.h | 1 + liba/src/strcmp.c | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/liba/include/string.h b/liba/include/string.h index ff58e0c8a..64a237111 100644 --- a/liba/include/string.h +++ b/liba/include/string.h @@ -13,6 +13,7 @@ void * memset(void * b, int c, size_t len); char * strchr(const char * s, int c); int strcmp(const char * s1, const char * s2); +int strncmp(const char * s1, const char * s2, size_t n); size_t strlcpy(char * dst, const char * src, size_t len); size_t strlen(const char * s); diff --git a/liba/src/strcmp.c b/liba/src/strcmp.c index cf72bf3f0..6df25bd06 100644 --- a/liba/src/strcmp.c +++ b/liba/src/strcmp.c @@ -7,3 +7,14 @@ int strcmp(const char *s1, const char *s2) { } return (*(unsigned char *)s1) - (*(unsigned char *)s2); } + +int strncmp(const char *s1, const char *s2, size_t n) { + while (n-- > 0) { + if (*s1 == NULL || *s2 != *s1) { + return (*(unsigned char *)s1) - (*(unsigned char *)s2); + } + s1++; + s2++; + } + return 0; +}