Linux: Handle single- and two-digit kernel versions like 3.0 and 4
authorHans de Goede <hdegoede@redhat.com>
Fri, 17 Jun 2011 08:19:00 +0000 (10:19 +0200)
committerPeter Stuge <peter@stuge.se>
Thu, 15 Sep 2011 13:16:20 +0000 (15:16 +0200)
The code has been tested with various version strings.

[stuge: Remove bogus string length check and optimize for newer kernels]

libusb/os/linux_usbfs.c

index ca75779..69a2773 100644 (file)
@@ -220,25 +220,33 @@ static clockid_t find_monotonic_clock(void)
 static int check_flag_bulk_continuation(void)
 {
        struct utsname uts;
-       int major, minor, sublevel;
+       int atoms, major, minor, sublevel;
 
        if (uname(&uts) < 0)
                return -1;
-       if (strlen(uts.release) < 4)
+       atoms = sscanf(uts.release, "%d.%d.%d", &major, &minor, &sublevel);
+       if (atoms < 1)
+               return -1;
+
+       if (major > 2)
+               return 1;
+       if (major < 2)
                return 0;
-       if (sscanf(uts.release, "%d.%d.%d", &major, &minor, &sublevel) != 3)
+
+       if (atoms < 2)
                return 0;
-       if (major < 2)
+
+       /* major == 2 */
+       if (minor < 6)
                return 0;
-       if (major == 2) {
-               if (minor < 6)
-                       return 0;
-               if (minor == 6) {
-                       if (sublevel < 32)
-                               return 0;
-               }
-       }
-       return 1;
+       if (minor > 6) /* Does not exist, just here for correctness */
+               return 1;
+
+       /* 2.6.x */
+       if (3 == atoms && sublevel >= 32)
+               return 1;
+
+       return 0;
 }
 
 /* Return 1 if filename exists inside dirname in sysfs.