1
0
mirror of git://git.suckless.org/ubase synced 2024-12-23 23:22:47 +00:00
ubase/lsusb.c

57 lines
1.0 KiB
C
Raw Normal View History

2013-09-03 12:52:47 +00:00
/* 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);
2013-10-07 18:11:40 +00:00
return EXIT_SUCCESS;
2013-09-03 12:52:47 +00:00
}
static void
lsusb(const char *file)
{
FILE *fp;
char *cwd;
char path[PATH_MAX];
char buf[BUFSIZ];
unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0;
2013-09-03 12:52:47 +00:00
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;
}
}
2013-09-05 08:53:39 +00:00
if (ferror(fp))
eprintf("%s: read error:", path);
2013-09-03 12:52:47 +00:00
fclose(fp);
}