diff options
| author | Burt P <pburt0@gmail.com> | 2017-09-10 12:10:19 -0500 | 
|---|---|---|
| committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2018-03-12 09:20:16 -0700 | 
| commit | 6f0a3e0f85fd439fabfc1b8f64b01d93151cff3c (patch) | |
| tree | e2156c1d91d77edd37a721545e910b91c6a8d4cf | |
| parent | fdb1a05b6b5c84b67e3df107288f5046b763647e (diff) | |
usb: requested changes
Signed-off-by: Burt P <pburt0@gmail.com>
| -rw-r--r-- | hardinfo/usb_util.c | 65 | ||||
| -rw-r--r-- | includes/usb_util.h | 20 | ||||
| -rw-r--r-- | modules/devices/usb.c | 16 | 
3 files changed, 57 insertions, 44 deletions
diff --git a/hardinfo/usb_util.c b/hardinfo/usb_util.c index e1db2777..62773e30 100644 --- a/hardinfo/usb_util.c +++ b/hardinfo/usb_util.c @@ -22,22 +22,18 @@  #include "usb_util.h"  usbd *usbd_new() { -    usbd *s = malloc(sizeof(usbd)); -    if (s) { -        memset(s, 0, sizeof(usbd)); -    } -    return s; +    return g_new0(usbd, 1);  }  void usbd_free(usbd *s) {      if (s) { -        free(s->vendor); -        free(s->product); -        free(s->usb_version); -        free(s->device_version); -        free(s->dev_class_str); -        free(s->dev_subclass_str); -        free(s); +        g_free(s->vendor); +        g_free(s->product); +        g_free(s->usb_version); +        g_free(s->device_version); +        g_free(s->dev_class_str); +        g_free(s->dev_subclass_str); +        g_free(s);      }  } @@ -51,7 +47,7 @@ void usbd_list_free(usbd *s) {  }  /* returns number of items after append */ -int usbd_list_append(usbd *l, usbd *n) { +static int usbd_list_append(usbd *l, usbd *n) {      int c = 0;      while(l != NULL) {          c++; @@ -71,15 +67,15 @@ int usbd_list_count(usbd *s) {      return usbd_list_append(s, NULL);  } -char *_lsusb_lv(char *line, const char *prefix) { -    if ( g_str_has_prefix(line, prefix) ) { +static char *lsusb_line_value(char *line, const char *prefix) { +    if (g_str_has_prefix(line, prefix)) {          line += strlen(prefix) + 1;          return g_strstrip(line);      } else          return NULL;  } -int usb_get_device_lsusb(int bus, int dev, usbd *s) { +static gboolean usb_get_device_lsusb(int bus, int dev, usbd *s) {      gboolean spawned;      gchar *out, *err, *p, *l, *t, *next_nl;      gchar *lsusb_cmd = g_strdup_printf("lsusb -s %d:%d -v", bus, dev); @@ -92,34 +88,34 @@ int usb_get_device_lsusb(int bus, int dev, usbd *s) {      g_free(lsusb_cmd);      if (spawned) {          if (strstr(err, "information will be missing")) { -            s->user_scan = 1; +            s->user_scan = TRUE;          }          p = out;          while(next_nl = strchr(p, '\n')) {              strend(p, '\n');              g_strstrip(p); -            if (l = _lsusb_lv(p, "idVendor")) { +            if (l = lsusb_line_value(p, "idVendor")) {                  s->vendor_id = strtol(l, NULL, 0);                  if (t = strchr(l, ' ')) -                    s->vendor = strdup(t + 1); -            } else if (l = _lsusb_lv(p, "idProduct")) { +                    s->vendor = g_strdup(t + 1); +            } else if (l = lsusb_line_value(p, "idProduct")) {                  s->product_id = strtol(l, NULL, 0);                  if (t = strchr(l, ' ')) -                    s->product = strdup(t + 1); -            } else if (l = _lsusb_lv(p, "MaxPower")) { +                    s->product = g_strdup(t + 1); +            } else if (l = lsusb_line_value(p, "MaxPower")) {                  s->max_curr_ma = atoi(l); -            } else if (l = _lsusb_lv(p, "bcdUSB")) { -                s->usb_version = strdup(l); -            } else if (l = _lsusb_lv(p, "bcdDevice")) { -                s->device_version = strdup(l); -            } else if (l = _lsusb_lv(p, "bDeviceClass")) { +            } else if (l = lsusb_line_value(p, "bcdUSB")) { +                s->usb_version = g_strdup(l); +            } else if (l = lsusb_line_value(p, "bcdDevice")) { +                s->device_version = g_strdup(l); +            } else if (l = lsusb_line_value(p, "bDeviceClass")) {                  s->dev_class = atoi(l);                  if (t = strchr(l, ' ')) -                    s->dev_class_str = strdup(t + 1); -            } else if (l = _lsusb_lv(p, "bDeviceSubClass")) { +                    s->dev_class_str = g_strdup(t + 1); +            } else if (l = lsusb_line_value(p, "bDeviceSubClass")) {                  s->dev_subclass = atoi(l);                  if (t = strchr(l, ' ')) -                    s->dev_subclass_str = strdup(t + 1); +                    s->dev_subclass_str = g_strdup(t + 1);              }              /* TODO: speed_mbs               * WISHLIST: interfaces, drivers */ @@ -127,9 +123,9 @@ int usb_get_device_lsusb(int bus, int dev, usbd *s) {          }          g_free(out);          g_free(err); -        return 1; +        return TRUE;      } -    return 0; +    return FALSE;  }  usbd *usb_get_device(int bus, int dev) { @@ -149,7 +145,7 @@ usbd *usb_get_device(int bus, int dev) {      return s;  } -usbd *usb_get_device_list_lsusb() { +static usbd *usb_get_device_list_lsusb() {      gboolean spawned;      gchar *out, *err, *p, *next_nl;      usbd *head = NULL, *nd; @@ -161,7 +157,8 @@ usbd *usb_get_device_list_lsusb() {          p = out;          while(next_nl = strchr(p, '\n')) {              strend(p, '\n'); -            if (ec = sscanf(p, "Bus %d Device %d: ID %x:%x", &bus, &dev, &vend, &prod) ) { +            ec = sscanf(p, "Bus %d Device %d: ID %x:%x", &bus, &dev, &vend, &prod); +            if (ec == 4) {                  nd = usb_get_device(bus, dev);                  if (head == NULL) {                      head = nd; diff --git a/includes/usb_util.h b/includes/usb_util.h index 5d7f55bf..e9130b99 100644 --- a/includes/usb_util.h +++ b/includes/usb_util.h @@ -1,3 +1,21 @@ +/* + *    HardInfo - Displays System Information + *    Copyright (C) 2003-2017 Leandro A. F. Pereira <leandro@hardinfo.org> + * + *    This program is free software; you can redistribute it and/or modify + *    it under the terms of the GNU General Public License as published by + *    the Free Software Foundation, version 2. + * + *    This program is distributed in the hope that it will be useful, + *    but WITHOUT ANY WARRANTY; without even the implied warranty of + *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *    GNU General Public License for more details. + * + *    You should have received a copy of the GNU General Public License + *    along with this program; if not, write to the Free Software + *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA + */ +  #ifndef __USB_UTIL_H__  #define __USB_UTIL_H__ @@ -20,7 +38,7 @@ typedef struct usbd {      int speed_mbs; /* TODO: */ -    int user_scan; /* not scanned as root */ +    gboolean user_scan; /* not scanned as root */      struct usbd *next;  } usbd; diff --git a/modules/devices/usb.c b/modules/devices/usb.c index 27b3c13e..cad52fd4 100644 --- a/modules/devices/usb.c +++ b/modules/devices/usb.c @@ -28,7 +28,7 @@  gchar *usb_list = NULL; -void __scan_usb_sysfs_add_device(gchar * endpoint, int n) +static void __scan_usb_sysfs_add_device(gchar * endpoint, int n)  {      gchar *manufacturer, *product, *mxpwr, *tmp, *strhash;      gint bus, classid, vendor, prodid; @@ -100,7 +100,7 @@ void __scan_usb_sysfs_add_device(gchar * endpoint, int n)      g_free(mxpwr);  } -gboolean __scan_usb_sysfs(void) +static gboolean __scan_usb_sysfs(void)  {      GDir *sysfs;      gchar *filename; @@ -136,7 +136,7 @@ gboolean __scan_usb_sysfs(void)      return usb_device_number > 0;  } -gboolean __scan_usb_procfs(void) +static gboolean __scan_usb_procfs(void)  {      FILE *dev;      gchar buffer[128]; @@ -260,7 +260,7 @@ gboolean __scan_usb_procfs(void)  } -void __scan_usb_lsusb_add_device(char *buffer, int bufsize, FILE * lsusb, int usb_device_number) +static void __scan_usb_lsusb_add_device(char *buffer, int bufsize, FILE * lsusb, int usb_device_number)  {      gint bus, device, vendor_id, product_id;      gchar *version = NULL, *product = NULL, *vendor = NULL, *dev_class = NULL, *int_class = NULL; @@ -367,7 +367,7 @@ void __scan_usb_lsusb_add_device(char *buffer, int bufsize, FILE * lsusb, int us      g_free(name);  } -gboolean __scan_usb_lsusb(void) +static gboolean __scan_usb_lsusb(void)  {      static gchar *lsusb_path = NULL;      int usb_device_number = 0; @@ -429,7 +429,7 @@ gboolean __scan_usb_lsusb(void)  #define UNKIFNULL_AC(f) (f != NULL) ? f : _("(Unknown)"); -void _usb_dev(const usbd *u) { +static void _usb_dev(const usbd *u) {      gchar *name, *key, *v_str, *str;      gchar *product, *vendor, *dev_class_str, *dev_subclass_str; /* don't free */ @@ -482,7 +482,7 @@ void _usb_dev(const usbd *u) {      g_free(key);  } -gboolean __scan_usb_util(void) { +static gboolean __scan_usb_util(void) {      usbd *list = usb_get_device_list();      usbd *curr = list; @@ -496,8 +496,6 @@ gboolean __scan_usb_util(void) {          usb_list = g_strdup_printf("[%s]\n", _("USB Devices"));          while(curr) { -            //printf("USB: Bus %03d Dev %03d Ven %04x Prod %04x %s %s\n", -            //    curr->bus, curr->dev, curr->vendor_id, curr->product_id, curr->vendor, curr->product);              _usb_dev(curr);              c++;              curr=curr->next;  | 
