diff options
Diffstat (limited to 'free.c')
-rw-r--r-- | free.c | 36 |
1 files changed, 27 insertions, 9 deletions
@@ -23,20 +23,36 @@ | |||
23 | 23 | ||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <sys/sysinfo.h> | 26 | #include <errno.h> |
27 | |||
27 | 28 | ||
28 | #define DIVISOR 1024 | ||
29 | extern int free_main(int argc, char **argv) | 29 | extern int free_main(int argc, char **argv) |
30 | { | 30 | { |
31 | struct sysinfo info; | 31 | struct sysinfo info; |
32 | sysinfo(&info); | 32 | sysinfo(&info); |
33 | info.totalram/=DIVISOR; | 33 | /* Kernels prior to 2.4.x will return info.mem_unit==0. Kernels after |
34 | info.freeram/=DIVISOR; | 34 | * 2.4.x actually fill this value in */ |
35 | info.totalswap/=DIVISOR; | 35 | if (info.mem_unit==0) { |
36 | info.freeswap/=DIVISOR; | 36 | /* Looks like we have a kernel prior to Linux 2.4.x */ |
37 | info.sharedram/=DIVISOR; | 37 | info.mem_unit=1024; |
38 | info.bufferram/=DIVISOR; | 38 | info.totalram/=info.mem_unit; |
39 | 39 | info.freeram/=info.mem_unit; | |
40 | info.totalswap/=info.mem_unit; | ||
41 | info.freeswap/=info.mem_unit; | ||
42 | info.sharedram/=info.mem_unit; | ||
43 | info.bufferram/=info.mem_unit; | ||
44 | } else { | ||
45 | /* Bah. Linux 2.4.x completely changed sysinfo. This can in theory | ||
46 | overflow a 32 bit unsigned long, but who puts more then 4GiB ram+swap | ||
47 | on an embedded system? */ | ||
48 | info.mem_unit/=1024; | ||
49 | info.totalram*=info.mem_unit; | ||
50 | info.freeram*=info.mem_unit; | ||
51 | info.totalswap*=info.mem_unit; | ||
52 | info.freeswap*=info.mem_unit; | ||
53 | info.sharedram*=info.mem_unit; | ||
54 | info.bufferram*=info.mem_unit; | ||
55 | } | ||
40 | if (argc > 1 && **(argv + 1) == '-') { | 56 | if (argc > 1 && **(argv + 1) == '-') { |
41 | usage("free\n" | 57 | usage("free\n" |
42 | #ifndef BB_FEATURE_TRIVIAL_HELP | 58 | #ifndef BB_FEATURE_TRIVIAL_HELP |
@@ -61,3 +77,5 @@ extern int free_main(int argc, char **argv) | |||
61 | info.freeram+info.freeswap); | 77 | info.freeram+info.freeswap); |
62 | return(TRUE); | 78 | return(TRUE); |
63 | } | 79 | } |
80 | |||
81 | |||