From a8aeb1a3c57d0b8a5fcb3f80f4c3bb3d801a3fc0 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Tue, 14 Oct 2014 14:07:55 -0700 Subject: [PATCH] Implement strncmp --- include/string.h | 1 + sys/kern/libc.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/string.h b/include/string.h index 8e87086..c4a93bd 100644 --- a/include/string.h +++ b/include/string.h @@ -12,6 +12,7 @@ char *strchr(const char *s, int c); int strcmp(const char *s1, const char *s2); char *strcpy(char *to, const char *from); size_t strlen(const char *str); +int strncmp(const char *s1, const char *s2, size_t len); char *strncpy(char *to, const char *from, size_t len); #endif /* __STRING_H__ */ diff --git a/sys/kern/libc.c b/sys/kern/libc.c index 16b4336..ec3730b 100644 --- a/sys/kern/libc.c +++ b/sys/kern/libc.c @@ -53,6 +53,27 @@ strcmp(const char *s1, const char *s2) return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1)); } +int +strncmp(const char *s1, const char *s2, size_t len) +{ + if (len == 0) + return 0; + + while (*s1 == *s2) { + if (*s1 == 0) + return 0; + + s1++; + s2++; + + len--; + if (len == 0) + return 0; + } + + return (*(const uint8_t *)s1 - *(const uint8_t *)s2); +} + size_t strlen(const char *str) {