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/stdio/vprintf.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/kernel/lib/stdio/vprintf.c (limited to 'src/kernel/lib/stdio/vprintf.c') diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c new file mode 100644 index 0000000..885f584 --- /dev/null +++ b/src/kernel/lib/stdio/vprintf.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +void __writes(const char *data) +{ + for (size_t i = 0; i < strlen(data); i++) writec(data[i]); +} + +void vprintf(const char *fmt, va_list args) +{ + uint8_t readyToFormat = 0; + + char buff = 0; + + for (; *fmt; fmt++) { + if (readyToFormat) { + if (*fmt == '%') { + writec('%'); + readyToFormat = 0; + continue; + } + + buff = *fmt; + if (buff == 's') { + const char *str = va_arg(args, const char*); + __writes(str); + readyToFormat = 0; + } else if (buff == 'x') { + char *p = htoa((uint32_t) va_arg(args, int)); + __writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'd') { + char *p = itoa(va_arg(args, int)); + __writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'c') { + writec((char) va_arg(args, int)); + readyToFormat = 0; + } + } else { + if (*fmt == '%') + readyToFormat = 1; + else + writec(*fmt); + } + } +} \ No newline at end of file -- cgit v1.2.3