ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit c5182bee36ba93429321a7178a6c67c4aea8ac61
parent 641c7e79f3add3b6decdbacf51119a908d458a33
Author: sin <sin@2f30.org>
Date:   Thu, 15 Aug 2013 16:02:53 +0100

Add free(1)

Diffstat:
MMakefile | 1+
Afree.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -18,6 +18,7 @@ SRC = \ chvt.c \ df.c \ dmesg.c \ + free.c \ halt.c \ insmod.c \ lsmod.c \ diff --git a/free.c b/free.c @@ -0,0 +1,69 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/sysinfo.h> +#include <stdio.h> +#include <stdlib.h> +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: %s [-bkmg]\n", argv0); +} + +static unsigned int mem_unit = 1; +static unsigned int unit_shift; + +static unsigned long long +scale(unsigned long long v) +{ + return (v * mem_unit) >> unit_shift; +} + +int +main(int argc, char *argv[]) +{ + struct sysinfo info; + + sysinfo(&info); + mem_unit = info.mem_unit ? info.mem_unit : 1; + + ARGBEGIN { + case 'b': + unit_shift = 0; + break; + case 'k': + unit_shift = 10; + break; + case 'm': + unit_shift = 20; + break; + case 'g': + unit_shift = 30; + break; + default: + usage(); + } ARGEND; + + printf(" %13s%13s%13s%13s%13s\n", + "total", + "used", + "free", + "shared", "buffers"); + printf("Mem: "); + printf("%13llu%13llu%13llu%13llu%13llu\n", + scale(info.totalram), + scale(info.totalram - info.freeram), + scale(info.freeram), + scale(info.sharedram), + scale(info.bufferram)); + printf("-/+ buffers/cache:"); + printf("%13llu%13llu\n", + scale(info.totalram - info.freeram - info.bufferram), + scale(info.freeram + info.bufferram)); + printf("Swap:"); + printf("%13llu%13llu%13llu\n", + scale(info.totalswap), + scale(info.totalswap - info.freeswap), + scale(info.freeswap)); + return 0; +}