aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-04-01 21:46:28 +0300
committeraqua <aqua@iserlohn-fortress.net>2022-08-12 10:13:59 +0300
commit9b777b088facde26c33df6c746b948caff725602 (patch)
tree3e88f6808e80a233f3e78640d1f2545489665376 /lib
parentlidt (diff)
downloadkernel-9b777b088facde26c33df6c746b948caff725602.tar.xz
printf: add %d, %u and %x
Diffstat (limited to 'lib')
-rw-r--r--lib/stdio.h3
-rw-r--r--lib/stdio/printf.c12
-rw-r--r--lib/string.h3
-rw-r--r--lib/string/itoa.c29
4 files changed, 40 insertions, 7 deletions
diff --git a/lib/stdio.h b/lib/stdio.h
index cf3a33f..201cce1 100644
--- a/lib/stdio.h
+++ b/lib/stdio.h
@@ -1,3 +1,6 @@
#pragma once
+/**
+ * Supports %s (string), %d (decimal), %u (unsigned), %x (hexadecimal)
+ */
int printf(const char *restrict format, ...);
diff --git a/lib/stdio/printf.c b/lib/stdio/printf.c
index f6b8ef6..269b9b6 100644
--- a/lib/stdio/printf.c
+++ b/lib/stdio/printf.c
@@ -1,8 +1,11 @@
#include <stdarg.h>
#include <stdio.h>
+#include <string.h>
#include <devices/uart_16550.h>
+static char buffer[3 * sizeof(int) + 2];
+
int
printf(const char *restrict format, ...)
{
@@ -22,6 +25,15 @@ printf(const char *restrict format, ...)
case 's':
written += uart_puts(COM1, va_arg(params, const char *), -1);
break;
+ case 'd':
+ written += uart_puts(COM1, itoa(buffer, va_arg(params, int), 10), -1);
+ break;
+ case 'u':
+ written += uart_puts(COM1, utoa(buffer, va_arg(params, unsigned int), 10), -1);
+ break;
+ case 'x':
+ written += uart_puts(COM1, utoa(buffer, va_arg(params, unsigned int), 16), -1);
+ break;
}
l = 0;
diff --git a/lib/string.h b/lib/string.h
index 361d308..9873362 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -1,3 +1,4 @@
#pragma once
-char *itoa(char *p, unsigned x);
+char *itoa(char *p, int x, int base);
+char *utoa(char *p, unsigned x, int base);
diff --git a/lib/string/itoa.c b/lib/string/itoa.c
index 5eefb37..3708be2 100644
--- a/lib/string/itoa.c
+++ b/lib/string/itoa.c
@@ -1,13 +1,30 @@
-#include "string.h"
+#include <stdbool.h>
+#include <string.h>
+
+static const char *numbers = "0123456789abcdef";
+
+char *
+itoa(char *p, int x, int base)
+{
+ const bool is_negative = (x < 0);
+ if (is_negative) x = -x;
+
+ p = utoa(p, x, base);
+
+ if (is_negative) *--p = '-';
+ return p;
+}
char *
-itoa(char *p, unsigned x)
+utoa(char *p, unsigned x, int base)
{
- p += 3 * sizeof(int);
- *--p = 0;
+ p += 3 * sizeof(unsigned);
+ *--p = '\0';
+
do {
- *--p = '0' + x % 10;
- x /= 10;
+ *--p = numbers[x % base];
+ x /= base;
} while (x);
+
return p;
}