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 | * |
| 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 8 | */ |
| 9 | #include "libbb.h" |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 10 | |
| 11 | static int FAST_FUNC fileAction( |
| 12 | const char *fileName, |
| 13 | struct stat *statbuf UNUSED_PARAM, |
| 14 | void *userData UNUSED_PARAM, |
| 15 | int depth UNUSED_PARAM) |
| 16 | { |
| 17 | parser_t *parser; |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 18 | char *tokens[4]; |
| 19 | char *busnum = NULL, *devnum = NULL; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 20 | int product_vid = 0, product_did = 0; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 21 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 22 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 23 | parser = config_open2(uevent_filename, fopen_for_read); |
| 24 | free(uevent_filename); |
| 25 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 26 | while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) { |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 27 | if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) { |
| 28 | break; |
| 29 | } |
| 30 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 31 | if (strcmp(tokens[0], "PRODUCT") == 0) { |
| 32 | product_vid = xstrtou(tokens[1], 16); |
| 33 | product_did = xstrtou(tokens[2], 16); |
| 34 | continue; |
| 35 | } |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 36 | |
| 37 | if (strcmp(tokens[0], "BUSNUM") == 0) { |
| 38 | busnum = xstrdup(tokens[1]); |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | if (strcmp(tokens[0], "DEVNUM") == 0) { |
| 43 | devnum = xstrdup(tokens[1]); |
| 44 | continue; |
| 45 | } |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 46 | } |
| 47 | config_close(parser); |
| 48 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 49 | if (busnum) { |
| 50 | printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did); |
| 51 | free(busnum); |
| 52 | free(devnum); |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return TRUE; |
| 56 | } |
| 57 | |
| 58 | int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 59 | int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 60 | { |
| 61 | /* no options, no getopt */ |
| 62 | |
| 63 | recursive_action("/sys/bus/usb/devices", |
| 64 | ACTION_RECURSE, |
| 65 | fileAction, |
| 66 | NULL, /* dirAction */ |
| 67 | NULL, /* userData */ |
| 68 | 0 /* depth */); |
| 69 | |
| 70 | return EXIT_SUCCESS; |
| 71 | } |