diff options
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/lsusb.c | 47 |
1 files changed, 18 insertions, 29 deletions
diff --git a/toys/other/lsusb.c b/toys/other/lsusb.c index 98b863ed..07886e8c 100644 --- a/toys/other/lsusb.c +++ b/toys/other/lsusb.c @@ -6,9 +6,11 @@ USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN)) config LSUSB bool "lsusb" - default n + default y help usage: lsusb + + List USB hosts/devices. */ #include "toys.h" @@ -17,38 +19,26 @@ static int list_device(struct dirtree *new) { FILE *file; char *name; - int busnum = 0; - int devnum = 0; - int pid = 0; - int vid = 0; - if (!new->parent) - return DIRTREE_RECURSE; - if (new->name[0] == '.') - return 0; + int busnum = 0, devnum = 0, pid = 0, vid = 0; + + if (!new->parent) return DIRTREE_RECURSE; + if (new->name[0] == '.') return 0; name = dirtree_path(new, 0); snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent"); file = fopen(toybuf, "r"); - if (!file) - return 0; - if (!fgets(toybuf, sizeof(toybuf), file) || !strncmp(toybuf, "DEVTYPE=", 8)) { + if (file) { + int count = 0; + + while (fgets(toybuf, sizeof(toybuf), file)) + if (sscanf(toybuf, "BUSNUM=%u\n", &busnum) + || sscanf(toybuf, "DEVNUM=%u\n", &devnum) + || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++; + + if (count == 3) + printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid); fclose(file); - return 0; - } - while (fgets(toybuf, sizeof(toybuf), file)) { - if (!strncmp(toybuf, "BUSNUM=", 7)) - busnum = atoi(&toybuf[7]); - if (!strncmp(toybuf, "DEVNUM=", 7)) - devnum = atoi(&toybuf[7]); - if (!strncmp(toybuf, "PRODUCT=", 8)) { - char *pos = strchr(toybuf, '/'); - pid = xstrtoul(&toybuf[8], NULL, 16); - if (pos) - vid = xstrtoul(pos + 1, NULL, 16); - } } - fclose(file); - - printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid); + free(name); return 0; } @@ -56,5 +46,4 @@ static int list_device(struct dirtree *new) void lsusb_main(void) { dirtree_read("/sys/bus/usb/devices/", list_device); - return; } |