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