master xplshn/aruu / cmd / linux / lsusb.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <limits.h>
 5#include <stdio.h>
 6#include <stdlib.h>
 7
 8#include "text.h"
 9#include "util.h"
10
11static void
12lsusb(const char *file)
13{
14	FILE *fp;
15	char path[PATH_MAX];
16	char *buf = NULL;
17	size_t size = 0;
18	unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0;
19
20	if (strlcpy(path, file, sizeof(path)) >= sizeof(path))
21		eprintf("path too long\n");
22	if (strlcat(path, "/uevent", sizeof(path)) >= sizeof(path))
23		eprintf("path too long\n");
24
25	if (!(fp = fopen(path, "r")))
26		return;
27	while (agetline(&buf, &size, fp) != -1) {
28		if (sscanf(buf, "BUSNUM=%u\n", &busnum) ||
29		    sscanf(buf, "DEVNUM=%u\n", &devnum) ||
30		    sscanf(buf, "PRODUCT=%x/%x/", &pid, &vid))
31			i++;
32		if (i == 3) {
33			printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum,
34			       pid, vid);
35			break;
36		}
37	}
38	if (ferror(fp))
39		eprintf("%s: read error:", path);
40	free(buf);
41	fclose(fp);
42}
43
44static void
45usage(void)
46{
47	eprintf("usage: %s\n", argv0);
48}
49
50// ?man lsusb: list usb devices
51// ?man display information about usb buses and connected devices
52int
53main(int argc, char *argv[])
54{
55	ARGBEGIN {
56	default:
57		usage();
58	} ARGEND;
59
60	recurse_dir("/sys/bus/usb/devices", lsusb);
61	return 0;
62}