diff options
Diffstat (limited to 'toys/pending/lspci.c')
-rw-r--r-- | toys/pending/lspci.c | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/toys/pending/lspci.c b/toys/pending/lspci.c index 16fd0f60..b557e459 100644 --- a/toys/pending/lspci.c +++ b/toys/pending/lspci.c @@ -1,49 +1,48 @@ /* * lspci - written by Isaac Dunham -USE_LSPCI(NEWTOY(lspci, "emkn@", TOYFLAG_USR|TOYFLAG_BIN)) +USE_LSPCI(NEWTOY(lspci, "emk"USE_LSPCI_TEXT("n@"), TOYFLAG_USR|TOYFLAG_BIN)) config LSPCI bool "lspci" default n help - usage: lspci [-ekmn@] + usage: lspci [-ekm] List PCI devices. - -e Print all 6 digits in class (like elspci) - -k Print kernel driver - -m Machine parseable format - -n Numeric output + + -e Print all 6 digits in class (like elspci) + -k Print kernel driver + -m Machine parseable format config LSPCI_TEXT bool "lspci readable output" depends on LSPCI default n help - lspci without -n prints readable descriptions; - lspci -nn prints both readable and numeric description + usage: lspci [-n] + + -n Numeric output (repeat for readable and numeric) */ + #define FOR_lspci #include "toys.h" -extern int find_in_db(char * , char * , FILE * , char * , char * ); GLOBALS( -long numeric; + long numeric; -FILE * db; + FILE *db; ) -char * id_check_match(char * id, char * buf) +char *id_check_match(char *id, char *buf) { int i = 0; + while (id[i]) { - if (id[i] == buf[i]) { - i++; - } else { - return (char *)0L; - } + if (id[i] == buf[i]) i++; + else return 0; } - return (buf + i + 2); + return buf + i + 2; } /* @@ -54,55 +53,60 @@ char * id_check_match(char * id, char * buf) * Returns (2 - number of IDs matched): vendor must be matched for * dev to be matched */ -int find_in_db(char * vendid, char * devid, FILE * fil, - char * vname, char * devname) +int find_in_db(char *vendid, char *devid, FILE *fil, char *vname, char *devname) { - fseek(fil, 0, SEEK_SET); char buf[256], *vtext = 0L, *dtext = 0L; - while (!(vname[0])) { + + fseek(fil, 0, SEEK_SET); + while (!*vname) { //loop through - if (fgets(buf, 255, fil)==NULL) return 2; + if (!fgets(buf, 255, fil)) return 2; if ((vtext = id_check_match(vendid, buf))) strncpy(vname, vtext, strlen(vtext) - 1); } - while (!(devname[0])) { - if ((fgets(buf, 255, fil)==NULL) || (buf[0] != '\t' )) - return 1; + while (!*devname) { + if (!fgets(buf, 255, fil) || *buf != '\t') return 1; if ((dtext = id_check_match(devid, buf + 1))) strncpy(devname, dtext, strlen(dtext) - 1); } - return 0; /* Succeeded in matching both */ + + // matched both + return 0; } int do_lspci(struct dirtree *new) { int alen = 8, dirfd, res = 2; //no textual descriptions read char *dname = dirtree_path(new, &alen); + memset(toybuf, 0, 4096); struct { char class[16], vendor[16], device[16], module[256], vname[256], devname[256]; } *bufs = (void*)(toybuf + 2); - if (!strcmp("/sys/bus/pci/devices", dname)) return DIRTREE_RECURSE; + if (!new->parent) return DIRTREE_RECURSE; errno = 0; dirfd = open(dname, O_RDONLY); if (dirfd > 0) { char *p, **fields = (char*[]){"class", "vendor", "device", ""}; for (p = toybuf; **fields; p+=16, fields++) { - int fd, size; + int fd, size = ((toys.optflags & FLAG_e) && p == toybuf) ? 8 : 6; if ((fd = openat(dirfd, *fields, O_RDONLY)) < 0) continue; - size = ((toys.optflags & FLAG_e) && (p == toybuf)) ? 8 : 6; - p[read(fd, p, size)] = '\0'; + xread(fd, p, size); close(fd); + + p[size] = 0; } close(dirfd); - if (!errno) { + if (errno) return 0; + + { char *driver = ""; - char *fmt = toys.optflags & FLAG_m ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n" + char *fmt = (toys.optflags & FLAG_m) ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n" : "%s Class %s: %s:%s %s\n"; if (toys.optflags & FLAG_k) { @@ -110,12 +114,12 @@ int do_lspci(struct dirtree *new) if (readlink(dname, bufs->module, sizeof(bufs->module)) != -1) driver = basename(bufs->module); } - if (CFG_LSPCI_TEXT && (TT.numeric != 1)) { + if (CFG_LSPCI_TEXT && TT.numeric != 1) { res = find_in_db(bufs->vendor, bufs->device, TT.db, bufs->vname, bufs->devname); } - if (CFG_LSPCI_TEXT && (TT.numeric == 2)) { - fmt = toys.optflags & FLAG_m + if (CFG_LSPCI_TEXT && TT.numeric > 1) { + fmt = (toys.optflags & FLAG_m) ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\" \"%s\"\n" : "%s Class %s: %s [%s] %s [%s] %s\n"; printf(fmt, new->name + 5, bufs->class, bufs->vname, bufs->vendor, @@ -125,9 +129,9 @@ int do_lspci(struct dirtree *new) (res < 2) ? bufs->vname : bufs->vendor, !(res) ? bufs->devname : bufs->device, driver); } - } } + return 0; } |