changeset 934:d0ab54f62346

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).
author Rob Landley <rob@landley.net>
date Sun, 23 Jun 2013 14:38:31 -0500
parents a81921ce4930
children d952f44620d4
files toys/other/modinfo.c
diffstat 1 files changed, 30 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/modinfo.c	Sun Jun 23 14:02:16 2013 -0500
+++ b/toys/other/modinfo.c	Sun Jun 23 14:38:31 2013 -0500
@@ -20,39 +20,33 @@
   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;
+  char *buf = 0, *pos, *modinfo_tags[] = {
+    "alias", "license", "description", "author", "firmware",
+    "vermagic", "srcversion", "intree", "parm", "depends",
+  };
 
-  full_name = dirtree_path(dir, NULL);
+  if (-1 != (fd = open(full_name, O_RDONLY))) {
+    len = fdlength(fd);
+    if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) close(fd);
+  }
+
+  if (!buf) {
+    perror_msg("%s", full_name);
+    return;
+  } 
+
   output_field("filename", full_name);
-  fd = xopen(full_name, O_RDONLY);
-  free(full_name);
-
-  len = fdlength(fd);
-  if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0))) {
-    perror_msg("mmap %s", full_name);
-    return 1;
-  } 
 
   for (pos = buf; pos < buf+len; pos++) {
     if (*pos) continue;
@@ -68,7 +62,6 @@
 
   munmap(buf, len);
   close(fd);
-  return 0;
 }
 
 static int check_module(struct dirtree *new)
@@ -87,7 +80,10 @@
       }
       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 @@
 
 void modinfo_main(void)
 {
-  struct utsname uts;
-
-  if (uname(&uts) < 0) perror_exit("bad uname");
-  sprintf(toybuf, "/lib/modules/%s", uts.release);
+  for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
+    char *s = strstr(toys.optargs[TT.mod], ".ko");
 
-  for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
-    if (strstr(toys.optargs[TT.mod], ".ko")) { 
-      dirtree_read(toys.optargs[TT.mod], modinfo_file);
-    } else {
+    if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
+    else {
+      struct utsname uts;
+
+      if (uname(&uts) < 0) perror_exit("bad uname");
+      sprintf(toybuf, "/lib/modules/%s", uts.release);
       dirtree_read(toybuf, check_module);
     }
   }