From 9e563e1c6c053ad848a15c30efbbe8a079154fb4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 6 Dec 2015 13:39:07 -0600 Subject: Add free -h, based on a patch from Isabella Parakiss. --- toys/other/free.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'toys/other/free.c') diff --git a/toys/other/free.c b/toys/other/free.c index cf4abc52..0a4d69b9 100644 --- a/toys/other/free.c +++ b/toys/other/free.c @@ -2,7 +2,8 @@ * * Copyright 2012 Elie De Brauwer -USE_FREE(NEWTOY(free, "tgmkb[!tgmkb]", TOYFLAG_USR|TOYFLAG_BIN)) +// Flag order is signifcant: b-t are units in order, FLAG_h-1 is unit mask +USE_FREE(NEWTOY(free, "htgmkb[!htgmkb]", TOYFLAG_USR|TOYFLAG_BIN)) config FREE bool "free" @@ -13,6 +14,7 @@ config FREE Display the total, free and used amount of physical memory and swap space. -bkmgt Output units (default is bytes) + -h Human readable */ #define FOR_free @@ -21,11 +23,19 @@ config FREE GLOBALS( unsigned bits; unsigned long long units; + char *buf; ) -static unsigned long long convert(unsigned long d) +static char *convert(unsigned long d) { - return (d*TT.units)>>TT.bits; + long long ll = d*TT.units; + char *s = TT.buf; + + if (toys.optflags & FLAG_h) human_readable(s, ll, 0); + else sprintf(s, "%llu",ll>>TT.bits); + TT.buf += strlen(TT.buf)+1; + + return s; } void free_main(void) @@ -34,12 +44,13 @@ void free_main(void) sysinfo(&in); TT.units = in.mem_unit ? in.mem_unit : 1; - for (TT.bits = 0; toys.optflags && !(toys.optflags&(1<