lib: add: memcmp

This commit is contained in:
starnakin 2024-09-10 16:37:18 +02:00
parent 8958b87f65
commit 07ff7f6087
2 changed files with 14 additions and 0 deletions

View File

@ -8,3 +8,4 @@ int strncmp(const char *s1, const char *s2, size_t n);
size_t strlen(const char *str); size_t strlen(const char *str);
char *strstr(const char *haystack, const char *needle); char *strstr(const char *haystack, const char *needle);
void *memcpy(void *dest, const void *src, size_t n); void *memcpy(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);

View File

@ -0,0 +1,13 @@
#include <stddef.h>
int memcmp(const void *s1, const void *s2, size_t n)
{
const char *str1 = s1;
const char *str2 = s2;
size_t i;
if (n == 0)
return 0;
for (i = 0; str1[i] == str2[i] && i < n - 1; i++);
return str1[i] - str2[i];
}