X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flib%2Fstdio.c;h=3549d4e9fd70f854880cb9a87cc26bfcee8f91d4;hb=4419844a889e14a9d7b08205a41b7d97fe4b2bfc;hp=13df2c7703fb47defa6fd3d9d0b9f71605961879;hpb=4fa88ad3aaf1a03e4cce8afe47c9215fa788878d;p=pintos-anon diff --git a/src/lib/stdio.c b/src/lib/stdio.c index 13df2c7..3549d4e 100644 --- a/src/lib/stdio.c +++ b/src/lib/stdio.c @@ -469,31 +469,27 @@ format_integer (uintmax_t value, bool negative, const struct integer_base *b, const char *signifier; /* b->signifier or "". */ int precision; /* Rendered precision. */ int pad_cnt; /* # of pad characters to fill field width. */ - int group_cnt; /* # of digits grouped so far. */ + int digit_cnt; /* # of digits output so far. */ /* Accumulate digits into buffer. This algorithm produces digits in reverse order, so later we will output the buffer's content in reverse. This is also the reason that later we append zeros and the sign. */ cp = buf; - group_cnt = 0; + digit_cnt = 0; while (value > 0) { - if ((c->flags & GROUP) && group_cnt++ == b->group) - { - *cp++ = ','; - group_cnt = 0; - } + if ((c->flags & GROUP) && digit_cnt > 0 && digit_cnt % b->group == 0) + *cp++ = ','; *cp++ = b->digits[value % b->base]; value /= b->base; + digit_cnt++; } /* Append enough zeros to match precision. If requested precision is 0, then a value of zero is rendered as a null string, otherwise as "0". */ precision = c->precision < 0 ? 1 : c->precision; - if (precision < 0) - precision = 1; while (cp - buf < precision && cp - buf < (int) sizeof buf - 8) *cp++ = '0'; @@ -562,9 +558,10 @@ __printf (const char *format, } /* Dumps the SIZE bytes in BUF to the console as hex bytes - arranged 16 per line, plus offsets listed starting at OFS for - the first byte in BU. If ASCII is true then the corresponding - ASCII characters are also rendered alongside. */ + arranged 16 per line. Numeric offsets are also included, + starting at OFS for the first byte in BUF. If ASCII is true + then the corresponding ASCII characters are also rendered + alongside. */ void hex_dump (uintptr_t ofs, const void *buf_, size_t size, bool ascii) { @@ -611,3 +608,21 @@ hex_dump (uintptr_t ofs, const void *buf_, size_t size, bool ascii) size -= n; } } + +/* Prints SIZE, which represents a number of bytes, in a + human-readable format, e.g. "256 kB". */ +void +print_human_readable_size (uint64_t size) +{ + if (size == 1) + printf ("1 byte"); + else + { + static const char *factors[] = {"bytes", "kB", "MB", "GB", "TB", NULL}; + const char **fp; + + for (fp = factors; size >= 1024 && fp[1] != NULL; fp++) + size /= 1024; + printf ("%"PRIu64" %s", *fp); + } +}