aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-03-28 20:03:38 +0300
committeraqua <aqua@iserlohn-fortress.net>2022-08-12 10:13:59 +0300
commitedf9e71e2a7b6b89775c29cf28c19c6b89992c25 (patch)
tree3adbf944d9e47a743063487c4facb7eed1fbdee0 /lib
downloadkernel-edf9e71e2a7b6b89775c29cf28c19c6b89992c25.tar.xz
Initial commit
x86 kernel that prints a hello world message to com1
Diffstat (limited to 'lib')
-rw-r--r--lib/memcpy.c20
-rw-r--r--lib/memset.c14
-rw-r--r--lib/stdio.h3
-rw-r--r--lib/stdio/printf.c38
-rw-r--r--lib/string.h3
-rw-r--r--lib/string/itoa.c13
6 files changed, 91 insertions, 0 deletions
diff --git a/lib/memcpy.c b/lib/memcpy.c
new file mode 100644
index 0000000..c648501
--- /dev/null
+++ b/lib/memcpy.c
@@ -0,0 +1,20 @@
+/**
+ * The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap.
+ * @param dest
+ * @param src
+ * @param n
+ * @return
+ */
+void *
+memcpy(void *restrict dest, const void *restrict src, unsigned n)
+{
+ char *pDest = (char *)dest;
+ const char *pSrc = (const char *)src;
+
+ while (n) {
+ *(pDest++) = *(pSrc++);
+ --n;
+ }
+
+ return dest;
+} \ No newline at end of file
diff --git a/lib/memset.c b/lib/memset.c
new file mode 100644
index 0000000..442a305
--- /dev/null
+++ b/lib/memset.c
@@ -0,0 +1,14 @@
+/**
+ * The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
+ * @param s
+ * @param c
+ * @param n
+ * @return
+ */
+void *
+memset(void *s, char c, unsigned n)
+{
+ char *pDest = (char *)s;
+ for (unsigned i = 0; i < n; ++i) pDest[i] = c;
+ return s;
+} \ No newline at end of file
diff --git a/lib/stdio.h b/lib/stdio.h
new file mode 100644
index 0000000..cf3a33f
--- /dev/null
+++ b/lib/stdio.h
@@ -0,0 +1,3 @@
+#pragma once
+
+int printf(const char *restrict format, ...);
diff --git a/lib/stdio/printf.c b/lib/stdio/printf.c
new file mode 100644
index 0000000..f6b8ef6
--- /dev/null
+++ b/lib/stdio/printf.c
@@ -0,0 +1,38 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <devices/uart_16550.h>
+
+int
+printf(const char *restrict format, ...)
+{
+ int written = 0;
+ va_list params;
+ va_start(params, format);
+
+ int s = 0;
+ int l = 0;
+ for (int i = 0; format[i] != '\0'; ++i) {
+ if (format[i] == '%') {
+ written += uart_puts(COM1, &format[s], l);
+ s = i + 2;
+ ++i;
+
+ switch (format[i]) {
+ case 's':
+ written += uart_puts(COM1, va_arg(params, const char *), -1);
+ break;
+ }
+
+ l = 0;
+ }
+
+ else
+ ++l;
+ }
+
+ if (l > 0) written += uart_puts(COM1, &format[s], l);
+
+ va_end(params);
+ return written;
+}
diff --git a/lib/string.h b/lib/string.h
new file mode 100644
index 0000000..361d308
--- /dev/null
+++ b/lib/string.h
@@ -0,0 +1,3 @@
+#pragma once
+
+char *itoa(char *p, unsigned x);
diff --git a/lib/string/itoa.c b/lib/string/itoa.c
new file mode 100644
index 0000000..5eefb37
--- /dev/null
+++ b/lib/string/itoa.c
@@ -0,0 +1,13 @@
+#include "string.h"
+
+char *
+itoa(char *p, unsigned x)
+{
+ p += 3 * sizeof(int);
+ *--p = 0;
+ do {
+ *--p = '0' + x % 10;
+ x /= 10;
+ } while (x);
+ return p;
+}