vbos/lib/string.c
Michael Shaw c46976eb1f string: fix strncmp
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-05 23:15:11 +02:00

243 lines
3.7 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/string.h>
void *memcpy(void *dest, const void *src, size_t num)
{
char *d = (char *)dest;
char *s = (char *)src;
for (size_t i = 0; i < num; i++) {
*d++ = *s++;
}
return dest;
}
int strcmp(const char *a, const char *b)
{
while (*a && *a == *b) {
a++;
b++;
}
return *a - *b;
}
int strncmp(const char *a, const char *b, size_t n)
{
if (n == 0)
return 0;
for (size_t i = 0; i < n; i++) {
unsigned char char_a = (unsigned char)a[i];
unsigned char char_b = (unsigned char)b[i];
if (char_a != char_b || char_a == '\0') {
return (int)char_a - (int)char_b;
}
}
return 0;
}
char *strcpy(char *dest, const char *src)
{
while ((*dest++ = *src++))
;
return dest;
}
char *strncpy(char *dest, const char *src, size_t n)
{
for (size_t i = 0; i < n; i++) {
dest[i] = src[i];
if (!src[i])
break;
}
return dest;
}
size_t strlen(const char *c)
{
const char *b = c;
while (*b)
b++;
return b - c;
}
int memcmp(const void *aa, const void *bb, size_t num)
{
const char *a = (const char *)aa;
const char *b = (const char *)bb;
for (size_t i = 0; i < num; i++) {
if (*a++ != *b++)
return a - b;
}
return 0;
}
void *memset(void *str, int c, size_t n)
{
vaddr_t ptr = (vaddr_t)str;
vaddr_t val = (vaddr_t)c;
val |= val << 8;
val |= val << 16;
val |= val << 32;
for (size_t i = 0; i < (n >> 3); i++) {
*(vaddr_t *)ptr = val;
ptr += 8;
}
if (n & 7) {
ptr = (vaddr_t)str + (n & (~7ull));
for (size_t i = 0; i < (n & 7); i++) {
*(char *)ptr = c;
ptr++;
}
}
return str;
}
int atoi(const char *a)
{
int ndigits = strlen(a);
int b = 1;
int ret = 0;
for (int i = ndigits - 1; i >= 0; i--) {
ret += b * (a[i] - '0');
b *= 10;
}
return ret;
}
char *strchr(const char *s, int c)
{
while (*s) {
if (*s == c)
return (char *)s;
s++;
}
return NULL;
}
char *strtok(char *str, const char *delim, char **llast)
{
if (str)
(*llast) = str;
if (!(*llast))
return NULL;
char *ret = (*llast);
while (*(*llast)) {
if (strchr(delim, *(*llast))) {
*(*llast) = 0;
(*llast)++;
return ret;
}
(*llast)++;
}
if (!(*ret))
return NULL;
return ret;
}
char *strdup(const char *s)
{
size_t len = strlen(s);
char *ret = kalloc(len + 1, ALLOC_KERN);
if (!ret)
return NULL;
ret[len] = 0;
strcpy(ret, s);
return ret;
}
bool strempty(const char *s)
{
if (!s)
return true;
if (!*s)
return true;
size_t i = strlen(s);
while (i--) {
if (s[i] != ' ')
return false;
}
return true;
}
static char *udirname(char *path)
{
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return ".";
if (last_slash_idx == 0)
return "/";
path[last_slash_idx] = '\0';
return path;
}
static char *ubasename(char *path)
{
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return path;
if (path[last_slash_idx + 1] == '\0' && last_slash_idx == 0)
return "/";
return &path[last_slash_idx + 1];
}
char *dirname(const char *path)
{
if (!path)
return strdup(".");
char *copy = strdup(path);
char *res = udirname(copy);
char *ret = strdup(res);
kfree(copy);
return ret;
}
char *basename(const char *path)
{
if (!path)
return strdup(".");
char *copy = strdup(path);
char *res = ubasename(copy);
char *ret = strdup(res);
kfree(copy);
return ret;
}