Mercurial > hg > toybox
annotate toys/pending/lspci.c @ 1289:9b3818b2a054 draft
Cleanup pass on lspci
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 18 May 2014 13:08:36 -0500 |
parents | 8caeba551a28 |
children | 5f59ec6e38ff |
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 |
1289 | 4 USE_LSPCI(NEWTOY(lspci, "emk"USE_LSPCI_TEXT("n@"), 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 |
14 -e Print all 6 digits in class (like elspci) | |
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 |
1289 | 23 usage: lspci [-n] |
24 | |
25 -n Numeric output (repeat for readable and numeric) | |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
26 */ |
1289 | 27 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
28 #define FOR_lspci |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
29 #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
|
30 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
31 GLOBALS( |
1289 | 32 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
|
33 |
1289 | 34 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
|
35 ) |
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 char *id_check_match(char *id, char *buf) |
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 int i = 0; |
1289 | 40 |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
41 while (id[i]) { |
1289 | 42 if (id[i] == buf[i]) i++; |
43 else return 0; | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
44 } |
1289 | 45 return buf + i + 2; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
46 } |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
47 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
48 /* |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
49 * In: vendid, devid, fil |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
50 * Out: vname, devname |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
51 * Out must be zeroed before use. |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
52 * vmame and devname must be char[256], zeroed out |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
53 * Returns (2 - number of IDs matched): vendor must be matched for |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
54 * dev to be matched |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
55 */ |
1289 | 56 int find_in_db(char *vendid, char *devid, FILE *fil, char *vname, char *devname) |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
57 { |
1289 | 58 char buf[256], *vtext = 0L, *dtext = 0L; |
59 | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
60 fseek(fil, 0, SEEK_SET); |
1289 | 61 while (!*vname) { |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
62 //loop through |
1289 | 63 if (!fgets(buf, 255, fil)) return 2; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
64 if ((vtext = id_check_match(vendid, buf))) |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
65 strncpy(vname, vtext, strlen(vtext) - 1); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
66 } |
1289 | 67 while (!*devname) { |
68 if (!fgets(buf, 255, fil) || *buf != '\t') return 1; | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
69 if ((dtext = id_check_match(devid, buf + 1))) |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
70 strncpy(devname, dtext, strlen(dtext) - 1); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
71 } |
1289 | 72 |
73 // matched both | |
74 return 0; | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
75 } |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
76 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
77 int do_lspci(struct dirtree *new) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
78 { |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
79 int alen = 8, dirfd, res = 2; //no textual descriptions read |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
80 char *dname = dirtree_path(new, &alen); |
1289 | 81 |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
82 memset(toybuf, 0, 4096); |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
83 struct { |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
84 char class[16], vendor[16], device[16], module[256], |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
85 vname[256], devname[256]; |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
86 } *bufs = (void*)(toybuf + 2); |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
87 |
1289 | 88 if (!new->parent) return DIRTREE_RECURSE; |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
89 errno = 0; |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
90 dirfd = open(dname, O_RDONLY); |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
91 if (dirfd > 0) { |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
92 char *p, **fields = (char*[]){"class", "vendor", "device", ""}; |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
93 |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
94 for (p = toybuf; **fields; p+=16, fields++) { |
1289 | 95 int fd, size = ((toys.optflags & FLAG_e) && p == toybuf) ? 8 : 6; |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
96 |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
97 if ((fd = openat(dirfd, *fields, O_RDONLY)) < 0) continue; |
1289 | 98 xread(fd, p, size); |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
99 close(fd); |
1289 | 100 |
101 p[size] = 0; | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
102 } |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
103 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
104 close(dirfd); |
1289 | 105 if (errno) return 0; |
106 | |
107 { | |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
108 char *driver = ""; |
1289 | 109 char *fmt = (toys.optflags & FLAG_m) ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n" |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
110 : "%s Class %s: %s:%s %s\n"; |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
111 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
112 if (toys.optflags & FLAG_k) { |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
113 strcat(dname, "/driver"); |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
114 if (readlink(dname, bufs->module, sizeof(bufs->module)) != -1) |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
115 driver = basename(bufs->module); |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
116 } |
1289 | 117 if (CFG_LSPCI_TEXT && TT.numeric != 1) { |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
118 res = find_in_db(bufs->vendor, bufs->device, TT.db, |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
119 bufs->vname, bufs->devname); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
120 } |
1289 | 121 if (CFG_LSPCI_TEXT && TT.numeric > 1) { |
122 fmt = (toys.optflags & FLAG_m) | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
123 ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\" \"%s\"\n" |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
124 : "%s Class %s: %s [%s] %s [%s] %s\n"; |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
125 printf(fmt, new->name + 5, bufs->class, bufs->vname, bufs->vendor, |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
126 bufs->devname, bufs->device, driver); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
127 } else { |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
128 printf(fmt, new->name + 5, bufs->class, |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
129 (res < 2) ? bufs->vname : bufs->vendor, |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
130 !(res) ? bufs->devname : bufs->device, driver); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
131 } |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
132 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
133 } |
1289 | 134 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
135 return 0; |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
136 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
137 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
138 void lspci_main(void) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
139 { |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
140 if (CFG_LSPCI_TEXT && (TT.numeric != 1)) { |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
141 TT.db = fopen("/usr/share/misc/pci.ids", "r"); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
142 if (errno) { |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
143 TT.numeric = 1; |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
144 error_msg("could not open PCI ID db"); |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
145 } |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
146 } |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
147 |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
148 dirtree_read("/sys/bus/pci/devices", do_lspci); |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
149 } |