annotate toys/pending/lspci.c @ 976:f84dd3473233

Fix lspci -e
author Felix Janda <felix.janda@posteo.de>
date Sat, 27 Jul 2013 09:45:40 +0200
parents a47c6658210c
children 8caeba551a28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
4 USE_LSPCI(NEWTOY(lspci, "emkns:", 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
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
10 usage: lspci [-ekmn]
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.
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
13 -e Print all 6 digits in class (like elspci)
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
14 -k Print kernel driver
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
15 -m Machine parseable format
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
16 -n Numeric output (default)
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
17 */
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
18 #define FOR_lspci
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
19 #include "toys.h"
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
20
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
21 int do_lspci(struct dirtree *new)
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
22 {
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
23 int alen = 8, dirfd;
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
24 char *dname = dirtree_path(new, &alen);
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
25 struct {
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
26 char class[16], vendor[16], device[16], module[256];
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
27 } *bufs = (void*)(toybuf + 2);
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
28
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
29 if (!strcmp("/sys/bus/pci/devices", dname)) return DIRTREE_RECURSE;
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
30 errno = 0;
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
31 dirfd = open(dname, O_RDONLY);
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
32 if (dirfd > 0) {
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
33 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
34
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
35 for (p = toybuf; **fields; p+=16, fields++) {
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
36 int fd, size;
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
37
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
38 if ((fd = openat(dirfd, *fields, O_RDONLY)) < 0) continue;
976
f84dd3473233 Fix lspci -e
Felix Janda <felix.janda@posteo.de>
parents: 969
diff changeset
39 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
40 p[read(fd, p, size)] = '\0';
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
41 close(fd);
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
42 }
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
43
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
44 close(dirfd);
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
45 if (!errno) {
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
46 char *driver = "";
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
47 char *fmt = toys.optflags & FLAG_m ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n"
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
48 : "%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
49
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
50 if (toys.optflags & FLAG_k) {
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
51 strcat(dname, "/driver");
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
52 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
53 driver = basename(bufs->module);
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
54 }
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
55 printf(fmt, new->name + 5, bufs->class, bufs->vendor, bufs->device,
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
56 driver);
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
57 }
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
58 }
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
59 return 0;
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
60 }
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
61
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
62 void lspci_main(void)
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
63 {
969
a47c6658210c lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents: 968
diff changeset
64 dirtree_read("/sys/bus/pci/devices", do_lspci);
968
b2b4bd208b65 I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff changeset
65 }