move remaining help text from include/usage.src.h
[oweals/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 //usage:#define free_trivial_usage
13 //usage:       "" IF_DESKTOP("[-b/k/m/g]")
14 //usage:#define free_full_usage "\n\n"
15 //usage:       "Display the amount of free and used system memory"
16 //usage:
17 //usage:#define free_example_usage
18 //usage:       "$ free\n"
19 //usage:       "              total         used         free       shared      buffers\n"
20 //usage:       "  Mem:       257628       248724         8904        59644        93124\n"
21 //usage:       " Swap:       128516         8404       120112\n"
22 //usage:       "Total:       386144       257128       129016\n"
23
24 #include "libbb.h"
25
26 struct globals {
27         unsigned mem_unit;
28 #if ENABLE_DESKTOP
29         unsigned unit_steps;
30 # define G_unit_steps G.unit_steps
31 #else
32 # define G_unit_steps 10
33 #endif
34 } FIX_ALIASING;
35 #define G (*(struct globals*)&bb_common_bufsiz1)
36 #define INIT_G() do { } while (0)
37
38
39 static unsigned long long scale(unsigned long d)
40 {
41         return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
42 }
43
44
45 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
47 {
48         struct sysinfo info;
49
50         INIT_G();
51
52 #if ENABLE_DESKTOP
53         G.unit_steps = 10;
54         if (argv[1] && argv[1][0] == '-') {
55                 switch (argv[1][1]) {
56                 case 'b':
57                         G.unit_steps = 0;
58                         break;
59                 case 'k': /* 2^10 */
60                         /* G.unit_steps = 10; - already is */
61                         break;
62                 case 'm': /* 2^(2*10) */
63                         G.unit_steps = 20;
64                         break;
65                 case 'g': /* 2^(3*10) */
66                         G.unit_steps = 30;
67                         break;
68                 default:
69                         bb_show_usage();
70                 }
71         }
72 #endif
73
74         sysinfo(&info);
75
76         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
77         G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
78
79         printf("     %13s%13s%13s%13s%13s\n",
80                 "total",
81                 "used",
82                 "free",
83                 "shared", "buffers" /* swap and total don't have these columns */
84                 /* procps version 3.2.8 also shows "cached" column, but
85                  * sysinfo() does not provide this value, need to parse
86                  * /proc/meminfo instead and get "Cached: NNN kB" from there.
87                  */
88         );
89
90 #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
91 #define FIELDS_3 (FIELDS_5 + 2*6)
92 #define FIELDS_2 (FIELDS_5 + 3*6)
93
94         printf("Mem: ");
95         printf(FIELDS_5,
96                 scale(info.totalram),
97                 scale(info.totalram - info.freeram),
98                 scale(info.freeram),
99                 scale(info.sharedram),
100                 scale(info.bufferram)
101         );
102         /* Show alternate, more meaningful busy/free numbers by counting
103          * buffer cache as free memory (make it "-/+ buffers/cache"
104          * if/when we add support for "cached" column): */
105         printf("-/+ buffers:      ");
106         printf(FIELDS_2,
107                 scale(info.totalram - info.freeram - info.bufferram),
108                 scale(info.freeram + info.bufferram)
109         );
110 #if BB_MMU
111         printf("Swap:");
112         printf(FIELDS_3,
113                 scale(info.totalswap),
114                 scale(info.totalswap - info.freeswap),
115                 scale(info.freeswap)
116         );
117 #endif
118         return EXIT_SUCCESS;
119 }