ubase/lsusb.c

61 lines
1.1 KiB
C
Raw Normal View History

2013-09-03 08:52:47 -04:00
/* See LICENSE file for copyright and license details. */
2014-06-30 10:39:42 -04:00
#include <limits.h>
2013-09-03 08:52:47 -04:00
#include <stdio.h>
#include <stdlib.h>
2014-06-30 14:03:41 -04:00
2014-06-14 08:13:10 -04:00
#include "text.h"
2013-09-03 08:52:47 -04:00
#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);
2014-10-02 18:45:25 -04:00
return 0;
2013-09-03 08:52:47 -04:00
}
static void
lsusb(const char *file)
{
FILE *fp;
char path[PATH_MAX];
2014-06-14 08:13:10 -04:00
char *buf = NULL;
size_t size = 0;
unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0;
2013-09-03 08:52:47 -04:00
2014-06-30 10:39:42 -04:00
if (strlcpy(path, file, sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
if (strlcat(path, "/uevent", sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
2013-09-03 08:52:47 -04:00
if (!(fp = fopen(path, "r")))
return;
2014-06-14 08:13:10 -04:00
while (agetline(&buf, &size, fp) != -1) {
2013-09-03 08:52:47 -04:00
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 04:53:39 -04:00
if (ferror(fp))
eprintf("%s: read error:", path);
2014-06-14 08:13:10 -04:00
free(buf);
2013-09-03 08:52:47 -04:00
fclose(fp);
}