[PATCH] fix for lsusb: unable to initialize libusb: -99

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Aruna Balakrishnaiah <aruna@xxxxxxxxxxxxxxxxxx>

   lsusb throws unusual error message "unable to initialize libusb: -99"                              
with exit status of 1 when usb devices are not listed. Patch handles it  
without printing anything and returns exit status of 0.

---
 lsusb.c |   27 ++++++++++++++++++++++++++-
 1 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/lsusb.c b/lsusb.c
index 37c43f7..081d42e 100644
--- a/lsusb.c
+++ b/lsusb.c
@@ -44,6 +44,7 @@
 #include "usbmisc.h"
 
 #include <getopt.h>
+#include <dirent.h>
 
 #define le16_to_cpu(x) libusb_cpu_to_le16(libusb_cpu_to_le16(x))
 
@@ -108,6 +109,7 @@
 #define	HUB_STATUS_BYTELEN	3	/* max 3 bytes status = hub + 23 ports */
 
 static const char procbususb[] = "/proc/bus/usb";
+static const char sysbususb[] = "/sys/bus/usb/devices";
 static unsigned int verblevel = VERBLEVEL_DEFAULT;
 static int do_report_desc = 1;
 static const char * const encryption_type[] = {
@@ -3920,6 +3922,25 @@ static int treedump(void)
 	return 0;
 }
 
+static int check_for_usbdevice(void)
+{
+	struct dirent *d;
+	int n = 0;
+	DIR *dir = opendir(sysbususb);
+
+	while ((d = readdir(dir)) != NULL) {
+		if (++n > 2)
+			break;
+	}
+
+	closedir(dir);
+
+	if (n <= 2)
+		return 1;
+	else
+		return 0;
+}
+
 /* ---------------------------------------------------------------------- */
 
 int main(int argc, char *argv[])
@@ -4027,7 +4048,11 @@ int main(int argc, char *argv[])
 				strerror(err));
 	status = 0;
 
-	err = libusb_init(&ctx);
+	if (check_for_usbdevice())
+		return 0;
+	else
+		err = libusb_init(&ctx);
+
 	if (err) {
 		fprintf(stderr, "unable to initialize libusb: %i\n", err);
 		return EXIT_FAILURE;

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux