diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index 30e92cba4dd5..776c4e80cbd6 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -800,7 +800,8 @@ reswitch: switch (ch = (u_char)*fmt++) { neg = 1; num = -(intmax_t)num; } - p = ksprintn(nbuf, num, base, &tmp, upper); + p = ksprintn(nbuf, num, base, &n, upper); + tmp = 0; if (sharpflag && num != 0) { if (base == 8) tmp++; @@ -810,10 +811,13 @@ reswitch: switch (ch = (u_char)*fmt++) { if (neg) tmp++; - if (!ladjust && padc != '0' && width - && (width -= tmp) > 0) - while (width--) - PCHAR(padc); + if (!ladjust && padc == '0') + dwidth = width - tmp; + width -= tmp + MAX(dwidth, n); + dwidth -= n; + if (!ladjust) + while (width-- > 0) + PCHAR(' '); if (neg) PCHAR('-'); if (sharpflag && num != 0) { @@ -824,16 +828,15 @@ reswitch: switch (ch = (u_char)*fmt++) { PCHAR('x'); } } - if (!ladjust && width && (width -= tmp) > 0) - while (width--) - PCHAR(padc); + while (dwidth-- > 0) + PCHAR('0'); while (*p) PCHAR(*p--); - if (ladjust && width && (width -= tmp) > 0) - while (width--) - PCHAR(padc); + if (ladjust) + while (width-- > 0) + PCHAR(' '); break; default: