lsusb.c 1.7 KB

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