diff options
author | Andre Renaud <andre@bluewatersys.com> | 2013-05-14 17:42:11 -0500 |
---|---|---|
committer | Andre Renaud <andre@bluewatersys.com> | 2013-05-14 17:42:11 -0500 |
commit | bd7a7fe6cddf790c66841c1fd636f51967060433 (patch) | |
tree | b5feda8a8b738070820e60d14651b55527d60bc2 /toys/other/lsusb.c | |
parent | 38aa170897534e31afcb1e940b13d103e40cd1c4 (diff) | |
download | toybox-bd7a7fe6cddf790c66841c1fd636f51967060433.tar.gz |
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Diffstat (limited to 'toys/other/lsusb.c')
-rw-r--r-- | toys/other/lsusb.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/toys/other/lsusb.c b/toys/other/lsusb.c new file mode 100644 index 00000000..98b863ed --- /dev/null +++ b/toys/other/lsusb.c @@ -0,0 +1,60 @@ +/* lsusb.c - list available USB devices + * + * Copyright 2013 Andre Renaud <andre@bluewatersys.com> + +USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN)) + +config LSUSB + bool "lsusb" + default n + help + usage: lsusb +*/ + +#include "toys.h" + +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; + 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)) { + 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); + + return 0; +} + +void lsusb_main(void) +{ + dirtree_read("/sys/bus/usb/devices/", list_device); + return; +} |