Mercurial > hg > toybox
annotate toys/other/modinfo.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
rev | line source |
---|---|
620 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * modinfo.c - Display module info | |
4 * | |
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> | |
6 * | |
7 | |
8 USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN)) | |
9 | |
10 config MODINFO | |
11 bool "modinfo" | |
12 default y | |
13 help | |
14 usage: modinfo [-0] [-F field] [modulename...] | |
15 */ | |
16 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
17 #define FOR_modinfo |
620 | 18 #include "toys.h" |
19 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
20 GLOBALS( |
620 | 21 char *field; |
22 ) | |
23 | |
24 static const char *modinfo_tags[] = { | |
25 "alias", "license", "description", "author", "vermagic", | |
26 "srcversion", "intree", "parm", "depends", | |
27 }; | |
28 | |
29 static void output_field(const char *field, const char *value) | |
30 { | |
31 int len; | |
32 | |
33 if (TT.field && strcmp(TT.field, field) != 0) | |
34 return; | |
35 | |
36 len = strlen(field); | |
37 | |
38 if (TT.field) | |
39 xprintf("%s", value); | |
40 else | |
41 xprintf("%s:%*s%s", | |
42 field, 15 - len, "", value); | |
43 if (toys.optflags & FLAG_0) | |
44 xwrite(fileno(stdout), "\0", 1); | |
45 else | |
46 xputs(""); | |
47 } | |
48 | |
49 | |
50 static void modinfo_file(struct dirtree *dir) | |
51 { | |
52 int fd, len, i; | |
53 char *buf, *pos; | |
54 char *full_name; | |
55 | |
56 full_name = dirtree_path(dir, NULL); | |
57 | |
58 output_field("filename", full_name); | |
59 fd = xopen(full_name, O_RDONLY); | |
60 len = fdlength(fd); | |
61 buf = xmalloc(len); | |
62 xreadall(fd, buf, len); | |
63 | |
64 for (pos = buf; pos < buf + len + 10; pos++) { | |
65 if (*pos) | |
66 continue; | |
67 | |
68 for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) { | |
69 const char *str = modinfo_tags[i]; | |
70 int len = strlen(str); | |
71 if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=') | |
72 output_field(str, &pos[len + 2]); | |
73 } | |
74 } | |
75 | |
76 free(full_name); | |
77 free(buf); | |
78 close(fd); | |
79 } | |
80 | |
81 static int check_module(struct dirtree *new) | |
82 { | |
83 if (S_ISREG(new->st.st_mode)) { | |
84 char **s; | |
85 for (s = toys.optargs; *s; s++) { | |
86 int len = strlen(*s); | |
632 | 87 if (!strncmp(*s, new->name, len) && !strcmp(new->name+len, ".ko")) |
620 | 88 modinfo_file(new); |
89 } | |
90 } | |
91 | |
92 return dirtree_notdotdot(new); | |
93 } | |
94 | |
95 void modinfo_main(void) | |
96 { | |
97 struct utsname uts; | |
632 | 98 if (uname(&uts) < 0) perror_exit("bad uname"); |
620 | 99 sprintf(toybuf, "/lib/modules/%s", uts.release); |
100 dirtree_read(toybuf, check_module); | |
101 } |