From 10217d386975fe4456bb98e9195096165da4119d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 23 Jun 2013 14:38:31 -0500 Subject: Modinfo cleanups. Don't use xopen() if you want to iterate through multiple files. Don't abort if unable to open the file, but return error if it can't map it. (And leak the filehandle.) All modinfo_file() actually uses is the filename, no reason to go through dirtree() for that. Nothing is actually _checking_ the return value of modinfo_file(). Avoid global data outside of toy_union. Make sure extension is at end of file (we can add support for more extensions later). --- toys/other/modinfo.c | 62 ++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/toys/other/modinfo.c b/toys/other/modinfo.c index 77be2d42..3548247d 100644 --- a/toys/other/modinfo.c +++ b/toys/other/modinfo.c @@ -20,40 +20,34 @@ GLOBALS( long mod; ) -static char *modinfo_tags[] = { - "alias", "license", "description", "author", "firmware", - "vermagic", "srcversion", "intree", "parm", "depends", -}; - static void output_field(char *field, char *value) { - int len; - - 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 (!TT.field) xprintf("%s:%*c", field, 15 - strlen(field), ' '); + else if (!strcmp(TT.field, field)) return; + xprintf("%s", value); xputc((toys.optflags & FLAG_0) ? 0 : '\n'); } -static int modinfo_file(struct dirtree *dir) +static void modinfo_file(char *full_name) { int fd, len, i; - 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); + char *buf = 0, *pos, *modinfo_tags[] = { + "alias", "license", "description", "author", "firmware", + "vermagic", "srcversion", "intree", "parm", "depends", + }; + + if (-1 != (fd = open(full_name, O_RDONLY))) { + len = fdlength(fd); + if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) close(fd); + } - len = fdlength(fd); - if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) { - perror_msg("mmap %s", full_name); - return 1; + if (!buf) { + perror_msg("%s", full_name); + return; } + output_field("filename", full_name); + for (pos = buf; pos < buf+len; pos++) { if (*pos) continue; @@ -68,7 +62,6 @@ static int modinfo_file(struct dirtree *dir) munmap(buf, len); close(fd); - return 0; } static int check_module(struct dirtree *new) @@ -87,7 +80,10 @@ static int check_module(struct dirtree *new) } if (s[len] || strcmp(new->name+len, ".ko")) break; - modinfo_file(new); + modinfo_file(s = dirtree_path(new, NULL)); + free(s); + + return DIRTREE_ABORT; } } @@ -96,15 +92,15 @@ static int check_module(struct dirtree *new) void modinfo_main(void) { - struct utsname uts; + for(TT.mod = 0; TT.mod