blob: 2b205d0c7fcca3e0d144d8777c31c576d815f936 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000012#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000013
Denis Vlasenko06af2162007-02-03 17:28:39 +000014int free_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000015int free_main(int argc, 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;
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 }
Eric Andersen73de6562000-09-10 16:10:41 +000048
Denis Vlasenko219d14d2007-03-24 15:40:16 +000049 if (argc > 1 && *argv[1] == '-')
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
Eric Andersen1dcf2182003-01-11 20:40:49 +000059#ifndef __uClinux__
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}