lsusb.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "libbb.h"
  10. static int FAST_FUNC fileAction(
  11. const char *fileName,
  12. struct stat *statbuf UNUSED_PARAM,
  13. void *userData UNUSED_PARAM,
  14. int depth UNUSED_PARAM)
  15. {
  16. parser_t *parser;
  17. char *tokens[4];
  18. char *busnum = NULL, *devnum = NULL;
  19. int product_vid = 0, product_did = 0;
  20. char *uevent_filename = concat_path_file(fileName, "/uevent");
  21. parser = config_open2(uevent_filename, fopen_for_read);
  22. free(uevent_filename);
  23. while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
  24. if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
  25. break;
  26. }
  27. if (strcmp(tokens[0], "PRODUCT") == 0) {
  28. product_vid = xstrtou(tokens[1], 16);
  29. product_did = xstrtou(tokens[2], 16);
  30. continue;
  31. }
  32. if (strcmp(tokens[0], "BUSNUM") == 0) {
  33. busnum = xstrdup(tokens[1]);
  34. continue;
  35. }
  36. if (strcmp(tokens[0], "DEVNUM") == 0) {
  37. devnum = xstrdup(tokens[1]);
  38. continue;
  39. }
  40. }
  41. config_close(parser);
  42. if (busnum) {
  43. printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
  44. free(busnum);
  45. free(devnum);
  46. }
  47. return TRUE;
  48. }
  49. int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  50. int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  51. {
  52. /* no options, no getopt */
  53. recursive_action("/sys/bus/usb/devices",
  54. ACTION_RECURSE,
  55. fileAction,
  56. NULL, /* dirAction */
  57. NULL, /* userData */
  58. 0 /* depth */);
  59. return EXIT_SUCCESS;
  60. }