Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini free implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 6 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 10 | /* getopt not needed */ |
| 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame^] | 15 | int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 16 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 17 | struct sysinfo info; |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 18 | unsigned mem_unit; |
| 19 | |
| 20 | #if ENABLE_DESKTOP |
| 21 | if (argv[1] && argv[1][0] == '-') |
| 22 | bb_show_usage(); |
| 23 | #endif |
| 24 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 25 | sysinfo(&info); |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 26 | |
| 27 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 28 | mem_unit = 1; |
| 29 | if (info.mem_unit != 0) { |
| 30 | mem_unit = info.mem_unit; |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 31 | } |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 32 | |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 33 | /* Convert values to kbytes */ |
| 34 | if (mem_unit == 1) { |
| 35 | info.totalram >>= 10; |
| 36 | info.freeram >>= 10; |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 37 | #if BB_MMU |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 38 | info.totalswap >>= 10; |
| 39 | info.freeswap >>= 10; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 40 | #endif |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 41 | info.sharedram >>= 10; |
| 42 | info.bufferram >>= 10; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 43 | } else { |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 44 | 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 Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 48 | #if BB_MMU |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 49 | info.totalswap *= mem_unit; |
| 50 | info.freeswap *= mem_unit; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 51 | #endif |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 52 | info.sharedram *= mem_unit; |
| 53 | info.bufferram *= mem_unit; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 54 | } |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 55 | |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 56 | 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 Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 68 | #if BB_MMU |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 69 | 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 Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 79 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 80 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 81 | } |