changeset 1289:9b3818b2a054 draft

Cleanup pass on lspci
author Rob Landley <rob@landley.net>
date Sun, 18 May 2014 13:08:36 -0500
parents ecec71144ddd
children 0d33dd5f537e
files toys/pending/lspci.c
diffstat 1 files changed, 41 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/lspci.c	Sun May 18 11:47:08 2014 -0500
+++ b/toys/pending/lspci.c	Sun May 18 13:08:36 2014 -0500
@@ -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 @@
  * 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)
 {
+  char buf[256], *vtext = 0L, *dtext = 0L;
+
   fseek(fil, 0, SEEK_SET);
-  char buf[256], *vtext = 0L, *dtext = 0L;
-  while (!(vname[0])) {
+  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 @@
         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 @@
                (res < 2) ? bufs->vname : bufs->vendor, 
                !(res) ? bufs->devname : bufs->device, driver);
       }
-      
     }
   }
+
   return 0;
 }