summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2011-02-17 12:49:36 +0100
committerPeter Stuge <peter@stuge.se>2011-07-24 23:34:58 +0200
commit511ec4301deb516691e39e3df5b90fe28cb2bb0f (patch)
treeb0dc98a29c8440d0754799559fe67f7598317a44
parente05bbc59ca7b896e2b825bde59bf883ac8c09f5c (diff)
downloadlibusb-511ec4301deb516691e39e3df5b90fe28cb2bb0f.tar.gz
Linux: Use __read_sysfs_attr() to get busnum and devaddr
Remove code duplication. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
-rw-r--r--libusb/os/linux_usbfs.c45
1 files changed, 6 insertions, 39 deletions
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c
index 62d2d95..a1825ad 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)