aboutsummaryrefslogtreecommitdiff
path: root/toys/other
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-06-23 14:38:31 -0500
committerRob Landley <rob@landley.net>2013-06-23 14:38:31 -0500
commit10217d386975fe4456bb98e9195096165da4119d (patch)
tree6d3402f3bdc7b8395bf98a85802413625365c829 /toys/other
parent9d5456c70b549e8eded8f634fcdad999b655c205 (diff)
downloadtoybox-10217d386975fe4456bb98e9195096165da4119d.tar.gz
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).
Diffstat (limited to 'toys/other')
-rw-r--r--toys/other/modinfo.c62
1 files 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<toys.optc; TT.mod++) {
+ char *s = strstr(toys.optargs[TT.mod], ".ko");
- if (uname(&uts) < 0) perror_exit("bad uname");
- sprintf(toybuf, "/lib/modules/%s", uts.release);
+ if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
+ else {
+ struct utsname uts;
- 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 (uname(&uts) < 0) perror_exit("bad uname");
+ sprintf(toybuf, "/lib/modules/%s", uts.release);
dirtree_read(toybuf, check_module);
}
}