diff --git a/libs/libc/include/string.h b/libs/libc/include/string.h index f48aec91..a7697433 100644 --- a/libs/libc/include/string.h +++ b/libs/libc/include/string.h @@ -50,6 +50,12 @@ extern "C" /* Concatenates at most max bytes of the string src into dest. */ char* strncat(char* dest, const char* src, size_t max); + /* Compares strings a and b. You might prefer to use the safer strncmp function. */ + int strcmp(const char* a, const char* b); + + /* Compares at most max bytes of the strings a and b. */ + int strncmp(const char* a, const char* b, size_t max); + /* Returns the error string associated with the error number err. */ char* strerror(int err); diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index f0be3b87..443fee15 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -81,6 +81,27 @@ extern "C" return dest; } + int strcmp(const char* a, const char* b) + { + while (*a && (*a == *b)) + { + a++; + b++; + } + return *(const unsigned char*)a - *(const unsigned char*)b; + } + + int strncmp(const char* a, const char* b, size_t max) + { + const char* base = a; + while (*a && (*a == *b) && (size_t)(a - base) < (max - 1)) + { + a++; + b++; + } + return *(const unsigned char*)a - *(const unsigned char*)b; + } + char* strcat(char* dest, const char* src) { size_t dest_len = strlen(dest);