blob: 7d8ffa893319a16b1904f1e138772a9b7cac1de0 [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 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under the GPL v2 or later, 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#include <stdio.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +000014#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000015#include <stdlib.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016
Rob Landleydfba7412006-03-06 20:47:33 +000017int free_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000018{
Erik Andersend07ee462000-02-21 21:26:32 +000019 struct sysinfo info;
20 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000021
22 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Eric Andersen10dc9d42000-06-26 10:45:52 +000023 if (info.mem_unit==0) {
Eric Andersen73de6562000-09-10 16:10:41 +000024 info.mem_unit=1;
Eric Andersen10dc9d42000-06-26 10:45:52 +000025 }
Eric Andersen1dcf2182003-01-11 20:40:49 +000026 if ( info.mem_unit == 1 ) {
27 info.mem_unit=1024;
28
29 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
30 info.totalram/=info.mem_unit;
31 info.freeram/=info.mem_unit;
32#ifndef __uClinux__
33 info.totalswap/=info.mem_unit;
34 info.freeswap/=info.mem_unit;
35#endif
36 info.sharedram/=info.mem_unit;
37 info.bufferram/=info.mem_unit;
38 } else {
39 info.mem_unit/=1024;
40 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
41 info.totalram*=info.mem_unit;
42 info.freeram*=info.mem_unit;
43#ifndef __uClinux__
44 info.totalswap*=info.mem_unit;
45 info.freeswap*=info.mem_unit;
46#endif
47 info.sharedram*=info.mem_unit;
48 info.bufferram*=info.mem_unit;
49 }
Eric Andersen73de6562000-09-10 16:10:41 +000050
Matt Kraai3bd8bd82000-07-14 23:28:47 +000051 if (argc > 1 && **(argv + 1) == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 bb_show_usage();
Erik Andersend07ee462000-02-21 21:26:32 +000053
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
Erik Andersend07ee462000-02-21 21:26:32 +000055 "shared", "buffers");
56
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
58 info.totalram-info.freeram, info.freeram,
Erik Andersend07ee462000-02-21 21:26:32 +000059 info.sharedram, info.bufferram);
60
Eric Andersen1dcf2182003-01-11 20:40:49 +000061#ifndef __uClinux__
Erik Andersend07ee462000-02-21 21:26:32 +000062 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
63 info.totalswap-info.freeswap, info.freeswap);
64
65 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
66 (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
67 info.freeram+info.freeswap);
Eric Andersen1dcf2182003-01-11 20:40:49 +000068#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000069 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000070}
Eric Andersen10dc9d42000-06-26 10:45:52 +000071