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