about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2020-06-24 03:59:26 +0200
committerYour Name <you@example.com>2020-06-24 03:59:26 +0200
commitc5b40777dd47654f0a381327467c54bc31f03c85 (patch)
tree26b787857767302199c4c5692a33b7a741809631
parentPower: added temp ACPI interface (diff)
downloadpOS-c5b40777dd47654f0a381327467c54bc31f03c85.tar.gz
pOS-c5b40777dd47654f0a381327467c54bc31f03c85.tar.bz2
pOS-c5b40777dd47654f0a381327467c54bc31f03c85.zip
stdlib: add memcmp() and strncmp()
-rw-r--r--src/pOS/arch/x86/libc/string/memcmp.cpp14
-rw-r--r--src/pOS/arch/x86/libc/string/strncmp.cpp9
-rw-r--r--src/pOS/include/libc/string.h2
3 files changed, 25 insertions, 0 deletions
diff --git a/src/pOS/arch/x86/libc/string/memcmp.cpp b/src/pOS/arch/x86/libc/string/memcmp.cpp
new file mode 100644
index 0000000..ee5ee35
--- /dev/null
+++ b/src/pOS/arch/x86/libc/string/memcmp.cpp
@@ -0,0 +1,14 @@
+#include <string.h>
+
+int memcmp(const void* ptr1, const void* ptr2, size_t size)
+{
+    int result = 0;
+    const uint8_t* p1 = static_cast<const uint8_t*>(ptr1);
+    const uint8_t* p2 = static_cast<const uint8_t*>(ptr2);
+
+    for(size_t i = 0; i < size; i++)
+        if(*p1++ != *p2++)
+            return *--p1 - *--p2;
+
+    return result;
+}
diff --git a/src/pOS/arch/x86/libc/string/strncmp.cpp b/src/pOS/arch/x86/libc/string/strncmp.cpp
new file mode 100644
index 0000000..a1b8379
--- /dev/null
+++ b/src/pOS/arch/x86/libc/string/strncmp.cpp
@@ -0,0 +1,9 @@
+#include <string.h>
+
+int strncmp(const char* str1, const char* str2, size_t len)
+{
+    size_t strln = strlen(str1);
+    strln = strln > strlen(str2) ? strlen(str2) : strln;
+    strln = strln > len ? len : strln;
+    return memcmp(str1, str2, strln);
+}
diff --git a/src/pOS/include/libc/string.h b/src/pOS/include/libc/string.h
index 27b496c..66f93ed 100644
--- a/src/pOS/include/libc/string.h
+++ b/src/pOS/include/libc/string.h
@@ -7,7 +7,9 @@
 size_t strlen(const char* str);
 void* memset(void* buf, int value, size_t size);
 void* memcpy(void* dst, const void* src, size_t size);
+int memcmp(const void* ptr1, const void* ptr2, size_t size);
 char* strncpy(char* dst, const char* src, size_t len);
 char* strncat(char *dest, const char *src, size_t len);
+int strncmp(const char* str1, const char* str2, size_t len);
 
 #endif