aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/kernel/lib/stdio/vsprintf.c18
-rw-r--r--src/kernel/lib/stdlib.h2
-rw-r--r--src/kernel/lib/stdlib/itoa.c4
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) {