lsusb.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * lspci 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>
  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[6];
  18. char *bus = NULL, *device = 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, 6, 1, "\\/=", PARSE_NORMAL)) {
  24. if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
  25. break;
  26. }
  27. if (strcmp(tokens[0], "DEVICE") == 0) {
  28. bus = xstrdup(tokens[4]);
  29. device = xstrdup(tokens[5]);
  30. continue;
  31. }
  32. if (strcmp(tokens[0], "PRODUCT") == 0) {
  33. product_vid = xstrtou(tokens[1], 16);
  34. product_did = xstrtou(tokens[2], 16);
  35. continue;
  36. }
  37. }
  38. config_close(parser);
  39. if (bus) {
  40. printf("Bus %s Device %s: ID %04x:%04x\n", bus, device, product_vid, product_did);
  41. free(bus);
  42. free(device);
  43. }
  44. return TRUE;
  45. }
  46. int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  48. {
  49. /* no options, no getopt */
  50. recursive_action("/sys/bus/usb/devices",
  51. ACTION_RECURSE,
  52. fileAction,
  53. NULL, /* dirAction */
  54. NULL, /* userData */
  55. 0 /* depth */);
  56. return EXIT_SUCCESS;
  57. }