1/* vi: set sw=4 ts=4: */
2/*
3 * Mini free implementation for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8 */
9
10/* getopt not needed */
11
12#include "libbb.h"
13
14int free_main(int argc, char **argv);
15int free_main(int argc, char **argv)
16{
17	struct sysinfo info;
18	sysinfo(&info);
19
20	/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
21	if (info.mem_unit == 0) {
22		info.mem_unit=1;
23	}
24	if (info.mem_unit == 1) {
25		info.mem_unit=1024;
26
27		/* TODO:  Make all this stuff not overflow when mem >= 4 Gib */
28		info.totalram/=info.mem_unit;
29		info.freeram/=info.mem_unit;
30#ifndef __uClinux__
31		info.totalswap/=info.mem_unit;
32		info.freeswap/=info.mem_unit;
33#endif
34		info.sharedram/=info.mem_unit;
35		info.bufferram/=info.mem_unit;
36	} else {
37		info.mem_unit/=1024;
38		/* TODO:  Make all this stuff not overflow when mem >= 4 Gib */
39		info.totalram*=info.mem_unit;
40		info.freeram*=info.mem_unit;
41#ifndef __uClinux__
42		info.totalswap*=info.mem_unit;
43		info.freeswap*=info.mem_unit;
44#endif
45		info.sharedram*=info.mem_unit;
46		info.bufferram*=info.mem_unit;
47	}
48
49	if (argc > 1 && *argv[1] == '-')
50		bb_show_usage();
51
52	printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
53			"shared", "buffers");
54
55	printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
56			info.totalram-info.freeram, info.freeram,
57			info.sharedram, info.bufferram);
58
59#ifndef __uClinux__
60	printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
61			info.totalswap-info.freeswap, info.freeswap);
62
63	printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
64			(info.totalram-info.freeram)+(info.totalswap-info.freeswap),
65			info.freeram+info.freeswap);
66#endif
67	return EXIT_SUCCESS;
68}
69