diff options
-rw-r--r-- | libusb/os/linux_usbfs.c | 45 |
1 files changed, 6 insertions, 39 deletions
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 6f65080..3f47cf2 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -1035,51 +1035,18 @@ out: static int sysfs_scan_device(struct libusb_context *ctx, struct discovered_devs **_discdevs, const char *devname) { - int r; - FILE *fd; - char filename[PATH_MAX]; int busnum; int devaddr; usbi_dbg("scan %s", devname); - snprintf(filename, PATH_MAX, "%s/%s/busnum", SYSFS_DEVICE_PATH, devname); - fd = fopen(filename, "r"); - if (!fd) { - if (errno == ENOENT) { - /* busnum doesn't exist. Assume the device has been - disconnected (unplugged). */ - return LIBUSB_ERROR_NO_DEVICE; - } - usbi_err(ctx, "open busnum failed, errno=%d", errno); - return LIBUSB_ERROR_IO; - } - - r = fscanf(fd, "%d", &busnum); - fclose(fd); - if (r != 1) { - usbi_err(ctx, "fscanf busnum returned %d, errno=%d", r, errno); - return LIBUSB_ERROR_NO_DEVICE; - } - - snprintf(filename, PATH_MAX, "%s/%s/devnum", SYSFS_DEVICE_PATH, devname); - fd = fopen(filename, "r"); - if (!fd) { - if (errno == ENOENT) { - /* devnum doesn't exist. Assume the device has been - disconnected (unplugged). */ - return LIBUSB_ERROR_NO_DEVICE; - } - usbi_err(ctx, "open devnum failed, errno=%d", errno); - return LIBUSB_ERROR_IO; - } + busnum = __read_sysfs_attr(ctx, devname, "busnum"); + if (busnum < 0) + return busnum; - r = fscanf(fd, "%d", &devaddr); - fclose(fd); - if (r != 1) { - usbi_err(ctx, "fscanf devnum returned %d, errno=%d", r, errno); - return LIBUSB_ERROR_NO_DEVICE; - } + devaddr = __read_sysfs_attr(ctx, devname, "devnum"); + if (devaddr < 0) + return devaddr; usbi_dbg("bus=%d dev=%d", busnum, devaddr); if (busnum > 255 || devaddr > 255) |