From 611f098bc38256adf2efadbc0b98138a5b4489de Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Sun, 17 Jan 2021 20:18:37 -0600 Subject: kernel: implement a basic printf function Implement a basic printf function using primitives from the TTY library. Signed-off-by: Danny Holman --- include/kernel/io.h | 10 ++++++++ kernel/io.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 include/kernel/io.h create mode 100644 kernel/io.c diff --git a/include/kernel/io.h b/include/kernel/io.h new file mode 100644 index 0000000..bef87af --- /dev/null +++ b/include/kernel/io.h @@ -0,0 +1,10 @@ +#ifndef IO_H +#define IO_H + +#include +#include + +int vprintf(const char *fmt, va_list args); +int printf(const char *fmt, ...); + +#endif diff --git a/kernel/io.c b/kernel/io.c new file mode 100644 index 0000000..ca787b3 --- /dev/null +++ b/kernel/io.c @@ -0,0 +1,72 @@ +#include +#include + +char* convert(unsigned int num, int base) { + static char rep[] = "0123456789ABCDEF"; + static char buffer[50]; + char *ptr; + + ptr = &buffer[49]; + *ptr = '\0'; + + do { + *--ptr = rep[num%base]; + num /= base; + } while (num != 0); + + return ptr; +} + +int vprintf(const char *fmt, va_list args) { + char *traverse; + char *s; + int i; + + for (traverse = fmt; *traverse != '\0'; traverse++) { + if (*traverse != '%') { + tty_putchar(*traverse); + continue; + } else { + traverse++; + } + + switch (*traverse) { + case 'c': + i = va_arg(args, int); + tty_putchar(i); + break; + case 'd': + i = va_arg(args, int); + if (i < 0) { + i = -i; + tty_putchar('-'); + } + tty_writestring(convert(i, 10)); + break; + case 'o': + i = va_arg(args, unsigned int); + tty_writestring(convert(i, 8)); + break; + case 's': + s = va_arg(args, char*); + tty_writestring(s); + break; + case 'x': + i = va_arg(args, unsigned int); + tty_writestring(convert(i, 16)); + break; + } + } + return 0; +} + +int printf(const char *fmt, ...) { + va_list args; + int done; + + va_start(args, fmt); + done = vprintf(fmt, args); + va_end(args); + + return done; +} -- cgit v1.2.3