diff options
author | Hans de Goede <hdegoede@redhat.com> | 2011-02-17 12:45:10 +0100 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2011-06-17 10:59:26 +0200 |
commit | fb13c18b75d953cd223b278f4036ec1aa4e19db9 (patch) | |
tree | 633a567bb0cac3f6fffcb7081b642510fc19dfeb | |
parent | 6f56d7b8c4983397e1512083b04bd23641fa5091 (diff) | |
download | libusb-fb13c18b75d953cd223b278f4036ec1aa4e19db9.tar.gz |
linux: Add __read_sysfs_attr helper function
Under 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 267aa7c..f0ae891 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) { |