aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-02-21 05:39:59 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-02-21 05:39:59 +0100
commitbef5711dab5b78dcaafb33a7f0071dfd90188fe1 (patch)
treed1f741bcd7209db3bbe8e1ffe28aa9accb31f33b
parent153fcaa6c17124ebbf0d50df5da95f3564c7402b (diff)
downloadbusybox-w32-bef5711dab5b78dcaafb33a7f0071dfd90188fe1.tar.gz
busybox-w32-bef5711dab5b78dcaafb33a7f0071dfd90188fe1.tar.bz2
busybox-w32-bef5711dab5b78dcaafb33a7f0071dfd90188fe1.zip
free: code shrink
function old new delta free_main 330 302 -28 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/free.c83
1 files changed, 48 insertions, 35 deletions
diff --git a/procps/free.c b/procps/free.c
index e8bea5064..a941b6252 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -15,54 +15,67 @@ int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int free_main(int argc UNUSED_PARAM, char **argv) 15int free_main(int argc UNUSED_PARAM, char **argv)
16{ 16{
17 struct sysinfo info; 17 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
18 sysinfo(&info); 25 sysinfo(&info);
19 26
20 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ 27 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
21 if (info.mem_unit == 0) { 28 mem_unit = 1;
22 info.mem_unit=1; 29 if (info.mem_unit != 0) {
30 mem_unit = info.mem_unit;
23 } 31 }
24 if (info.mem_unit == 1) {
25 info.mem_unit=1024;
26 32
27 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */ 33 /* Convert values to kbytes */
28 info.totalram/=info.mem_unit; 34 if (mem_unit == 1) {
29 info.freeram/=info.mem_unit; 35 info.totalram >>= 10;
36 info.freeram >>= 10;
30#if BB_MMU 37#if BB_MMU
31 info.totalswap/=info.mem_unit; 38 info.totalswap >>= 10;
32 info.freeswap/=info.mem_unit; 39 info.freeswap >>= 10;
33#endif 40#endif
34 info.sharedram/=info.mem_unit; 41 info.sharedram >>= 10;
35 info.bufferram/=info.mem_unit; 42 info.bufferram >>= 10;
36 } else { 43 } else {
37 info.mem_unit/=1024; 44 mem_unit >>= 10;
38 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */ 45 /* TODO: Make all this stuff not overflow when mem >= 4 Tb */
39 info.totalram*=info.mem_unit; 46 info.totalram *= mem_unit;
40 info.freeram*=info.mem_unit; 47 info.freeram *= mem_unit;
41#if BB_MMU 48#if BB_MMU
42 info.totalswap*=info.mem_unit; 49 info.totalswap *= mem_unit;
43 info.freeswap*=info.mem_unit; 50 info.freeswap *= mem_unit;
44#endif 51#endif
45 info.sharedram*=info.mem_unit; 52 info.sharedram *= mem_unit;
46 info.bufferram*=info.mem_unit; 53 info.bufferram *= mem_unit;
47 } 54 }
48 55
49 if (argv[1] && argv[1][0] == '-') 56 printf(" %13s%13s%13s%13s%13s\n",
50 bb_show_usage(); 57 "total",
51 58 "used",
52 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free", 59 "free",
53 "shared", "buffers"); 60 "shared", "buffers" /* swap and total don't have these columns */
54 61 );
55 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram, 62 printf("%6s%13lu%13lu%13lu%13lu%13lu\n", "Mem:",
56 info.totalram-info.freeram, info.freeram, 63 info.totalram,
57 info.sharedram, info.bufferram); 64 info.totalram - info.freeram,
58 65 info.freeram,
66 info.sharedram, info.bufferram
67 );
59#if BB_MMU 68#if BB_MMU
60 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap, 69 printf("%6s%13lu%13lu%13lu\n", "Swap:",
61 info.totalswap-info.freeswap, info.freeswap); 70 info.totalswap,
62 71 info.totalswap - info.freeswap,
63 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap, 72 info.freeswap
64 (info.totalram-info.freeram)+(info.totalswap-info.freeswap), 73 );
65 info.freeram+info.freeswap); 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 );
66#endif 79#endif
67 return EXIT_SUCCESS; 80 return EXIT_SUCCESS;
68} 81}