summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2021-10-06 06:28:04 +0000
committerdzwdz2021-10-06 06:28:04 +0000
commita3469d648a4d0f276d979ceb14b8716b46f4f72e (patch)
tree1d529e3e229e55b6df4779254566121466102f64 /src
parent1ee734761a3eecbdaf9631a653dee668d2de26ef (diff)
init: printf base
Diffstat (limited to 'src')
-rw-r--r--src/init/main.c3
-rw-r--r--src/init/stdlib.c18
-rw-r--r--src/init/stdlib.h2
3 files changed, 23 insertions, 0 deletions
diff --git a/src/init/main.c b/src/init/main.c
index ec6d5be..a819152 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -1,3 +1,4 @@
+#include <init/stdlib.h>
#include <init/tar.h>
#include <shared/flags.h>
#include <shared/syscalls.h>
@@ -28,6 +29,8 @@ int main(void) {
fs_test();
test_await();
+ printf("printf test\n");
+
char c;
while (_syscall_read(tty_fd, &c, 1, 0))
_syscall_write(tty_fd, &c, 1, 0);
diff --git a/src/init/stdlib.c b/src/init/stdlib.c
index 6ed5a0a..ac80c28 100644
--- a/src/init/stdlib.c
+++ b/src/init/stdlib.c
@@ -1,4 +1,6 @@
#include <init/stdlib.h>
+#include <shared/syscalls.h>
+#include <stdarg.h>
int memcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *c1 = s1, *c2 = s2;
@@ -10,3 +12,19 @@ int memcmp(const void *s1, const void *s2, size_t n) {
}
return 0;
}
+
+int printf(const char *fmt, ...) {
+ const char *seg = fmt; // beginning of the current segment
+ int total = 0;
+ va_list argp;
+ va_start(argp, fmt);
+ for (;;) {
+ char c = *fmt++;
+ switch (c) {
+ case '\0':
+ // TODO don't assume that stdout is @ fd 0
+ _syscall_write(0, seg, fmt - seg, 0);
+ return total + (fmt - seg);
+ }
+ }
+}
diff --git a/src/init/stdlib.h b/src/init/stdlib.h
index 1cabc7a..357a396 100644
--- a/src/init/stdlib.h
+++ b/src/init/stdlib.h
@@ -5,3 +5,5 @@
// stb-style header file
int memcmp(const void *s1, const void *s2, size_t n);
+
+int printf(const char *fmt, ...);