12 |
#include "libbb.h" |
#include "libbb.h" |
13 |
|
|
14 |
int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
15 |
int free_main(int argc UNUSED_PARAM, char **argv) |
int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) |
16 |
{ |
{ |
17 |
struct sysinfo info; |
struct sysinfo info; |
18 |
|
unsigned mem_unit; |
19 |
|
|
20 |
|
#if ENABLE_DESKTOP |
21 |
|
if (argv[1] && argv[1][0] == '-') |
22 |
|
bb_show_usage(); |
23 |
|
#endif |
24 |
|
|
25 |
sysinfo(&info); |
sysinfo(&info); |
26 |
|
|
27 |
/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
28 |
if (info.mem_unit == 0) { |
mem_unit = 1; |
29 |
info.mem_unit=1; |
if (info.mem_unit != 0) { |
30 |
|
mem_unit = info.mem_unit; |
31 |
} |
} |
|
if (info.mem_unit == 1) { |
|
|
info.mem_unit=1024; |
|
32 |
|
|
33 |
/* TODO: Make all this stuff not overflow when mem >= 4 Gib */ |
/* Convert values to kbytes */ |
34 |
info.totalram/=info.mem_unit; |
if (mem_unit == 1) { |
35 |
info.freeram/=info.mem_unit; |
info.totalram >>= 10; |
36 |
#ifndef __uClinux__ |
info.freeram >>= 10; |
37 |
info.totalswap/=info.mem_unit; |
#if BB_MMU |
38 |
info.freeswap/=info.mem_unit; |
info.totalswap >>= 10; |
39 |
|
info.freeswap >>= 10; |
40 |
#endif |
#endif |
41 |
info.sharedram/=info.mem_unit; |
info.sharedram >>= 10; |
42 |
info.bufferram/=info.mem_unit; |
info.bufferram >>= 10; |
43 |
} else { |
} else { |
44 |
info.mem_unit/=1024; |
mem_unit >>= 10; |
45 |
/* TODO: Make all this stuff not overflow when mem >= 4 Gib */ |
/* TODO: Make all this stuff not overflow when mem >= 4 Tb */ |
46 |
info.totalram*=info.mem_unit; |
info.totalram *= mem_unit; |
47 |
info.freeram*=info.mem_unit; |
info.freeram *= mem_unit; |
48 |
#ifndef __uClinux__ |
#if BB_MMU |
49 |
info.totalswap*=info.mem_unit; |
info.totalswap *= mem_unit; |
50 |
info.freeswap*=info.mem_unit; |
info.freeswap *= mem_unit; |
51 |
#endif |
#endif |
52 |
info.sharedram*=info.mem_unit; |
info.sharedram *= mem_unit; |
53 |
info.bufferram*=info.mem_unit; |
info.bufferram *= mem_unit; |
54 |
} |
} |
55 |
|
|
56 |
if (argv[1] && argv[1][0] == '-') |
printf(" %13s%13s%13s%13s%13s\n", |
57 |
bb_show_usage(); |
"total", |
58 |
|
"used", |
59 |
printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free", |
"free", |
60 |
"shared", "buffers"); |
"shared", "buffers" /* swap and total don't have these columns */ |
61 |
|
); |
62 |
printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram, |
printf("%6s%13lu%13lu%13lu%13lu%13lu\n", "Mem:", |
63 |
info.totalram-info.freeram, info.freeram, |
info.totalram, |
64 |
info.sharedram, info.bufferram); |
info.totalram - info.freeram, |
65 |
|
info.freeram, |
66 |
#ifndef __uClinux__ |
info.sharedram, info.bufferram |
67 |
printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap, |
); |
68 |
info.totalswap-info.freeswap, info.freeswap); |
#if BB_MMU |
69 |
|
printf("%6s%13lu%13lu%13lu\n", "Swap:", |
70 |
printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap, |
info.totalswap, |
71 |
(info.totalram-info.freeram)+(info.totalswap-info.freeswap), |
info.totalswap - info.freeswap, |
72 |
info.freeram+info.freeswap); |
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 |
|
); |
79 |
#endif |
#endif |
80 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
81 |
} |
} |