pmap: get rid of a warning
[platform/upstream/busybox.git] / procps / free.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini free implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 /* getopt not needed */
11
12 #include "libbb.h"
13
14 struct globals {
15         unsigned mem_unit;
16 #if ENABLE_DESKTOP
17         unsigned unit_steps;
18 # define G_unit_steps G.unit_steps
19 #else
20 # define G_unit_steps 10
21 #endif
22 };
23 #define G (*(struct globals*)&bb_common_bufsiz1)
24 #define INIT_G() do { } while (0)
25
26
27 static unsigned long long scale(unsigned long d)
28 {
29         return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
30 }
31
32
33 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
35 {
36         struct sysinfo info;
37
38         INIT_G();
39
40 #if ENABLE_DESKTOP
41         G.unit_steps = 10;
42         if (argv[1] && argv[1][0] == '-') {
43                 switch (argv[1][1]) {
44                 case 'b':
45                         G.unit_steps = 0;
46                         break;
47                 case 'k': /* 2^10 */
48                         /* G.unit_steps = 10; - already is */
49                         break;
50                 case 'm': /* 2^(2*10) */
51                         G.unit_steps = 20;
52                         break;
53                 case 'g': /* 2^(3*10) */
54                         G.unit_steps = 30;
55                         break;
56                 default:
57                         bb_show_usage();
58                 }
59         }
60 #endif
61
62         sysinfo(&info);
63
64         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
65         G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
66
67         printf("     %13s%13s%13s%13s%13s\n",
68                 "total",
69                 "used",
70                 "free",
71                 "shared", "buffers" /* swap and total don't have these columns */
72                 /* procps version 3.2.8 also shows "cached" column, but
73                  * sysinfo() does not provide this value, need to parse
74                  * /proc/meminfo instead and get "Cached: NNN kB" from there.
75                  */
76         );
77
78 #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
79 #define FIELDS_3 (FIELDS_5 + 2*6)
80 #define FIELDS_2 (FIELDS_5 + 3*6)
81
82         printf("Mem: ");
83         printf(FIELDS_5,
84                 scale(info.totalram),
85                 scale(info.totalram - info.freeram),
86                 scale(info.freeram),
87                 scale(info.sharedram),
88                 scale(info.bufferram)
89         );
90         /* Show alternate, more meaningful busy/free numbers by counting
91          * buffer cache as free memory (make it "-/+ buffers/cache"
92          * if/when we add support for "cached" column): */
93         printf("-/+ buffers:      ");
94         printf(FIELDS_2,
95                 scale(info.totalram - info.freeram - info.bufferram),
96                 scale(info.freeram + info.bufferram)
97         );
98 #if BB_MMU
99         printf("Swap:");
100         printf(FIELDS_3,
101                 scale(info.totalswap),
102                 scale(info.totalswap - info.freeswap),
103                 scale(info.freeswap)
104         );
105 #endif
106         return EXIT_SUCCESS;
107 }