lsusb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.4 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 char * FAST_FUNC add_sysfs_prop(const char *dir, const char *suffix,
  23. char *buf, size_t size)
  24. {
  25. char *filename;
  26. ssize_t len;
  27. filename = concat_path_file(dir, suffix);
  28. len = open_read_close(filename, buf, size - 1);
  29. free(filename);
  30. if (len < 0)
  31. len = 0;
  32. buf[len] = '\0';
  33. return trim(buf);
  34. }
  35. static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
  36. const char *fileName,
  37. struct stat *statbuf UNUSED_PARAM)
  38. {
  39. parser_t *parser;
  40. char *tokens[4];
  41. char *busnum = NULL, *devnum = NULL;
  42. int product_vid = 0, product_did = 0;
  43. char *uevent_filename = concat_path_file(fileName, "/uevent");
  44. parser = config_open2(uevent_filename, fopen_for_read);
  45. free(uevent_filename);
  46. while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
  47. if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
  48. break;
  49. }
  50. if (strcmp(tokens[0], "PRODUCT") == 0) {
  51. product_vid = xstrtou(tokens[1], 16);
  52. product_did = xstrtou(tokens[2], 16);
  53. continue;
  54. }
  55. if (strcmp(tokens[0], "BUSNUM") == 0) {
  56. busnum = xstrdup(tokens[1]);
  57. continue;
  58. }
  59. if (strcmp(tokens[0], "DEVNUM") == 0) {
  60. devnum = xstrdup(tokens[1]);
  61. continue;
  62. }
  63. }
  64. config_close(parser);
  65. if (busnum) {
  66. char name[256], *p;
  67. p = add_sysfs_prop(fileName, "/manufacturer", name, sizeof(name) - 1);
  68. if (p != name)
  69. p = stpcpy(p, " ");
  70. add_sysfs_prop(fileName, "/product", p, name + sizeof(name) - p);
  71. printf("Bus %s Device %s: ID %04x:%04x %s\n", busnum, devnum,
  72. product_vid, product_did, name);
  73. free(busnum);
  74. free(devnum);
  75. }
  76. return TRUE;
  77. }
  78. int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  79. int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  80. {
  81. /* no options, no getopt */
  82. recursive_action("/sys/bus/usb/devices",
  83. ACTION_RECURSE,
  84. fileAction,
  85. NULL, /* dirAction */
  86. NULL /* userData */
  87. );
  88. return EXIT_SUCCESS;
  89. }