feat: some extensions to kernel standard c library. Other minor

improvements and formats
This commit is contained in:
2021-03-26 19:57:07 +01:00
parent 20ab9e1d6e
commit f120b104e5
10 changed files with 221 additions and 19 deletions

View File

@@ -4,6 +4,7 @@
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
int memcpy(void *dst, const void *src, size_t amount) {
for (size_t i = 0; i < amount; i++) {
@@ -24,6 +25,10 @@ int strcpy(char *dst, const char *src) {
return memcpy(dst, src, strlen(src) + 1);
}
int strncpy(char *dst, const char *src, size_t n) {
return memcpy(dst, src, MIN(strlen(src), n) + 1);
}
size_t strlen(const char *str) {
int length = 0;
while (str[length] != 0) {
@@ -51,6 +56,17 @@ const char *strchr(const char *s, char c) {
}
}
int memcmp(const void *s1, const void *s2, size_t n) {
uint8_t a, b;
for (size_t i = 0; i < n; ++i) {
a = ((uint8_t *) s1)[i];
b = ((uint8_t *) s2)[i];
if (a > b) return 1;
if (b < a) return -1;
}
return 0;
}
int strncmp(const char *s1, const char *s2, int n) {
for (int i = 0; i < n; ++i) {
if (s1[i] == 0 && s2[i] == 0) {
@@ -71,3 +87,21 @@ int strncmp(const char *s1, const char *s2, int n) {
}
return 0;
}
char *strcat(char *dest, const char *src) {
size_t count = strlen(src);
return strncat(dest, src, count);
}
char *strncat(char *dest, const char *src, size_t n) {
size_t start = strlen(dest);
for (size_t index = 0; index < n; index++) {
char val = src[index];
dest[start + index] = val;
if (val == 0) {
dest[start + index] = 0;
break;
}
}
return dest;
}