From 46c4540dd2f6181e77b0800a4e007d78d0162487 Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Sat, 19 May 2018 22:52:17 +0200 Subject: Implement fmt_human_2() and fmt_human_10() These functions take the raw number and a unit and automatically print it out "scaled down" to a proper SI-prefix, for powers of 2 and 10 respectively. Apply them to the 2-power cases and keep the 10-power for a later commit. --- components/ram.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'components/ram.c') diff --git a/components/ram.c b/components/ram.c index 0eb2ea9..1c12aab 100644 --- a/components/ram.c +++ b/components/ram.c @@ -14,7 +14,7 @@ "MemFree: %ld kB\n" "MemAvailable: %ld kB\n", &free, &free, &free) == 3) ? - fmt_scaled(free * 1024) : NULL; + fmt_human_2(free * 1024, "B") : NULL; } const char * @@ -39,7 +39,7 @@ long total; return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ? - fmt_scaled(total * 1024) : NULL; + fmt_human_2(total * 1024, "B") : NULL; } const char * @@ -53,7 +53,7 @@ "MemAvailable: %ld kB\nBuffers: %ld kB\n" "Cached: %ld kB\n", &total, &free, &buffers, &buffers, &cached) == 5) ? - fmt_scaled((total - free - buffers - cached) * 1024) : NULL; + fmt_human_2((total - free - buffers - cached) * 1024, "B") : NULL; } #elif defined(__OpenBSD__) #include @@ -83,7 +83,7 @@ if (load_uvmexp(&uvmexp)) { free_pages = uvmexp.npages - uvmexp.active; - return fmt_scaled(pagetok(free_pages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -109,7 +109,7 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -121,7 +121,7 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.active, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B"); } return NULL; -- cgit v1.2.3