X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fstdio.c;h=8927c50555d9993ae1ac952e21bc3fe7deb93f7e;hb=a03618133f7df0954802a470a4bee7674f7aed45;hp=29396589d61995fe1e3a1d299ef05a9ee7cb406f;hpb=a477e8092f5575f8518361066f6db0fd447b0c99;p=pintos-anon diff --git a/src/lib/stdio.c b/src/lib/stdio.c index 2939658..8927c50 100644 --- a/src/lib/stdio.c +++ b/src/lib/stdio.c @@ -74,8 +74,7 @@ snprintf (char *buffer, size_t buf_size, const char *format, ...) /* Writes formatted output to the console. In the kernel, the console is both the video display and first serial port. - In userspace, the console is file descriptor 1. -*/ + In userspace, the console is file descriptor 1. */ int printf (const char *format, ...) { @@ -464,9 +463,9 @@ parse_conversion (const char *format, struct printf_conversion *c, auxiliary data AUX. The integer converted has absolute value VALUE. If IS_SIGNED is true, does a signed conversion with NEGATIVE indicating a negative value; otherwise does an - unsigned conversion and ignores IS_SIGNED. The output will - use the given DIGITS, with strlen(DIGITS) indicating the - output base. Details of the conversion are in C. */ + unsigned conversion and ignores NEGATIVE. The output is done + according to the provided base B. Details of the conversion + are in C. */ static void format_integer (uintmax_t value, bool is_signed, bool negative, const struct integer_base *b, @@ -516,7 +515,7 @@ format_integer (uintmax_t value, bool is_signed, bool negative, /* 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". - If the # flag is used with base 0, the result must always + If the # flag is used with base 8, the result must always begin with a zero. */ precision = c->precision < 0 ? 1 : c->precision; while (cp - buf < precision && cp < buf + sizeof buf - 1) @@ -636,3 +635,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", size, *fp); + } +}