lsmod: unicode-aware column formatting
[oweals/busybox.git] / modutils / lsmod.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini lsmod implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "libbb.h"
12 #include "unicode.h"
13
14 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
15 enum {
16         TAINT_PROPRIETORY_MODULE = (1 << 0),
17         TAINT_FORCED_MODULE      = (1 << 1),
18         TAINT_UNSAFE_SMP         = (1 << 2),
19 };
20
21 static void check_tainted(void)
22 {
23         int tainted = 0;
24         char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
25         if (buf) {
26                 tainted = atoi(buf);
27                 if (ENABLE_FEATURE_CLEAN_UP)
28                         free(buf);
29         }
30
31         if (tainted) {
32                 printf("    Tainted: %c%c%c\n",
33                                 tainted & TAINT_PROPRIETORY_MODULE      ? 'P' : 'G',
34                                 tainted & TAINT_FORCED_MODULE           ? 'F' : ' ',
35                                 tainted & TAINT_UNSAFE_SMP              ? 'S' : ' ');
36         } else {
37                 puts("    Not tainted");
38         }
39 }
40 #else
41 static void check_tainted(void) { putchar('\n'); }
42 #endif
43
44 int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
46 {
47 #if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
48         char *token[4];
49         parser_t *parser = config_open("/proc/modules");
50 #if ENABLE_FEATURE_ASSUME_UNICODE
51         size_t name_len;
52 #endif
53         check_unicode_in_env();
54
55         printf("%-24sSize  Used by", "Module");
56         check_tainted();
57
58         if (ENABLE_FEATURE_2_4_MODULES
59          && get_linux_version_code() < KERNEL_VERSION(2,6,0)
60         ) {
61                 while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
62                         if (token[3] != NULL && token[3][0] == '[') {
63                                 token[3]++;
64                                 token[3][strlen(token[3])-1] = '\0';
65                         } else
66                                 token[3] = (char *) "";
67 #if ENABLE_FEATURE_ASSUME_UNICODE
68                         name_len = bb_mbstrlen(token[0]);
69                         name_len = (name_len > 19) ? 0 : 19 - name_len;
70                         printf("%s%*s %8s %2s %s\n", token[0], name_len, "", token[1], token[2], token[3]);
71 #else
72                         printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
73 #endif
74                 }
75         } else {
76                 while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
77                         // N.B. token[3] is either '-' (module is not used by others)
78                         // or comma-separated list ended by comma
79                         // so trimming the trailing char is just what we need!
80                         token[3][strlen(token[3])-1] = '\0';
81 #if ENABLE_FEATURE_ASSUME_UNICODE
82                         name_len = bb_mbstrlen(token[0]);
83                         name_len = (name_len > 19) ? 0 : 19 - name_len;
84                         printf("%s%*s %8s %2s %s\n", token[0], name_len, "", token[1], token[2], token[3]);
85 #else
86                         printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
87 #endif
88                 }
89         }
90         if (ENABLE_FEATURE_CLEAN_UP)
91                 config_close(parser);
92 #else
93         check_tainted();
94         xprint_and_close_file(xfopen_for_read("/proc/modules"));
95 #endif
96         return EXIT_SUCCESS;
97 }