Skip to content

Commit

Permalink
Updates based on feedback from stefanrueger.
Browse files Browse the repository at this point in the history
Change sense of check_for_port_argument_match() to return nonzero when there is a match.

Increase size of 'bus_num' and 'dev_addr' from 4 to 21.

Various cleanup, formatting changes, and simplifications.

Match trailing end of serial numbers.
  • Loading branch information
MikeRaffa committed Dec 4, 2023
1 parent 447b176 commit fa2e69d
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/usbasp.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,25 +410,16 @@ static int check_for_port_argument_match(const char *port, char *bus, char *devi
char *colon_pointer = strchr(port, ':');
if (colon_pointer) {
// Value contains ':' character. Compare with bus/device.
if (strncmp(port, bus, colon_pointer - port)) {
return 1;
}
port = colon_pointer + 1;
if (strcmp(port, device)) {
return 1;
}
return 0;
} else {
// serial number case
if (!str_eq(serial_num, port)) {
return 1;
} else {
if (strncmp(port, bus, colon_pointer - port))
return 0;
}
port = colon_pointer + 1;
return str_eq(port, device);
}
// serial number case
return (*port && str_ends(serial_num, port));
}
// Invalid -P option.
return 1;
return 0;
}

/*
Expand Down Expand Up @@ -498,11 +489,11 @@ static int usbOpenDevice(libusb_device_handle **device, int vendor, const char *
if(!str_eq(port, "usb")) {
// -P option given
libusb_get_string_descriptor_ascii(handle, descriptor.iSerialNumber, (unsigned char*)string, sizeof(string));
char bus_num[4];
char bus_num[21];
sprintf(bus_num, "%d", libusb_get_bus_number(dev));
char dev_addr[4];
char dev_addr[21];
sprintf(dev_addr, "%d", libusb_get_device_address(dev));
if (check_for_port_argument_match(port, bus_num, dev_addr, string)) {
if (!check_for_port_argument_match(port, bus_num, dev_addr, string)) {
errorCode = USB_ERROR_NOTFOUND;
}
}
Expand Down Expand Up @@ -582,7 +573,7 @@ static int didUsbInit = 0;
// -P option given
usb_get_string_simple(handle, dev->descriptor.iSerialNumber,
string, sizeof(string));
if (check_for_port_argument_match(port, bus->dirname, dev->filename, string)) {
if (!check_for_port_argument_match(port, bus->dirname, dev->filename, string)) {
errorCode = USB_ERROR_NOTFOUND;
}
}
Expand Down

0 comments on commit fa2e69d

Please sign in to comment.