ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit ac2c8e14959694bfbb91db1772362a07006e61af
parent fc7c1c0a881a08272afd71204ad2de5c6239e2e7
Author: sin <sin@2f30.org>
Date:   Tue,  3 Sep 2013 13:52:47 +0100

Add barebones lsusb(8)

Diffstat:
MMakefile | 1+
Alsusb.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,6 +25,7 @@ SRC = \ halt.c \ insmod.c \ lsmod.c \ + lsusb.c \ mkswap.c \ mount.c \ mountpoint.c \ diff --git a/lsusb.c b/lsusb.c @@ -0,0 +1,55 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <limits.h> +#include "util.h" + +static void lsusb(const char *file); + +static void +usage(void) +{ + eprintf("usage: %s\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + ARGBEGIN { + default: + usage(); + } ARGEND; + + recurse("/sys/bus/usb/devices", lsusb); + return 0; +} + +static void +lsusb(const char *file) +{ + FILE *fp; + char *cwd; + char path[PATH_MAX]; + char buf[BUFSIZ]; + int busnum, devnum, pid, vid; + int i = 0; + + cwd = agetcwd(); + snprintf(path, sizeof(path), "%s/%s/uevent", cwd, file); + free(cwd); + if (!(fp = fopen(path, "r"))) + return; + while (fgets(buf, sizeof(buf), fp)) { + if (sscanf(buf, "BUSNUM=%u\n", &busnum) || + sscanf(buf, "DEVNUM=%u\n", &devnum) || + sscanf(buf, "PRODUCT=%x/%x/", &pid, &vid)) + i++; + if (i == 3) { + printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, + pid, vid); + break; + } + } + fclose(fp); +} +