From c67079dd740d6ba435f5cc7d9dde6506730db5e0 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 18:07:33 +0200 Subject: [PATCH] Kernel, libc: Implement %p in *printf() So we can avoid writing (unsigned long)ptr or (uint64_t)ptr everywhere when wanting to print a pointer. --- kernel/src/std/stdio.cpp | 8 ++++++++ libs/libc/src/printf.cpp | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/kernel/src/std/stdio.cpp b/kernel/src/std/stdio.cpp index 97b2fde3..9b1dc0b0 100644 --- a/kernel/src/std/stdio.cpp +++ b/kernel/src/std/stdio.cpp @@ -159,6 +159,14 @@ static int internal_printf(const char* format, PutString put_string_callback, ss } break; } + case 'p': { + char result[25]; + ultoa(va_arg(ap, uint64_t), result, 16); + if (buffer_insert_index + strlen(result) > 1024) flush_buffer(); + memcpy(buffer + buffer_insert_index, result, strlen(result)); + buffer_insert_index += strlen(result); + if (buffer_insert_index == 1024) flush_buffer(); + } case 's': { const char* str = va_arg(ap, const char*); while (strlen(str) > 1024) diff --git a/libs/libc/src/printf.cpp b/libs/libc/src/printf.cpp index 9d2efbb3..c978c270 100644 --- a/libs/libc/src/printf.cpp +++ b/libs/libc/src/printf.cpp @@ -242,6 +242,14 @@ static int internal_printf(const char* format, PutString put_string_callback, ss } break; } + case 'p': { + char result[25]; + __unsignedtoa(va_arg(ap, uint64_t), result, 16); + if (buffer_insert_index + strlen(result) > 1024) flush_buffer(); + memcpy(buffer + buffer_insert_index, result, strlen(result)); + buffer_insert_index += strlen(result); + if (buffer_insert_index == 1024) flush_buffer(); + } case 's': { const char* str = va_arg(ap, const char*); while (strlen(str) > 1024)