From a41f08402e78e9066551d72a9835a352db4069e4 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Wed, 6 Oct 2021 06:43:27 +0000
Subject: init printf: implement %x

---
 src/init/main.c   |  2 +-
 src/init/stdlib.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/init/main.c b/src/init/main.c
index 7c827eb..2029bc9 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -29,7 +29,7 @@ int main(void) {
 	fs_test();
 	test_await();
 
-	printf("%s\n", "printf test");
+	printf("%s %x\n", "printf test", 0xACAB1312);
 
 	char c;
 	while (_syscall_read(tty_fd, &c, 1, 0))
diff --git a/src/init/stdlib.c b/src/init/stdlib.c
index 876c74c..a60854b 100644
--- a/src/init/stdlib.c
+++ b/src/init/stdlib.c
@@ -42,6 +42,21 @@ int printf(const char *fmt, ...) {
 						_syscall_write(0, s, strlen(s), 0);
 						total += strlen(s);
 						break;
+
+					case 'x':
+						unsigned int n = va_arg(argp, int);
+						size_t i = 4; // nibbles * 4
+						while (n >> i && i < (sizeof(int) * 8))
+							i += 4;
+
+						while (i > 0) {
+							i -= 4;
+							char h = '0' + ((n >> i) & 0xf);
+							if (h > '9') h += 'a' - '9' - 1;
+							_syscall_write(0, &h, 1, 0);
+							total++;
+						}
+						break;
 				}
 				seg = fmt;
 				break;
-- 
cgit v1.2.3