lib/libc: move some functions to libc.c

This commit is contained in:
xvanc 2024-05-31 12:18:38 -05:00
parent b5c6bdcf10
commit 7ac5935b5d
No known key found for this signature in database
2 changed files with 45 additions and 42 deletions

45
common/lib/libc.c Normal file
View File

@ -0,0 +1,45 @@
#include <stddef.h>
#include <stdint.h>
#include <lib/libc.h>
size_t strnlen(const char *str, size_t maxlen) {
size_t len;
for (len = 0; len < maxlen && str[len]; len++);
return len;
}
void *memchr(const void *ptr, int ch, size_t n) {
uint8_t *p = (uint8_t *)ptr;
for (size_t i = 0; i < n; i++) {
if (p[i] == ch) {
return (void *)ptr + i;
}
}
return NULL;
}
char *strchr(const char *str, int ch) {
for (size_t i = 0; str[i]; i++) {
if (str[i] == ch) {
return (char *)str + i;
}
}
return NULL;
}
char *strrchr(const char *str, int ch) {
char *p = NULL;
for (size_t i = 0; str[i]; i++) {
if (str[i] == ch) {
p = (char *)str + i;
}
}
return p;
}

View File

@ -112,48 +112,6 @@ size_t strlen(const char *str) {
return len;
}
size_t strnlen(const char *str, size_t maxlen) {
size_t len;
for (len = 0; len < maxlen && str[len]; len++);
return len;
}
void *memchr(const void *ptr, int ch, size_t n) {
uint8_t *p = (uint8_t *)ptr;
for (size_t i = 0; i < n; i++) {
if (p[i] == ch) {
return (void *)ptr + i;
}
}
return NULL;
}
char *strchr(const char *str, int ch) {
for (size_t i = 0; str[i]; i++) {
if (str[i] == ch) {
return (char *)str + i;
}
}
return NULL;
}
char *strrchr(const char *str, int ch) {
char *p = NULL;
for (size_t i = 0; str[i]; i++) {
if (str[i] == ch) {
p = (char *)str + i;
}
}
return p;
}
int inet_pton(const char *src, void *dst) {
uint8_t array[4];
const char *current = src;