From d94b024b73aeca06de417e0fd3c502495312a8b2 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 7 Dec 2019 13:40:28 +0100 Subject: Added userspace libc and began userspace based shell --- src/kernel/lib/string/strcat.c | 10 ++++++++++ src/kernel/lib/string/strcati.c | 8 ++++++++ src/kernel/lib/string/strcmp.c | 10 ++++++++++ src/kernel/lib/string/strcpy.c | 9 +++++++++ src/kernel/lib/string/strdisp.c | 12 ++++++++++++ src/kernel/lib/string/strdup.c | 10 ++++++++++ src/kernel/lib/string/strinv.c | 13 +++++++++++++ src/kernel/lib/string/strlen.c | 8 ++++++++ 8 files changed, 80 insertions(+) create mode 100644 src/kernel/lib/string/strcat.c create mode 100644 src/kernel/lib/string/strcati.c create mode 100644 src/kernel/lib/string/strcmp.c create mode 100644 src/kernel/lib/string/strcpy.c create mode 100644 src/kernel/lib/string/strdisp.c create mode 100644 src/kernel/lib/string/strdup.c create mode 100644 src/kernel/lib/string/strinv.c create mode 100644 src/kernel/lib/string/strlen.c (limited to 'src/kernel/lib/string') diff --git a/src/kernel/lib/string/strcat.c b/src/kernel/lib/string/strcat.c new file mode 100644 index 0000000..e0d6885 --- /dev/null +++ b/src/kernel/lib/string/strcat.c @@ -0,0 +1,10 @@ +#include + +void strcat(char *dest, const char *orig) +{ + size_t s_dest = strlen(dest); + size_t s_orig = strlen(orig); + + for (size_t i = 0; i < s_orig; i++) dest[s_dest + i] = orig[i]; + dest[s_dest + s_orig] = 0; +} \ No newline at end of file diff --git a/src/kernel/lib/string/strcati.c b/src/kernel/lib/string/strcati.c new file mode 100644 index 0000000..9ee3a43 --- /dev/null +++ b/src/kernel/lib/string/strcati.c @@ -0,0 +1,8 @@ +#include + +void strcati(char *dest, const char *orig) +{ + size_t s_orig = strlen(orig); + strdisp(dest, (int) s_orig); + for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; +} \ No newline at end of file diff --git a/src/kernel/lib/string/strcmp.c b/src/kernel/lib/string/strcmp.c new file mode 100644 index 0000000..b8ac2a7 --- /dev/null +++ b/src/kernel/lib/string/strcmp.c @@ -0,0 +1,10 @@ +#include + +char strcmp(const char *a, const char *b) +{ + if (strlen(a) != strlen(b)) return 1; + + for (size_t i = 0; i < strlen(a); i++) if (a[i] != b[i]) return 1; + + return 0; +} \ No newline at end of file diff --git a/src/kernel/lib/string/strcpy.c b/src/kernel/lib/string/strcpy.c new file mode 100644 index 0000000..bab8535 --- /dev/null +++ b/src/kernel/lib/string/strcpy.c @@ -0,0 +1,9 @@ +#include + +void strcpy(char *dest, const char *orig) +{ + size_t s_orig = strlen(orig); + + for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; + dest[s_orig] = 0; +} \ No newline at end of file diff --git a/src/kernel/lib/string/strdisp.c b/src/kernel/lib/string/strdisp.c new file mode 100644 index 0000000..36c03c3 --- /dev/null +++ b/src/kernel/lib/string/strdisp.c @@ -0,0 +1,12 @@ +#include + +void strdisponce(char *str) +{ + for (size_t i = sizeof(str) + 2; i > 0; i--) str[i] = str[i - 1]; + str[0] = 0; +} + +void strdisp(char *str, int n) +{ + for (int i = 0; i < n; i++) strdisponce(str); +} \ No newline at end of file diff --git a/src/kernel/lib/string/strdup.c b/src/kernel/lib/string/strdup.c new file mode 100644 index 0000000..9330144 --- /dev/null +++ b/src/kernel/lib/string/strdup.c @@ -0,0 +1,10 @@ +#include +#include + +char *strdup(const char *orig) +{ + size_t s_orig = strlen(orig); + char *ret = kmalloc(s_orig + 1); + strcpy(ret, orig); + return ret; +} \ No newline at end of file diff --git a/src/kernel/lib/string/strinv.c b/src/kernel/lib/string/strinv.c new file mode 100644 index 0000000..b4b4212 --- /dev/null +++ b/src/kernel/lib/string/strinv.c @@ -0,0 +1,13 @@ +#include + +void strinv(char *str) +{ + size_t s_str = strlen(str); + + int iterations = (int) s_str / 2; + for (int i = 0; i < iterations; i++) { + char aux = str[i]; + str[i] = str[(s_str - i) - 1]; + str[(s_str - i) - 1] = aux; + } +} \ No newline at end of file diff --git a/src/kernel/lib/string/strlen.c b/src/kernel/lib/string/strlen.c new file mode 100644 index 0000000..77e3c00 --- /dev/null +++ b/src/kernel/lib/string/strlen.c @@ -0,0 +1,8 @@ +#include + +size_t strlen(const char *str) +{ + size_t len = 0; + while (str[len]) len++; + return len; +} \ No newline at end of file -- cgit v1.2.3