From 45a5ecc72d8e700614a0751f0e7ec68199885b77 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 29 May 2020 23:47:29 +0200 Subject: Added more printf features --- src/kernel/lib/stdio/vsprintf.c | 18 ++++++++++++++---- src/kernel/lib/stdlib.h | 2 +- src/kernel/lib/stdlib/itoa.c | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/kernel/lib/stdio/vsprintf.c b/src/kernel/lib/stdio/vsprintf.c index 6584ed1..61f4323 100644 --- a/src/kernel/lib/stdio/vsprintf.c +++ b/src/kernel/lib/stdio/vsprintf.c @@ -27,20 +27,30 @@ int vsprintf(char *str, const char *fmt, va_list args) } buf = *fmt; + + // TODO: Improve this repetitive code if (buf == 's') { char *string = va_arg(args, char *); append(str, string, i); i = strlen(str); } else if (buf == 'x') { - itoa_base((u32)va_arg(args, int), format_buffer, 16); + itoa_base(va_arg(args, u32), format_buffer, 16, 0); + append(str, format_buffer, i); + i = strlen(str); + } else if (buf == 'd' || buf == 'i') { + itoa_base(va_arg(args, s32), format_buffer, 10, 1); append(str, format_buffer, i); i = strlen(str); - } else if (buf == 'd') { - itoa_base(va_arg(args, int), format_buffer, 10); + } else if (buf == 'u') { + itoa_base(va_arg(args, u32), format_buffer, 10, 0); append(str, format_buffer, i); i = strlen(str); } else if (buf == 'o') { - itoa_base(va_arg(args, int), format_buffer, 8); + itoa_base(va_arg(args, u32), format_buffer, 8, 0); + append(str, format_buffer, i); + i = strlen(str); + } else if (buf == 'b') { + itoa_base(va_arg(args, u32), format_buffer, 2, 0); append(str, format_buffer, i); i = strlen(str); } else if (buf == 'c') { diff --git a/src/kernel/lib/stdlib.h b/src/kernel/lib/stdlib.h index 77254b6..cee453c 100644 --- a/src/kernel/lib/stdlib.h +++ b/src/kernel/lib/stdlib.h @@ -10,7 +10,7 @@ #endif char *itoa(int n); -char *itoa_base(int value, char *result, int base); +char *itoa_base(int value, char *result, int base, int is_signed); int atoi(char *str); diff --git a/src/kernel/lib/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c index 2d2243a..b15befd 100644 --- a/src/kernel/lib/stdlib/itoa.c +++ b/src/kernel/lib/stdlib/itoa.c @@ -44,7 +44,7 @@ char *itoa(int n) } // TODO: Rename itoa_base -char *itoa_base(int value, char *result, int base) +char *itoa_base(int value, char *result, int base, int is_signed) { if (base < 2 || base > 36) { *result = '\0'; @@ -61,7 +61,7 @@ char *itoa_base(int value, char *result, int base) [35 + (tmp_value - value * base)]; } while (value); - if (tmp_value < 0) + if (is_signed && tmp_value < 0) *ptr++ = '-'; *ptr-- = '\0'; while (ptr1 < ptr) { -- cgit v1.2.3