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; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame^] | 15 | int free_main(int argc UNUSED_PARAM, char **argv) |
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; |
| 18 | sysinfo(&info); |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 19 | |
| 20 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 21 | if (info.mem_unit == 0) { |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 22 | info.mem_unit=1; |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 23 | } |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 24 | if (info.mem_unit == 1) { |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 25 | 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 Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame^] | 49 | if (argv[1] && argv[1][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | bb_show_usage(); |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 51 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 52 | printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free", |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 53 | "shared", "buffers"); |
| 54 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 55 | printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram, |
| 56 | info.totalram-info.freeram, info.freeram, |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 57 | info.sharedram, info.bufferram); |
| 58 | |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 59 | #ifndef __uClinux__ |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 60 | 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 Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 66 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 67 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 68 | } |