blob: 473d70be80eed4e0a4caa9c67659e69e22237bdf [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;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010015int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016{
Erik Andersend07ee462000-02-21 21:26:32 +000017 struct sysinfo info;
Denys Vlasenkobef57112010-02-21 05:39:59 +010018 unsigned mem_unit;
19
20#if ENABLE_DESKTOP
21 if (argv[1] && argv[1][0] == '-')
22 bb_show_usage();
23#endif
24
Erik Andersend07ee462000-02-21 21:26:32 +000025 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000026
27 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Denys Vlasenkobef57112010-02-21 05:39:59 +010028 mem_unit = 1;
29 if (info.mem_unit != 0) {
30 mem_unit = info.mem_unit;
Eric Andersen10dc9d42000-06-26 10:45:52 +000031 }
Eric Andersen1dcf2182003-01-11 20:40:49 +000032
Denys Vlasenkobef57112010-02-21 05:39:59 +010033 /* Convert values to kbytes */
34 if (mem_unit == 1) {
35 info.totalram >>= 10;
36 info.freeram >>= 10;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010037#if BB_MMU
Denys Vlasenkobef57112010-02-21 05:39:59 +010038 info.totalswap >>= 10;
39 info.freeswap >>= 10;
Eric Andersen1dcf2182003-01-11 20:40:49 +000040#endif
Denys Vlasenkobef57112010-02-21 05:39:59 +010041 info.sharedram >>= 10;
42 info.bufferram >>= 10;
Eric Andersen1dcf2182003-01-11 20:40:49 +000043 } else {
Denys Vlasenkobef57112010-02-21 05:39:59 +010044 mem_unit >>= 10;
45 /* TODO: Make all this stuff not overflow when mem >= 4 Tb */
46 info.totalram *= mem_unit;
47 info.freeram *= mem_unit;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010048#if BB_MMU
Denys Vlasenkobef57112010-02-21 05:39:59 +010049 info.totalswap *= mem_unit;
50 info.freeswap *= mem_unit;
Eric Andersen1dcf2182003-01-11 20:40:49 +000051#endif
Denys Vlasenkobef57112010-02-21 05:39:59 +010052 info.sharedram *= mem_unit;
53 info.bufferram *= mem_unit;
Eric Andersen1dcf2182003-01-11 20:40:49 +000054 }
Eric Andersen73de6562000-09-10 16:10:41 +000055
Denys Vlasenkobef57112010-02-21 05:39:59 +010056 printf(" %13s%13s%13s%13s%13s\n",
57 "total",
58 "used",
59 "free",
60 "shared", "buffers" /* swap and total don't have these columns */
61 );
62 printf("%6s%13lu%13lu%13lu%13lu%13lu\n", "Mem:",
63 info.totalram,
64 info.totalram - info.freeram,
65 info.freeram,
66 info.sharedram, info.bufferram
67 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010068#if BB_MMU
Denys Vlasenkobef57112010-02-21 05:39:59 +010069 printf("%6s%13lu%13lu%13lu\n", "Swap:",
70 info.totalswap,
71 info.totalswap - info.freeswap,
72 info.freeswap
73 );
74 printf("%6s%13lu%13lu%13lu\n", "Total:",
75 info.totalram + info.totalswap,
76 (info.totalram - info.freeram) + (info.totalswap - info.freeswap),
77 info.freeram + info.freeswap
78 );
Eric Andersen1dcf2182003-01-11 20:40:49 +000079#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000080 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000081}