diff options
author | Daniel Drake <dsd@gentoo.org> | 2008-05-04 00:51:59 +0100 |
---|---|---|
committer | Daniel Drake <dsd@gentoo.org> | 2008-05-04 00:51:59 +0100 |
commit | 5878daa85e3364bb3885190425d4f9deaa2d8c36 (patch) | |
tree | 12b7ece4e65b58b7d36b6e9dc07af14ab285e61c | |
parent | a74106a9b48b88bae0e099be34a18d653a8f1238 (diff) | |
download | libusb-5878daa85e3364bb3885190425d4f9deaa2d8c36.tar.gz |
Fix libusb_get_device_list return value
It was documented to return the list length, but was returning 0.
-rw-r--r-- | examples/lsusb.c | 7 | ||||
-rw-r--r-- | libusb/core.c | 18 | ||||
-rw-r--r-- | libusb/libusb.h | 2 |
3 files changed, 17 insertions, 10 deletions
diff --git a/examples/lsusb.c b/examples/lsusb.c index 9aad2bf..193b23a 100644 --- a/examples/lsusb.c +++ b/examples/lsusb.c @@ -39,14 +39,15 @@ int main(void) { libusb_device **devs; int r; + size_t cnt; r = libusb_init(); if (r < 0) return r; - r = libusb_get_device_list(&devs); - if (r < 0) - return r; + cnt = libusb_get_device_list(&devs); + if (cnt < 0) + return (int) cnt; print_devs(devs); libusb_free_device_list(devs, 1); diff --git a/libusb/core.c b/libusb/core.c index 479f919..59df43e 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -80,8 +80,8 @@ pthread_mutex_t usbi_open_devs_lock = PTHREAD_MUTEX_INITIALIZER; // discover devices libusb_device **list; libusb_device *found = NULL; -int cnt = libusb_get_device_list(&list); -int i = 0; +size_t cnt = libusb_get_device_list(&list); +size_t i = 0; if (cnt < 0) error(); @@ -281,12 +281,16 @@ struct libusb_device *usbi_get_device_by_session_id(unsigned long session_id) * not to unreference a device you are about to open until after you have * opened it. * + * This return value of this function indicates the number of devices in + * the resultant list. The list is actually one element larger, as it is + * NULL-terminated. + * * \param list output location for a list of devices. Must be later freed with * libusb_free_device_list(). * \returns the number of devices in the outputted list, or LIBUSB_ERROR_NO_MEM * on memory allocation failure. */ -API_EXPORTED int libusb_get_device_list(libusb_device ***list) +API_EXPORTED size_t libusb_get_device_list(libusb_device ***list) { struct discovered_devs *discdevs = discovered_devs_alloc(); struct libusb_device **ret; @@ -299,14 +303,16 @@ API_EXPORTED int libusb_get_device_list(libusb_device ***list) return LIBUSB_ERROR_NO_MEM; r = usbi_backend->get_device_list(&discdevs); - if (r < 0) + if (r < 0) { + len = r; goto out; + } /* convert discovered_devs into a list */ len = discdevs->len; ret = malloc(sizeof(void *) * (len + 1)); if (!ret) { - r = LIBUSB_ERROR_NO_MEM; + len = LIBUSB_ERROR_NO_MEM; goto out; } @@ -319,7 +325,7 @@ API_EXPORTED int libusb_get_device_list(libusb_device ***list) out: discovered_devs_free(discdevs); - return r; + return len; } /** \ingroup dev diff --git a/libusb/libusb.h b/libusb/libusb.h index dc88a16..b69dd7f 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -635,7 +635,7 @@ struct libusb_transfer { int libusb_init(void); void libusb_exit(void); -int libusb_get_device_list(libusb_device ***list); +size_t libusb_get_device_list(libusb_device ***list); void libusb_free_device_list(libusb_device **list, int unref_devices); const struct libusb_device_descriptor *libusb_get_device_descriptor( libusb_device *dev); |