lsusb.c (1325B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <limits.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 #include "paths.h" 8 #include "text.h" 9 #include "util.h" 10 11 static void 12 lsusb(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) || sscanf(buf, "DEVNUM=%u\n", &devnum) 29 || sscanf(buf, "PRODUCT=%x/%x/", &pid, &vid)) 30 i++; 31 if (i == 3) { 32 printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, 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 // ?man lsusb: list usb devices 49 // ?man display information about usb buses and connected devices 50 int 51 main(int argc, char *argv[]) 52 { 53 ARGBEGIN 54 { 55 default: 56 usage(); 57 } 58 ARGEND; 59 60 recurse_dir(ARUU_LINUX_PATH_SYS_BUS_USB, lsusb); 61 return 0; 62 }