diff options
author | Hans de Goede <hdegoede@redhat.com> | 2011-09-06 10:36:00 +0000 |
---|---|---|
committer | Vitali Lovich <vlovich@aliph.com> | 2011-09-06 10:42:58 +0100 |
commit | f471c635db625536bb6d60ebc935791cfe2cd38d (patch) | |
tree | 8fa6a882fe0db82216d8a3f3d280cd21d51d5af9 | |
parent | bec707ec820ab1efc239ed2b611fe50bc47f56f8 (diff) | |
download | libusb-f471c635db625536bb6d60ebc935791cfe2cd38d.tar.gz |
Linux: Add __read_sysfs_attr() helper function
On Linux we often need to read (postive) integers from sysfs. This patch
adds a helper function for this. This is a preparation patch for adding
a libusb_get_device_speed() function to libusb.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
-rw-r--r-- | libusb/os/linux_usbfs.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 95c17b0..6f65080 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -375,6 +375,41 @@ static int __open_sysfs_attr(struct libusb_device *dev, const char *attr) return fd; } +/* Note only suitable for attributes which always read >= 0, < 0 is error */ +static int __read_sysfs_attr(struct libusb_context *ctx, + const char *devname, const char *attr) +{ + char filename[PATH_MAX]; + FILE *f; + int r, value; + + snprintf(filename, PATH_MAX, "%s/%s/%s", SYSFS_DEVICE_PATH, + devname, attr); + f = fopen(filename, "r"); + if (f == NULL) { + if (errno == ENOENT) { + /* File doesn't exist. Assume the device has been + disconnected (see trac ticket #70). */ + return LIBUSB_ERROR_NO_DEVICE; + } + usbi_err(ctx, "open %s failed errno=%d", filename, errno); + return LIBUSB_ERROR_IO; + } + + r = fscanf(f, "%d", &value); + fclose(f); + if (r != 1) { + usbi_err(ctx, "fscanf %s returned %d, errno=%d", attr, r, errno); + return LIBUSB_ERROR_NO_DEVICE; /* For unplug race (trac #70) */ + } + if (value < 0) { + usbi_err(ctx, "%s contains a negative value", filename); + return LIBUSB_ERROR_IO; + } + + return value; +} + static int sysfs_get_device_descriptor(struct libusb_device *dev, unsigned char *buffer) { |