summaryrefslogtreecommitdiff
path: root/userland/libc/stdio
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-11-01 19:34:42 +0100
committerAnton Kling <anton@kling.gg>2023-11-13 14:55:09 +0100
commit249d7f4ab0bff181534b0d17f3387f59b7c5feba (patch)
treebbd2aebe704041a086fc2104ae7a092a592b530e /userland/libc/stdio
parentec2ca84628a253eda95ccb097a2e269dc1dc47b8 (diff)
LibC: Support negative values in printf for %d
Diffstat (limited to 'userland/libc/stdio')
-rw-r--r--userland/libc/stdio/vfprintf.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/userland/libc/stdio/vfprintf.c b/userland/libc/stdio/vfprintf.c
index 79a22fb..f8d56ea 100644
--- a/userland/libc/stdio/vfprintf.c
+++ b/userland/libc/stdio/vfprintf.c
@@ -13,8 +13,6 @@ const char HEX_SET[0x10] = {'0', '1', '2', '3', '4', '5', '6', '7',
assert(0); \
*(int *)(_r) += _rc; \
}
-// if ((size_t)0 == _rc) \
-// assert(0); \
int fprint_num(FILE *f, int n, int base, char *char_set, int prefix,
int zero_padding, int right_padding) {
@@ -25,10 +23,22 @@ int fprint_num(FILE *f, int n, int base, char *char_set, int prefix,
}
char str[32];
int i = 0;
+
+ int is_signed = 0;
+
+ if (n < 0) {
+ is_signed = 1;
+ n *= -1;
+ }
+
for (; n != 0 && i < 32; i++, n /= base)
- // str[i] = (n % base) + '0';
str[i] = char_set[(n % base)];
+ if (is_signed) {
+ str[i] = '-';
+ i++;
+ }
+
char t = (zero_padding) ? '0' : ' ';
int orig_i = i;
@@ -175,10 +185,10 @@ int vfprintf(FILE *f, const char *fmt, va_list ap) {
break;
case 'i':
case 'd':
- if(-1 != precision) {
- zero_padding = 1;
- prefix = precision;
- right_padding = 0;
+ if (-1 != precision) {
+ zero_padding = 1;
+ prefix = precision;
+ right_padding = 0;
}
rc += fprint_int(f, va_arg(ap, int), prefix, zero_padding, right_padding);
cont = 0;