Added VFS and Ext2 support. Optimized attributes of methods to improve code highlighting. Printf attribute, malloc attribute, etc.
148 lines
3.2 KiB
C
148 lines
3.2 KiB
C
//
|
|
// Created by rick on 01-02-21.
|
|
//
|
|
|
|
#include <string.h>
|
|
#include <sys/param.h>
|
|
#include <sys/types.h>
|
|
#include <myke/mem/malloc.h>
|
|
|
|
void *memcpy(void *dst, const void *src, size_t amount) {
|
|
for (size_t i = 0; i < amount; i++) {
|
|
((char *) dst)[i] = ((const char *) src)[i];
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
void *memset(void *dst, int data, size_t amount) {
|
|
for (size_t i = 0; i < amount; ++i) {
|
|
((char *) dst)[i] = (char) data;
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
void *memmove(void *dst, const void *src, size_t amount) {
|
|
void *tmp = malloc(amount);
|
|
if (tmp == NULL) {
|
|
return NULL;
|
|
}
|
|
memcpy(dst, memcpy(tmp, src, amount), amount);
|
|
free(tmp);
|
|
return dst;
|
|
}
|
|
|
|
void *strcpy(char *dst, const char *src) {
|
|
return memcpy(dst, src, strlen(src) + 1);
|
|
}
|
|
|
|
void *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) {
|
|
length++;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
int strcmp(const char *s1, const char *s2) {
|
|
size_t len1 = strlen(s1);
|
|
size_t len2 = strlen(s2);
|
|
return strncmp(s1, s2, MAX(len1, len2));
|
|
}
|
|
|
|
char *strchr(const char *s, char c) {
|
|
size_t index = 0;
|
|
while (1) {
|
|
if (s[index] == c) {
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
|
|
return &s[index];
|
|
#pragma clang diagnostic pop
|
|
}
|
|
if (s[index] == 0) {
|
|
return NULL;
|
|
}
|
|
index++;
|
|
}
|
|
}
|
|
|
|
char *strrchr(const char *s, char c) {
|
|
size_t index = strlen(s);
|
|
while (1) {
|
|
if (s[index] == c) {
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
|
|
return &s[index];
|
|
#pragma clang diagnostic pop
|
|
}
|
|
if (index == 0) {
|
|
return NULL;
|
|
}
|
|
index--;
|
|
}
|
|
}
|
|
|
|
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 (a < b) return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int strncmp(const char *s1, const char *s2, size_t n) {
|
|
for (size_t i = 0; i < n; ++i) {
|
|
if (s1[i] == 0 && s2[i] == 0) {
|
|
return 0;
|
|
}
|
|
if (s1[i] == 0) {
|
|
return -1;
|
|
}
|
|
if (s2[i] == 0) {
|
|
return 1;
|
|
}
|
|
if (s1[i] > s2[i]) {
|
|
return -1;
|
|
}
|
|
if (s1[i] < s2[i]) {
|
|
return 1;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
char *strdup(const char *s) {
|
|
return strndup(s, strlen(s));
|
|
}
|
|
|
|
char *strndup(const char *s, size_t n) {
|
|
char *new = malloc(n + 1);
|
|
memcpy(new, s, n);
|
|
new[n] = 0;
|
|
return new;
|
|
}
|