From 5a737917b00a76936c1b50b39ac95732fa5d0c1a Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 4 May 2018 12:21:52 +0900 Subject: [PATCH] strncmp --- base/usr/include/string.h | 1 + libc/string/strncmp.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 libc/string/strncmp.c diff --git a/base/usr/include/string.h b/base/usr/include/string.h index 1c568aaf..3f963a1a 100644 --- a/base/usr/include/string.h +++ b/base/usr/include/string.h @@ -25,6 +25,7 @@ extern char * strpbrk(const char * s, const char * b); extern char * strstr(const char * h, const char * n); extern int strcmp(const char * l, const char * r); +extern int strncmp(const char *s1, const char *s2, size_t n); extern size_t strcspn(const char * s, const char * c); extern size_t strspn(const char * s, const char * c); diff --git a/libc/string/strncmp.c b/libc/string/strncmp.c new file mode 100644 index 00000000..2bd0f810 --- /dev/null +++ b/libc/string/strncmp.c @@ -0,0 +1,13 @@ +#include + +int strncmp(const char *s1, const char *s2, size_t n) { + size_t i = 0; + while (i < n && *s1 && *s2) { + if (*s1 < *s2) return -1; + if (*s1 > *s2) return 1; + s1++; + s2++; + i++; + } + return 0; +}