From: Aurelien Jacobs Date: Mon, 6 Oct 2014 22:02:10 +0000 (+0200) Subject: usb: Simplify usb_get_port_path() and fix writing into a const char *. X-Git-Tag: libsigrok-0.4.0~878 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=76bc5f6376104e8b65322011b67aafbd45348fe2;p=libsigrok.git usb: Simplify usb_get_port_path() and fix writing into a const char *. --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index bed08e4c..93da7f96 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -677,7 +677,7 @@ SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb); SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx, int timeout, sr_receive_data_callback cb, void *cb_data); SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx); -SR_PRIV int usb_get_port_path(libusb_device *dev, const char *path, int path_len); +SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len); #endif /*--- hardware/common/scpi.c ------------------------------------------------*/ diff --git a/src/usb.c b/src/usb.c index 1c9e4118..ec502c97 100644 --- a/src/usb.c +++ b/src/usb.c @@ -271,27 +271,18 @@ SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx return SR_OK; } -SR_PRIV int usb_get_port_path(libusb_device *dev, const char *path, int path_len) +SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len) { - gchar s[64]; - gchar tmp_s[8]; - uint8_t port_numbers[32]; - int i; - - memset(port_numbers, 0, sizeof(port_numbers)); - libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); - - g_snprintf((gchar*)&s, sizeof(s), "usb/%d-%d", - libusb_get_bus_number(dev), port_numbers[0]); - - i = 1; - while (port_numbers[i]) { - g_snprintf(tmp_s, sizeof(tmp_s), ".%d", port_numbers[i]); - g_strlcat((gchar*)&s, tmp_s, sizeof(s)); - i++; - } + uint8_t port_numbers[8]; + int i, n, len; + + n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); + + len = snprintf(path, path_len, "usb/%d-%d", + libusb_get_bus_number(dev), port_numbers[0]); - g_strlcpy((gchar*)path, s, path_len); + for (i = 1; i < n; i++) + len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]); return SR_OK; }