diff options
Diffstat (limited to 'user/lib/printf.c')
-rw-r--r-- | user/lib/printf.c | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/user/lib/printf.c b/user/lib/printf.c index 274fa72..89806da 100644 --- a/user/lib/printf.c +++ b/user/lib/printf.c @@ -269,43 +269,44 @@ static int printf_lltoa(char *buf, options_t *opts, bool is_neg, } precision = 0; - // sign - if (is_neg) { - *(buf++) = '-'; - } else if (opts->sign) { - *(buf++) = '+'; - } else if (opts->space) { - *(buf++) = ' '; + // write number + if (num == 0) { + *(buf++) = '0'; + } + while (num) { + if (opts->precision_set && precision++ >= opts->precision) + break; + *(buf++) = printf_itoc(opts->is_uppercase, num % opts->radix); + num /= opts->radix; + } + + // print zeros if needed + if (opts->width_set && len < opts->width && opts->zero) { + while (len++ < opts->width) + *(buf++) = '0'; } // radix specifier if (opts->hash) { if (opts->radix == 8) { - *(buf++) = '0'; *(buf++) = 'o'; + *(buf++) = '0'; } if (opts->radix == 16) { - *(buf++) = '0'; *(buf++) = 'x'; + *(buf++) = '0'; } } - // print zeros if needed - if (opts->width_set && len < opts->width && opts->zero) { - while (len++ < opts->width) - *(buf++) = '0'; + // sign + if (is_neg) { + *(buf++) = '-'; + } else if (opts->sign) { + *(buf++) = '+'; + } else if (opts->space) { + *(buf++) = ' '; } - // write number - if (num == 0) { - *(buf++) = '0'; - } - while (num) { - if (opts->precision_set && precision++ >= opts->precision) - break; - *(buf++) = printf_itoc(opts->is_uppercase, num % opts->radix); - num /= opts->radix; - } *(buf++) = '\0'; return buf - start; @@ -340,8 +341,8 @@ static void handle_int_specifier(context_t *ctx, options_t *const opts, printf_putc(ctx, opts->zero ? '0' : ' '); } // number - for (int i = 0; i < buf_len; i++) - printf_putc(ctx, buf[i]); + for (int i = 1; i <= buf_len; i++) + printf_putc(ctx, buf[buf_len - i]); // right padding if (opts->left == 1) { for (int i = 0; i < padding; i++) @@ -431,7 +432,8 @@ static void do_printf(context_t *ctx, va_list args) switch (spec) { case 'p': opts.len = PRINTF_LEN_SIZE_T; - opts.width_set = true; + opts.width_set = 1; + opts.width = sizeof(void *) * 2; opts.radix = 16; opts.hash = true; opts.zero = true; |