Linux: Add __read_sysfs_attr() helper function
authorHans de Goede <hdegoede@redhat.com>
Thu, 17 Feb 2011 11:45:10 +0000 (12:45 +0100)
committerPeter Stuge <peter@stuge.se>
Sun, 24 Jul 2011 21:34:58 +0000 (23:34 +0200)
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>
libusb/os/linux_usbfs.c

index aca6cd0..62d2d95 100644 (file)
@@ -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)
 {