feature: a lot of stuff and got to a working command line ish
This commit is contained in:
60
kernel/libc/string.c
Normal file
60
kernel/libc/string.c
Normal file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by rick on 01-02-21.
|
||||
//
|
||||
|
||||
#include "string.h"
|
||||
#include "stdbool.h"
|
||||
#include <libc/libc.h>
|
||||
#include <types.h>
|
||||
|
||||
int strcpy(char *dst, char *src) {
|
||||
return memcpy(dst, src, strlen(src) + 1);
|
||||
}
|
||||
|
||||
int strlen(const char *str) {
|
||||
int length = 0;
|
||||
while (str[length] != 0) {
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
int strcmp(const char *s1, const char *s2) {
|
||||
int len1 = strlen(s1);
|
||||
int len2 = strlen(s1);
|
||||
return strncmp(s1, s2, maxi(len1, len2));
|
||||
}
|
||||
|
||||
const char *strchr(const char *s, char c) {
|
||||
int index = 0;
|
||||
while (true) {
|
||||
if (s[index] == c) {
|
||||
return &s[index];
|
||||
}
|
||||
if (s[index] == 0) {
|
||||
return NULL;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
int strncmp(const char *s1, const char *s2, int n) {
|
||||
for (int 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 (s2[i] < s1[i]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user