2 * lspci - written by Isaac Dunham
4 USE_LSPCI(NEWTOY(lspci, "emkn"USE_LSPCI_TEXT("@i:"), TOYFLAG_USR|TOYFLAG_BIN))
14 -e Print all 6 digits in class
15 -k Print kernel driver
16 -m Machine parseable format
19 bool "lspci readable output"
23 usage: lspci [-n] [-i FILE ]
25 -n Numeric output (repeat for readable and numeric)
26 -i PCI ID database (default /usr/share/misc/pci.ids)
40 static int do_lspci(struct dirtree *new)
42 char *p = toybuf, *vendor = toybuf+9, *device = toybuf+18,
43 driver[256], *vbig = 0, *dbig = 0, **fields;
46 if (!new->parent) return DIRTREE_RECURSE;
48 // Parse data out of /proc
50 if (-1 == (dirfd = openat(dirtree_parentfd(new), new->name, O_RDONLY)))
54 if (toys.optflags & FLAG_k)
55 readlinkat0(dirfd, "driver", driver, sizeof(driver));
57 for (fields = (char*[]){"class", "vendor", "device", 0}; *fields; fields++) {
58 int fd, size = 6 + 2*((toys.optflags & FLAG_e) && p == toybuf);
61 if (-1 == (fd = openat(dirfd, *fields, O_RDONLY))) {
65 xreadall(fd, p, size);
66 memmove(p, p+2, size -= 2);
74 // Lookup/display data from pci.ids?
76 if (CFG_LSPCI_TEXT && TT.db) {
77 if (TT.numeric != 1) {
80 fseek(TT.db, 0, SEEK_SET);
81 while (!vbig || !dbig) {
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)) {
91 s[-1] = 0; // trim ending newline
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);
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);
114 printf((toys.optflags & FLAG_m) ? " \"%s\"" : " %s", basename(driver));
120 void lspci_main(void)
122 if (CFG_LSPCI_TEXT && TT.numeric != 1) {
123 if (!TT.ids) TT.ids = "/usr/share/misc/pci.ids";
124 if (!(TT.db = fopen(TT.ids, "r"))) perror_msg("%s", TT.ids);
127 dirtree_read("/sys/bus/pci/devices", do_lspci);