- note to cleanup fprintf(2,...) -> bb_error_msg(...) and
[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 the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 /* getopt not needed */
11
12 #include "busybox.h"
13 #include <stdio.h>
14 #include <errno.h>
15 #include <stdlib.h>
16
17 int free_main(int argc, char **argv)
18 {
19         struct sysinfo info;
20         sysinfo(&info);
21
22         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
23         if (info.mem_unit==0) {
24                 info.mem_unit=1;
25         }
26         if ( info.mem_unit == 1 ) {
27                 info.mem_unit=1024;
28
29                 /* TODO:  Make all this stuff not overflow when mem >= 4 Gib */
30                 info.totalram/=info.mem_unit;
31                 info.freeram/=info.mem_unit;
32 #ifndef __uClinux__
33                 info.totalswap/=info.mem_unit;
34                 info.freeswap/=info.mem_unit;
35 #endif
36                 info.sharedram/=info.mem_unit;
37                 info.bufferram/=info.mem_unit;
38         } else {
39                 info.mem_unit/=1024;
40                 /* TODO:  Make all this stuff not overflow when mem >= 4 Gib */
41                 info.totalram*=info.mem_unit;
42                 info.freeram*=info.mem_unit;
43 #ifndef __uClinux__
44                 info.totalswap*=info.mem_unit;
45                 info.freeswap*=info.mem_unit;
46 #endif
47                 info.sharedram*=info.mem_unit;
48                 info.bufferram*=info.mem_unit;
49         }
50
51         if (argc > 1 && **(argv + 1) == '-')
52                 bb_show_usage();
53
54         printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
55                         "shared", "buffers");
56
57         printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
58                         info.totalram-info.freeram, info.freeram,
59                         info.sharedram, info.bufferram);
60
61 #ifndef __uClinux__
62         printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
63                         info.totalswap-info.freeswap, info.freeswap);
64
65         printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
66                         (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
67                         info.freeram+info.freeswap);
68 #endif
69         return EXIT_SUCCESS;
70 }
71