blob: 84432e0d1a1664622918f6627310cbd969f6dfd5 [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
Rob Landleydfba7412006-03-06 20:47:33 +000014int free_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000015{
Erik Andersend07ee462000-02-21 21:26:32 +000016 struct sysinfo info;
17 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000018
19 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Eric Andersen10dc9d42000-06-26 10:45:52 +000020 if (info.mem_unit==0) {
Eric Andersen73de6562000-09-10 16:10:41 +000021 info.mem_unit=1;
Eric Andersen10dc9d42000-06-26 10:45:52 +000022 }
Eric Andersen1dcf2182003-01-11 20:40:49 +000023 if ( info.mem_unit == 1 ) {
24 info.mem_unit=1024;
25
26 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
27 info.totalram/=info.mem_unit;
28 info.freeram/=info.mem_unit;
29#ifndef __uClinux__
30 info.totalswap/=info.mem_unit;
31 info.freeswap/=info.mem_unit;
32#endif
33 info.sharedram/=info.mem_unit;
34 info.bufferram/=info.mem_unit;
35 } else {
36 info.mem_unit/=1024;
37 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
38 info.totalram*=info.mem_unit;
39 info.freeram*=info.mem_unit;
40#ifndef __uClinux__
41 info.totalswap*=info.mem_unit;
42 info.freeswap*=info.mem_unit;
43#endif
44 info.sharedram*=info.mem_unit;
45 info.bufferram*=info.mem_unit;
46 }
Eric Andersen73de6562000-09-10 16:10:41 +000047
Matt Kraai3bd8bd82000-07-14 23:28:47 +000048 if (argc > 1 && **(argv + 1) == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_show_usage();
Erik Andersend07ee462000-02-21 21:26:32 +000050
Eric Andersenc7bda1c2004-03-15 08:29:22 +000051 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
Erik Andersend07ee462000-02-21 21:26:32 +000052 "shared", "buffers");
53
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
55 info.totalram-info.freeram, info.freeram,
Erik Andersend07ee462000-02-21 21:26:32 +000056 info.sharedram, info.bufferram);
57
Eric Andersen1dcf2182003-01-11 20:40:49 +000058#ifndef __uClinux__
Erik Andersend07ee462000-02-21 21:26:32 +000059 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
60 info.totalswap-info.freeswap, info.freeswap);
61
62 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
63 (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
64 info.freeram+info.freeswap);
Eric Andersen1dcf2182003-01-11 20:40:49 +000065#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000066 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000067}
Eric Andersen10dc9d42000-06-26 10:45:52 +000068