comparison toys/other/modinfo.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 37e668afd008
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* modinfo.c - Display module info
2 *
3 * modinfo.c - Display module info
4 * 2 *
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> 3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6 *
7 4
8 USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN)) 5 USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN))
9 6
10 config MODINFO 7 config MODINFO
11 bool "modinfo" 8 bool "modinfo"
12 default y 9 default y
13 help 10 help
14 usage: modinfo [-0] [-F field] [modulename...] 11 usage: modinfo [-0] [-F field] [modulename...]
15 */ 12 */
16 13
17 #define FOR_modinfo 14 #define FOR_modinfo
18 #include "toys.h" 15 #include "toys.h"
19 16
20 GLOBALS( 17 GLOBALS(
21 char *field; 18 char *field;
22 ) 19 )
23 20
24 static const char *modinfo_tags[] = { 21 static const char *modinfo_tags[] = {
25 "alias", "license", "description", "author", "vermagic", 22 "alias", "license", "description", "author", "vermagic",
26 "srcversion", "intree", "parm", "depends", 23 "srcversion", "intree", "parm", "depends",
27 }; 24 };
28 25
29 static void output_field(const char *field, const char *value) 26 static void output_field(const char *field, const char *value)
30 { 27 {
31 int len; 28 int len;
32 29
33 if (TT.field && strcmp(TT.field, field) != 0) 30 if (TT.field && strcmp(TT.field, field) != 0) return;
34 return;
35 31
36 len = strlen(field); 32 len = strlen(field);
37 33
38 if (TT.field) 34 if (TT.field) xprintf("%s", value);
39 xprintf("%s", value); 35 else xprintf("%s:%*s%s", field, 15 - len, "", value);
40 else 36 if (toys.optflags & FLAG_0) xwrite(fileno(stdout), "\0", 1);
41 xprintf("%s:%*s%s", 37 else xputs("");
42 field, 15 - len, "", value);
43 if (toys.optflags & FLAG_0)
44 xwrite(fileno(stdout), "\0", 1);
45 else
46 xputs("");
47 } 38 }
48 39
49 40
50 static void modinfo_file(struct dirtree *dir) 41 static void modinfo_file(struct dirtree *dir)
51 { 42 {
52 int fd, len, i; 43 int fd, len, i;
53 char *buf, *pos; 44 char *buf, *pos;
54 char *full_name; 45 char *full_name;
55 46
56 full_name = dirtree_path(dir, NULL); 47 full_name = dirtree_path(dir, NULL);
57 48
58 output_field("filename", full_name); 49 output_field("filename", full_name);
59 fd = xopen(full_name, O_RDONLY); 50 fd = xopen(full_name, O_RDONLY);
60 len = fdlength(fd); 51 len = fdlength(fd);
61 buf = xmalloc(len); 52 buf = xmalloc(len);
62 xreadall(fd, buf, len); 53 xreadall(fd, buf, len);
63 54
64 for (pos = buf; pos < buf + len + 10; pos++) { 55 for (pos = buf; pos < buf + len + 10; pos++) {
65 if (*pos) 56 if (*pos) continue;
66 continue;
67 57
68 for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) { 58 for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) {
69 const char *str = modinfo_tags[i]; 59 const char *str = modinfo_tags[i];
70 int len = strlen(str); 60 int len = strlen(str);
71 if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=') 61 if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=')
72 output_field(str, &pos[len + 2]); 62 output_field(str, &pos[len + 2]);
73 }
74 } 63 }
64 }
75 65
76 free(full_name); 66 free(full_name);
77 free(buf); 67 free(buf);
78 close(fd); 68 close(fd);
79 } 69 }
80 70
81 static int check_module(struct dirtree *new) 71 static int check_module(struct dirtree *new)
82 { 72 {
83 if (S_ISREG(new->st.st_mode)) { 73 if (S_ISREG(new->st.st_mode)) {
84 char **s; 74 char **s;
85 for (s = toys.optargs; *s; s++) { 75 for (s = toys.optargs; *s; s++) {
86 int len = strlen(*s); 76 int len = strlen(*s);
87 if (!strncmp(*s, new->name, len) && !strcmp(new->name+len, ".ko")) 77 if (!strncmp(*s, new->name, len) && !strcmp(new->name+len, ".ko"))
88 modinfo_file(new); 78 modinfo_file(new);
89 }
90 } 79 }
80 }
91 81
92 return dirtree_notdotdot(new); 82 return dirtree_notdotdot(new);
93 } 83 }
94 84
95 void modinfo_main(void) 85 void modinfo_main(void)
96 { 86 {
97 struct utsname uts; 87 struct utsname uts;
98 if (uname(&uts) < 0) perror_exit("bad uname"); 88
99 sprintf(toybuf, "/lib/modules/%s", uts.release); 89 if (uname(&uts) < 0) perror_exit("bad uname");
100 dirtree_read(toybuf, check_module); 90 sprintf(toybuf, "/lib/modules/%s", uts.release);
91 dirtree_read(toybuf, check_module);
101 } 92 }