Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 3 | * lsusb implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 8 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 9 | |
| 10 | //usage:#define lsusb_trivial_usage NOUSAGE_STR |
| 11 | //usage:#define lsusb_full_usage "" |
| 12 | |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 13 | #include "libbb.h" |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 14 | |
| 15 | static int FAST_FUNC fileAction( |
| 16 | const char *fileName, |
| 17 | struct stat *statbuf UNUSED_PARAM, |
| 18 | void *userData UNUSED_PARAM, |
| 19 | int depth UNUSED_PARAM) |
| 20 | { |
| 21 | parser_t *parser; |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 22 | char *tokens[4]; |
| 23 | char *busnum = NULL, *devnum = NULL; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 24 | int product_vid = 0, product_did = 0; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 25 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 26 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 27 | parser = config_open2(uevent_filename, fopen_for_read); |
| 28 | free(uevent_filename); |
| 29 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 30 | while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) { |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 31 | if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) { |
| 32 | break; |
| 33 | } |
| 34 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 35 | if (strcmp(tokens[0], "PRODUCT") == 0) { |
| 36 | product_vid = xstrtou(tokens[1], 16); |
| 37 | product_did = xstrtou(tokens[2], 16); |
| 38 | continue; |
| 39 | } |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 40 | |
| 41 | if (strcmp(tokens[0], "BUSNUM") == 0) { |
| 42 | busnum = xstrdup(tokens[1]); |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | if (strcmp(tokens[0], "DEVNUM") == 0) { |
| 47 | devnum = xstrdup(tokens[1]); |
| 48 | continue; |
| 49 | } |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 50 | } |
| 51 | config_close(parser); |
| 52 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 53 | if (busnum) { |
| 54 | printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did); |
| 55 | free(busnum); |
| 56 | free(devnum); |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 63 | int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 64 | { |
| 65 | /* no options, no getopt */ |
| 66 | |
| 67 | recursive_action("/sys/bus/usb/devices", |
| 68 | ACTION_RECURSE, |
| 69 | fileAction, |
| 70 | NULL, /* dirAction */ |
| 71 | NULL, /* userData */ |
| 72 | 0 /* depth */); |
| 73 | |
| 74 | return EXIT_SUCCESS; |
| 75 | } |