diff mbox series

[XTF,4/4] libc: add strncmp() function

Message ID 20200423101918.13566-5-wipawel@amazon.de (mailing list archive)
State New, archived
Headers show
Series Add strncmp(), strtol() and strtoul() functions | expand

Commit Message

Wieczorkiewicz, Pawel April 23, 2020, 10:19 a.m. UTC
Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
---
 common/libc/string.c | 11 +++++++++++
 include/xtf/libc.h   |  3 +++
 2 files changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/common/libc/string.c b/common/libc/string.c
index 5c25e27..d81e324 100644
--- a/common/libc/string.c
+++ b/common/libc/string.c
@@ -56,6 +56,17 @@  int (strcmp)(const char *_s1, const char *_s2)
     return (s1 < s2) ? -1 : (s1 > s2);
 }
 
+int (strncmp)(const char *_s1, const char *_s2, size_t n)
+{
+    size_t ctr;
+    for (ctr = 0; ctr < n; ctr++) {
+        if (_s1[ctr] != _s2[ctr])
+            return (int)(_s1[ctr] - _s2[ctr]);
+    }
+
+    return 0;
+}
+
 void *(memset)(void *s, int c, size_t n)
 {
     char *p = s;
diff --git a/include/xtf/libc.h b/include/xtf/libc.h
index 75f193b..c3783a8 100644
--- a/include/xtf/libc.h
+++ b/include/xtf/libc.h
@@ -26,6 +26,9 @@  char *strncpy(char *dst, const char *src, size_t n);
 int strcmp(const char *s1, const char *s2);
 #define strcmp(s1, s2)              __builtin_strcmp(s1, s2)
 
+int strncmp(const char *s1, const char *s2, size_t n);
+#define strncmp(s1, s2, n)          __builtin_strncmp(s1, s2, n)
+
 void *memset(void *s, int c, size_t n);
 #define memset(d, c, n)             __builtin_memset(d, c, n)