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

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

 



On Wed, 23 Nov 2011 18:02:33 +0100, Aruna Balakrishnaiah <aruna@xxxxxxxxxxxxxxxxxx> wrote:
   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.

@@ -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);

You are aware dir can be NULL on error, right?  Shouldn't that be handled?

+
+	while ((d = readdir(dir)) != NULL) {
+		if (++n > 2)
+			break;
+	}
+
+	closedir(dir);
+
+	if (n <= 2)
+		return 1;
+	else
+		return 0;

So if the directory is empty (ie. has only “.” and “..”) the function returns
true value?  This feels contrary to the name of the function. Also, how about just:

	return n > 2;

(not that this has reversed logic).

+}
+
 /* ---------------------------------------------------------------------- */
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;

So if there are no USB devices you return with zero exit code and no error
message?  That's not very friendly.

+	else
+		err = libusb_init(&ctx);

else not really needed as there is return in the other branch of the if
statement.

+
 	if (err) {
 		fprintf(stderr, "unable to initialize libusb: %i\n", err);
 		return EXIT_FAILURE;

--
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +----<email/xmpp: mpn@xxxxxxxxxx>--------------ooO--(_)--Ooo--
--
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