Implemented getenv(), atexit(), number parsing, and more
This commit is contained in:
14
photon/libc/string/memcmp.c
Normal file
14
photon/libc/string/memcmp.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stddef.h>
|
||||
|
||||
int memcmp(const void *a, const void *b, size_t n)
|
||||
{
|
||||
const unsigned char *s1 = a, *s2 = b;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (s1[i] != s2[i]) {
|
||||
return ((s1[i] < s2[i]) ? -1 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
photon/libc/string/memmove.c
Normal file
27
photon/libc/string/memmove.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <string.h>
|
||||
|
||||
#define ALIGNED(p) (!((long)p & (sizeof(long) - 1)))
|
||||
#define QUADBLOCKSIZE (sizeof(long) << 2)
|
||||
#define BLOCKSIZE (sizeof(long))
|
||||
|
||||
static void *memcpy_r(void *dest, const void *src, size_t sz)
|
||||
{
|
||||
unsigned char *d = dest;
|
||||
const unsigned char *s = src;
|
||||
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
size_t b = sz - i - 1;
|
||||
d[b] = s[b];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void *memmove(void *dest, const void *src, size_t n)
|
||||
{
|
||||
if (dest > src) {
|
||||
return memcpy(dest, src, n);
|
||||
} else {
|
||||
return memcpy_r(dest, src, n);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,25 @@
|
||||
#include <stddef.h>
|
||||
|
||||
int strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; s1[i] == s2[i]; i++)
|
||||
if (s1[i] == '\0')
|
||||
for (i = 0; s1[i] == s2[i]; i++) {
|
||||
if (s1[i] == '\0') {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return s1[i] - s2[i];
|
||||
}
|
||||
|
||||
int strncmp(const char *s1, const char *s2, unsigned int n)
|
||||
int strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
for (; n > 0; s1++, s2++, --n)
|
||||
if (*s1 != *s2)
|
||||
for (; n > 0; s1++, s2++, --n) {
|
||||
if (*s1 != *s2) {
|
||||
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : 1);
|
||||
else if (*s1 == '\0')
|
||||
} else if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user