lsusb.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //config:config LSUSB
  10. //config: bool "lsusb (4.2 kb)"
  11. //config: default y
  12. //config: help
  13. //config: lsusb is a utility for displaying information about USB buses in the
  14. //config: system and devices connected to them.
  15. //config:
  16. //config: This version uses sysfs (/sys/bus/usb/devices) only.
  17. //applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb))
  18. //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
  19. //usage:#define lsusb_trivial_usage NOUSAGE_STR
  20. //usage:#define lsusb_full_usage ""
  21. #include "libbb.h"
  22. static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
  23. const char *fileName,
  24. struct stat *statbuf UNUSED_PARAM)
  25. {
  26. parser_t *parser;
  27. char *tokens[4];
  28. char *busnum = NULL, *devnum = NULL;
  29. int product_vid = 0, product_did = 0;
  30. char *uevent_filename = concat_path_file(fileName, "/uevent");
  31. parser = config_open2(uevent_filename, fopen_for_read);
  32. free(uevent_filename);
  33. while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
  34. if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
  35. break;
  36. }
  37. if (strcmp(tokens[0], "PRODUCT") == 0) {
  38. product_vid = xstrtou(tokens[1], 16);
  39. product_did = xstrtou(tokens[2], 16);
  40. continue;
  41. }
  42. if (strcmp(tokens[0], "BUSNUM") == 0) {
  43. busnum = xstrdup(tokens[1]);
  44. continue;
  45. }
  46. if (strcmp(tokens[0], "DEVNUM") == 0) {
  47. devnum = xstrdup(tokens[1]);
  48. continue;
  49. }
  50. }
  51. config_close(parser);
  52. if (busnum) {
  53. printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
  54. free(busnum);
  55. free(devnum);
  56. }
  57. return TRUE;
  58. }
  59. int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  61. {
  62. /* no options, no getopt */
  63. recursive_action("/sys/bus/usb/devices",
  64. ACTION_RECURSE,
  65. fileAction,
  66. NULL, /* dirAction */
  67. NULL /* userData */
  68. );
  69. return EXIT_SUCCESS;
  70. }