Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/modutils/modinfo.c
- Timestamp:
- Dec 20, 2016, 4:07:32 PM (7 years ago)
- Location:
- branches/3.3
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3/mindi-busybox/modutils/modinfo.c
r3232 r3621 6 6 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 7 */ 8 9 //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP))10 11 //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o12 13 8 //config:config MODINFO 14 9 //config: bool "modinfo" … … 18 13 //config: Show information about a Linux Kernel module 19 14 15 //applet:IF_MODINFO(APPLET(modinfo, BB_DIR_SBIN, BB_SUID_DROP)) 16 17 //kbuild:lib-$(CONFIG_MODINFO) += modinfo.o modutils.o 18 20 19 #include <fnmatch.h> 21 20 #include <sys/utsname.h> /* uname() */ … … 23 22 #include "modutils.h" 24 23 24 static const char *const shortcuts[] = { 25 "filename", // -n 26 "author", // -a 27 "description", // -d 28 "license", // -l 29 "parm", // -p 30 "version", // the rest has no shortcut options 31 "alias", 32 "srcversion", 33 "depends", 34 "uts_release", 35 "intree", 36 "vermagic", 37 "firmware", 38 }; 25 39 26 40 enum { 27 OPT_TAGS = (1 << 12) - 1, /* shortcut count */ 28 OPT_F = (1 << 12), /* field name */ 29 OPT_0 = (1 << 13), /* \0 as separator */ 41 OPT_0 = (1 << 0), /* \0 as separator */ 42 OPT_F = (1 << 1), /* field name */ 43 /* first bits are for -nadlp options, the rest are for 44 * fields not selectable with "shortcut" options 45 */ 46 OPT_n = (1 << 2), 47 OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 2, 30 48 }; 31 49 32 struct modinfo_env { 33 char *field; 34 int tags; 35 }; 36 37 static int display(const char *data, const char *pattern, int flag) 50 static void display(const char *data, const char *pattern) 38 51 { 39 if (flag) { 52 int flag = option_mask32 >> 1; /* shift out -0 bit */ 53 if (flag & (flag-1)) { 54 /* more than one field to show: print "FIELD:" pfx */ 40 55 int n = printf("%s:", pattern); 41 56 while (n++ < 16) 42 57 bb_putchar(' '); 43 58 } 44 returnprintf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n');59 printf("%s%c", data, (option_mask32 & OPT_0) ? '\0' : '\n'); 45 60 } 46 61 47 62 static void modinfo(const char *path, const char *version, 48 const struct modinfo_env *env)63 const char *field) 49 64 { 50 static const char *const shortcuts[] = {51 "filename",52 "license",53 "author",54 "description",55 "version",56 "alias",57 "srcversion",58 "depends",59 "uts_release",60 "vermagic",61 "parm",62 "firmware",63 };64 65 size_t len; 65 int j , length;66 int j; 66 67 char *ptr, *the_module; 67 c onst char *field = env->field;68 int tags = env->tags;68 char *allocated; 69 int tags = option_mask32; 69 70 70 if (tags & 1) { /* filename */ 71 display(path, shortcuts[0], 1 != tags); 72 } 73 71 allocated = NULL; 74 72 len = MAXINT(ssize_t); 75 73 the_module = xmalloc_open_zipped_read_close(path, &len); … … 78 76 return; 79 77 /* Newer depmod puts relative paths in modules.dep */ 80 path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path);78 path = allocated = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); 81 79 the_module = xmalloc_open_zipped_read_close(path, &len); 82 free((char*)path); 83 if (!the_module) 84 return; 80 if (!the_module) { 81 bb_error_msg("module '%s' not found", path); 82 goto ret; 83 } 85 84 } 86 85 87 if (field) 88 tags |= OPT_F; 89 for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) { 86 for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) { 90 87 const char *pattern; 91 88 92 89 if (!((1<<j) & tags)) 93 90 continue; 91 94 92 pattern = field; 95 93 if ((1<<j) & OPT_TAGS) 96 pattern = shortcuts[j]; 97 length = strlen(pattern); 94 pattern = shortcuts[j-2]; 95 96 if (strcmp(pattern, shortcuts[0]) == 0) { 97 /* "-n" or "-F filename" */ 98 display(path, shortcuts[0]); 99 continue; 100 } 101 98 102 ptr = the_module; 99 103 while (1) { 104 char *after_pattern; 105 100 106 ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); 101 107 if (ptr == NULL) /* no occurance left, done */ 102 108 break; 103 if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') { 109 after_pattern = is_prefixed_with(ptr, pattern); 110 if (after_pattern && *after_pattern == '=') { 104 111 /* field prefixes are 0x80 or 0x00 */ 105 if ((ptr[-1] & 0x7F) == '\0') { 106 ptr += length + 1; 107 ptr += display(ptr, pattern, (1<<j) != tags); 112 if ((ptr[-1] & 0x7F) == 0x00) { 113 ptr = after_pattern + 1; 114 display(ptr, pattern); 115 ptr += strlen(ptr); 108 116 } 109 117 } … … 112 120 } 113 121 free(the_module); 122 ret: 123 free(allocated); 114 124 } 115 125 116 126 //usage:#define modinfo_trivial_usage 117 //usage: "[-adlp 0] [-F keyword] MODULE"127 //usage: "[-adlpn0] [-F keyword] MODULE" 118 128 //usage:#define modinfo_full_usage "\n\n" 119 129 //usage: " -a Shortcut for '-F author'" … … 121 131 //usage: "\n -l Shortcut for '-F license'" 122 132 //usage: "\n -p Shortcut for '-F parm'" 133 ////usage: "\n -n Shortcut for '-F filename'" 123 134 //usage: "\n -F keyword Keyword to look for" 124 135 //usage: "\n -0 Separate output with NULs" … … 129 140 int modinfo_main(int argc UNUSED_PARAM, char **argv) 130 141 { 131 struct modinfo_env env;142 const char *field; 132 143 char name[MODULE_NAME_LEN]; 133 144 struct utsname uts; … … 137 148 unsigned i; 138 149 139 env.field = NULL;150 field = NULL; 140 151 opt_complementary = "-1"; /* minimum one param */ 141 opts = getopt32(argv, "nladvAsDumpF:0", &env.field); 142 env.tags = opts & OPT_TAGS ? opts & OPT_TAGS : OPT_TAGS; 152 opts = getopt32(argv, "0F:nadlp", &field); 153 /* If no field selected, show all */ 154 if (!(opts & (OPT_TAGS|OPT_F))) 155 option_mask32 |= OPT_TAGS; 143 156 argv += optind; 144 157 … … 154 167 continue; 155 168 *colon = '\0'; 156 filename2modname( tokens[0], name);169 filename2modname(bb_basename(tokens[0]), name); 157 170 for (i = 0; argv[i]; i++) { 158 171 if (fnmatch(argv[i], name, 0) == 0) { 159 modinfo(tokens[0], uts.release, &env);172 modinfo(tokens[0], uts.release, field); 160 173 argv[i] = (char *) ""; 161 174 } … … 167 180 for (i = 0; argv[i]; i++) { 168 181 if (argv[i][0]) { 169 modinfo(argv[i], uts.release, &env);182 modinfo(argv[i], uts.release, field); 170 183 } 171 184 }
Note:
See TracChangeset
for help on using the changeset viewer.