Mercurial > hg > toybox
annotate toys/pending/lspci.c @ 1431:e36306d6eece draft
Cleanup pass on lspci.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 11 Aug 2014 20:11:58 -0500 |
parents | bc618abf4fb2 |
children |
rev | line source |
---|---|
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
1 /* |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
2 * lspci - written by Isaac Dunham |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
3 |
1293
5f59ec6e38ff
lspci text output: add -i, allow -n when disabled
Isaac Dunham <ibid.ag@gmail.com>
parents:
1289
diff
changeset
|
4 USE_LSPCI(NEWTOY(lspci, "emkn"USE_LSPCI_TEXT("@i:"), TOYFLAG_USR|TOYFLAG_BIN)) |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
5 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
6 config LSPCI |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
7 bool "lspci" |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
8 default n |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
9 help |
1289 | 10 usage: lspci [-ekm] |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
11 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
12 List PCI devices. |
1289 | 13 |
1431 | 14 -e Print all 6 digits in class |
1289 | 15 -k Print kernel driver |
16 -m Machine parseable format | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
17 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
18 config LSPCI_TEXT |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
19 bool "lspci readable output" |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
20 depends on LSPCI |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
21 default n |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
22 help |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
23 usage: lspci [-n] [-i FILE ] |
1289 | 24 |
25 -n Numeric output (repeat for readable and numeric) | |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
26 -i PCI ID database (default /usr/share/misc/pci.ids) |
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
27 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
28 */ |
1289 | 29 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
30 #define FOR_lspci |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
31 #include "toys.h" |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
32 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
33 GLOBALS( |
1293
5f59ec6e38ff
lspci text output: add -i, allow -n when disabled
Isaac Dunham <ibid.ag@gmail.com>
parents:
1289
diff
changeset
|
34 char *ids; |
1289 | 35 long numeric; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
36 |
1289 | 37 FILE *db; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
38 ) |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
39 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
40 int do_lspci(struct dirtree *new) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
41 { |
1431 | 42 char *p = toybuf, *vendor = toybuf+9, *device = toybuf+18, |
43 driver[256], *vbig = 0, *dbig = 0, **fields; | |
44 int dirfd; | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
45 |
1289 | 46 if (!new->parent) return DIRTREE_RECURSE; |
1431 | 47 |
48 // Parse data out of /proc | |
49 | |
50 if (-1 == (dirfd = openat(dirtree_parentfd(new), new->name, O_RDONLY))) | |
51 return 0; | |
52 | |
53 *driver = 0; | |
54 if (toys.optflags & FLAG_k) | |
55 readlinkat(dirfd, "driver", driver, sizeof(driver)); | |
56 | |
57 for (fields = (char*[]){"class", "vendor", "device", 0}; *fields; fields++) { | |
58 int fd, size = 6 + 2*((toys.optflags & FLAG_e) && p == toybuf); | |
59 *p = 0; | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
60 |
1431 | 61 if (-1 == (fd = openat(dirfd, *fields, O_RDONLY))) { |
62 close(dirfd); | |
63 return 0; | |
64 } | |
65 xreadall(fd, p, size); | |
66 memmove(p, p+2, size -= 2); | |
67 p[size] = 0; | |
68 close(fd); | |
69 p += 9; | |
70 } | |
71 | |
72 close(dirfd); | |
73 | |
74 // Lookup/display data from pci.ids? | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
75 |
1431 | 76 if (CFG_LSPCI_TEXT && TT.db) { |
77 if (TT.numeric != 1) { | |
78 char *s; | |
1289 | 79 |
1431 | 80 fseek(TT.db, 0, SEEK_SET); |
81 while (!vbig || !dbig) { | |
82 s = p; | |
83 if (!fgets(s, sizeof(toybuf)-(p-toybuf)-1, TT.db)) break; | |
84 while (isspace(*s)) s++; | |
85 if (*s == '#') continue; | |
86 if (vbig && s == p) break; | |
87 if (strstart(&s, vbig ? device : vendor)) { | |
88 if (vbig) dbig = s+2; | |
89 else vbig = s+2; | |
90 s += strlen(s); | |
91 s[-1] = 0; // trim ending newline | |
92 p = s + 1; | |
93 } | |
94 } | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
95 } |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
96 |
1431 | 97 if (TT.numeric > 1) { |
98 printf((toys.optflags & FLAG_m) | |
99 ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\"" | |
100 : "%s Class %s: %s [%s] %s [%s]", | |
101 new->name+5, toybuf, vbig ? vbig : "", vendor, | |
102 dbig ? dbig : "", device); | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
103 |
1431 | 104 goto driver; |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
105 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
106 } |
1289 | 107 |
1431 | 108 printf((toys.optflags & FLAG_m) ? "%s \"%s\" \"%s\" \"%s\"" |
109 : "%s Class %s: %s:%s", new->name+5, toybuf, | |
110 vbig ? vbig : vendor, dbig ? dbig : device); | |
111 | |
112 driver: | |
113 if (*driver) | |
114 printf((toys.optflags & FLAG_m) ? " \"%s\"" : " %s", basename(driver)); | |
115 xputc('\n'); | |
116 | |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
117 return 0; |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
118 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
119 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
120 void lspci_main(void) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
121 { |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
122 if (CFG_LSPCI_TEXT && TT.numeric != 1) { |
1431 | 123 if (!TT.ids) TT.ids = "/usr/share/misc/pci.ids"; |
124 if (!(TT.db = fopen(TT.ids, "r"))) | |
125 perror_msg("could not open PCI ID db"); | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
126 } |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
127 |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
128 dirtree_read("/sys/bus/pci/devices", do_lspci); |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
129 } |