diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/printf.c | 17 | ||||
-rw-r--r-- | src/user/app/tests/shared/printf.c | 9 |
2 files changed, 22 insertions, 4 deletions
diff --git a/src/shared/printf.c b/src/shared/printf.c index 4e81603..c1206f3 100644 --- a/src/shared/printf.c +++ b/src/shared/printf.c @@ -46,7 +46,7 @@ static void pad(struct out_state *os, struct mods *m, size_t len) { output(os, &m->fill_char, 1); } -static void output_uint(struct out_state *os, struct mods *m, unsigned long long n) { +static void output_uint(struct out_state *os, struct mods *m, unsigned long long n, char sign) { char buf[sizeof(unsigned long long) * 3]; size_t pos = sizeof(buf); @@ -57,6 +57,7 @@ static void output_uint(struct out_state *os, struct mods *m, unsigned long long buf[--pos] = r + '0'; n = q; } + if (sign) buf[--pos] = sign; pad(os, m, sizeof(buf) - pos); output(os, buf + pos, sizeof(buf) - pos); } @@ -111,6 +112,8 @@ int __printf_internal(const char *fmt, va_list argp, switch (c) { unsigned long n, len; + long ns; + char sign; case 'c': output_c(&os, va_arg(argp, int)); @@ -142,7 +145,17 @@ int __printf_internal(const char *fmt, va_list argp, case 'u': n = va_arg(argp, unsigned long); - output_uint(&os, &m, n); + output_uint(&os, &m, n, 0); + break; + + case 'd': + ns = va_arg(argp, int); + sign = 0; + if (ns < 0) { + ns = -ns; + sign = '-'; + } + output_uint(&os, &m, (long)ns, sign); break; case '%': diff --git a/src/user/app/tests/shared/printf.c b/src/user/app/tests/shared/printf.c index 04175db..4e5ffb8 100644 --- a/src/user/app/tests/shared/printf.c +++ b/src/user/app/tests/shared/printf.c @@ -26,8 +26,13 @@ static void test_printf(void) { snprintf(buf, sizeof buf, "%05u %05u", 1234, 56789); test(!strcmp(buf, "01234 56789")); - snprintf(buf, sizeof buf, "%u %x", 0, 0); - test(!strcmp(buf, "0 0")); + snprintf(buf, sizeof buf, "%5d %5d", 123, 4567); + test(!strcmp(buf, " 123 4567")); + snprintf(buf, sizeof buf, "%5d %5d", -123, -4567); + test(!strcmp(buf, " -123 -4567")); + + snprintf(buf, sizeof buf, "%u %d %x", 0, 0, 0); + test(!strcmp(buf, "0 0 0")); } void r_s_printf(void) { |