diff options
author | Rob Landley <rob@landley.net> | 2013-04-24 03:04:31 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2013-04-24 03:04:31 -0500 |
commit | 4cf2a1ccff7cd6df59782ee0d5673f4969671ca9 (patch) | |
tree | 457c2a7d0fa840acde96854c45dcc3f82a1941a3 /toys/other/modinfo.c | |
parent | c732dce980dfec2c34b4d9bee131fb87d2f1557f (diff) | |
download | toybox-4cf2a1ccff7cd6df59782ee0d5673f4969671ca9.tar.gz |
Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
Diffstat (limited to 'toys/other/modinfo.c')
-rw-r--r-- | toys/other/modinfo.c | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/toys/other/modinfo.c b/toys/other/modinfo.c index 36a38382..66cd09fe 100644 --- a/toys/other/modinfo.c +++ b/toys/other/modinfo.c @@ -18,64 +18,72 @@ GLOBALS( char *field; ) -static const char *modinfo_tags[] = { +static char *modinfo_tags[] = { "alias", "license", "description", "author", "vermagic", "srcversion", "intree", "parm", "depends", }; -static void output_field(const char *field, const char *value) +static void output_field(char *field, char *value) { int len; - if (TT.field && strcmp(TT.field, field) != 0) return; + if (TT.field && strcmp(TT.field, field)) return; len = strlen(field); if (TT.field) xprintf("%s", value); else xprintf("%s:%*s%s", field, 15 - len, "", value); - if (toys.optflags & FLAG_0) xwrite(fileno(stdout), "\0", 1); - else xputs(""); + xputc((toys.optflags & FLAG_0) ? 0 : '\n'); } - static void modinfo_file(struct dirtree *dir) { int fd, len, i; - char *buf, *pos; - char *full_name; + char *buf, *pos, *full_name; full_name = dirtree_path(dir, NULL); - output_field("filename", full_name); fd = xopen(full_name, O_RDONLY); + free(full_name); + len = fdlength(fd); - buf = xmalloc(len); - xreadall(fd, buf, len); + if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) + perror_exit("mmap %s", full_name); - for (pos = buf; pos < buf + len + 10; pos++) { + for (pos = buf; pos < buf+len; pos++) { if (*pos) continue; - for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) { - const char *str = modinfo_tags[i]; + for (i = 0; i < sizeof(modinfo_tags) / sizeof(*modinfo_tags); i++) { + char *str = modinfo_tags[i]; int len = strlen(str); - if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=') - output_field(str, &pos[len + 2]); + + if (!strncmp(pos+1, str, len) && pos[len+1] == '=') + output_field(str, pos+len+2); } } - free(full_name); - free(buf); + munmap(buf, len); close(fd); } static int check_module(struct dirtree *new) { if (S_ISREG(new->st.st_mode)) { - char **s; - for (s = toys.optargs; *s; s++) { - int len = strlen(*s); - if (!strncmp(*s, new->name, len) && !strcmp(new->name+len, ".ko")) - modinfo_file(new); + char **ss; + + for (ss = toys.optargs; *ss; ss++) { + char *s = *ss; + int len = 0; + + // The kernel treats - and _ the same, so we should too. + for (len = 0; s[len]; len++) { + if (s[len] == '-' && new->name[len] == '_') continue; + if (s[len] == '_' && new->name[len] == '-') continue; + if (s[len] != new->name[len]) break; + } + if (s[len] || strcmp(new->name+len, ".ko")) break; + + modinfo_file(new); } } @@ -88,5 +96,6 @@ void modinfo_main(void) if (uname(&uts) < 0) perror_exit("bad uname"); sprintf(toybuf, "/lib/modules/%s", uts.release); + dirtree_read(toybuf, check_module); } |