ubase

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

free.c (1415B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/sysinfo.h>
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 #include "util.h"
      8 
      9 static unsigned int mem_unit = 1;
     10 static unsigned int unit_shift;
     11 
     12 static unsigned long long
     13 scale(unsigned long long v)
     14 {
     15 	return (v * mem_unit) >> unit_shift;
     16 }
     17 
     18 static void
     19 usage(void)
     20 {
     21 	eprintf("usage: %s [-bkmg]\n", argv0);
     22 }
     23 
     24 int
     25 main(int argc, char *argv[])
     26 {
     27 	struct sysinfo info;
     28 
     29 	if (sysinfo(&info) < 0)
     30 		eprintf("sysinfo:");
     31 	mem_unit = info.mem_unit ? info.mem_unit : 1;
     32 
     33 	ARGBEGIN {
     34 	case 'b':
     35 		unit_shift = 0;
     36 		break;
     37 	case 'k':
     38 		unit_shift = 10;
     39 		break;
     40 	case 'm':
     41 		unit_shift = 20;
     42 		break;
     43 	case 'g':
     44 		unit_shift = 30;
     45 		break;
     46 	default:
     47 		usage();
     48 	} ARGEND;
     49 
     50 	printf("     %13s%13s%13s%13s%13s\n",
     51 	       "total",
     52 	       "used",
     53 	       "free",
     54 	       "shared", "buffers");
     55 	printf("Mem: ");
     56 	printf("%13llu%13llu%13llu%13llu%13llu\n",
     57 	       scale(info.totalram),
     58 	       scale(info.totalram - info.freeram),
     59 	       scale(info.freeram),
     60 	       scale(info.sharedram),
     61 	       scale(info.bufferram));
     62 	printf("-/+ buffers/cache:");
     63 	printf("%13llu%13llu\n",
     64 	       scale(info.totalram - info.freeram - info.bufferram),
     65 	       scale(info.freeram + info.bufferram));
     66 	printf("Swap:");
     67 	printf("%13llu%13llu%13llu\n",
     68 	       scale(info.totalswap),
     69 	       scale(info.totalswap - info.freeswap),
     70 	       scale(info.freeswap));
     71 	return 0;
     72 }