blob: 23b03495f6b475649f0ba60b4413b45d3360023e [file] [log] [blame]
Souf Oued11a802a2009-12-07 01:37:34 +01001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +02003 * 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 Oued11a802a2009-12-07 01:37:34 +010010
11static 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 Oued1c3f1172010-03-23 05:06:37 +010018 char *tokens[4];
19 char *busnum = NULL, *devnum = NULL;
Souf Oued11a802a2009-12-07 01:37:34 +010020 int product_vid = 0, product_did = 0;
Souf Oued11a802a2009-12-07 01:37:34 +010021 char *uevent_filename = concat_path_file(fileName, "/uevent");
Souf Oued1c3f1172010-03-23 05:06:37 +010022
Souf Oued11a802a2009-12-07 01:37:34 +010023 parser = config_open2(uevent_filename, fopen_for_read);
24 free(uevent_filename);
25
Souf Oued1c3f1172010-03-23 05:06:37 +010026 while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
Souf Oued11a802a2009-12-07 01:37:34 +010027 if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
28 break;
29 }
30
Souf Oued11a802a2009-12-07 01:37:34 +010031 if (strcmp(tokens[0], "PRODUCT") == 0) {
32 product_vid = xstrtou(tokens[1], 16);
33 product_did = xstrtou(tokens[2], 16);
34 continue;
35 }
Souf Oued1c3f1172010-03-23 05:06:37 +010036
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 Oued11a802a2009-12-07 01:37:34 +010046 }
47 config_close(parser);
48
Souf Oued1c3f1172010-03-23 05:06:37 +010049 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 Oued11a802a2009-12-07 01:37:34 +010053 }
54
55 return TRUE;
56}
57
58int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59int 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}