blob: e8bea50642897add644c31d5ff950eebf633838f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenabc0f4f1999-12-08 23:19:36 +00002/*
3 * Mini free implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenabc0f4f1999-12-08 23:19:36 +00006 *
Rob Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersenabc0f4f1999-12-08 23:19:36 +00008 */
9
Mark Whitley827e45c2001-03-09 23:59:51 +000010/* getopt not needed */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000013
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010015int free_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016{
Erik Andersend07ee462000-02-21 21:26:32 +000017 struct sysinfo info;
18 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000019
20 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Denis Vlasenko219d14d2007-03-24 15:40:16 +000021 if (info.mem_unit == 0) {
Eric Andersen73de6562000-09-10 16:10:41 +000022 info.mem_unit=1;
Eric Andersen10dc9d42000-06-26 10:45:52 +000023 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +000024 if (info.mem_unit == 1) {
Eric Andersen1dcf2182003-01-11 20:40:49 +000025 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;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010030#if BB_MMU
Eric Andersen1dcf2182003-01-11 20:40:49 +000031 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;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010041#if BB_MMU
Eric Andersen1dcf2182003-01-11 20:40:49 +000042 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 }
Eric Andersen73de6562000-09-10 16:10:41 +000048
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010049 if (argv[1] && argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_show_usage();
Erik Andersend07ee462000-02-21 21:26:32 +000051
Eric Andersenc7bda1c2004-03-15 08:29:22 +000052 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
Erik Andersend07ee462000-02-21 21:26:32 +000053 "shared", "buffers");
54
Eric Andersenc7bda1c2004-03-15 08:29:22 +000055 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
56 info.totalram-info.freeram, info.freeram,
Erik Andersend07ee462000-02-21 21:26:32 +000057 info.sharedram, info.bufferram);
58
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010059#if BB_MMU
Erik Andersend07ee462000-02-21 21:26:32 +000060 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);
Eric Andersen1dcf2182003-01-11 20:40:49 +000066#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000067 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000068}