]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib,strutils: share parse_range()
authorDavidlohr Bueso <dave@gnu.org>
Wed, 12 Oct 2011 03:18:12 +0000 (23:18 -0400)
committerKarel Zak <kzak@redhat.com>
Wed, 12 Oct 2011 08:01:26 +0000 (10:01 +0200)
This function is currently only being used by partx(8), but it's handy and
generic enough that we can use it elsewhere as well.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
include/strutils.h
lib/strutils.c
partx/partx.c

index dbcc3d904f881e7e99a2b159fa20b9dc44c86e6f..28af8b503cd09ac49d2d39efe0290432e0781dd8 100644 (file)
@@ -45,4 +45,6 @@ extern int string_to_idarray(const char *list, int ary[], size_t arysz,
 extern int string_to_bitarray(const char *list, char *ary,
                            int (*name2bit)(const char *, size_t));
 
+extern int parse_range(const char *str, int *lower, int *upper);
+
 #endif
index aad9f77381216dfe1290faeae040fec604e525da..fb1822976de37f7cbe160ebc0169758cf2198dc9 100644 (file)
@@ -424,7 +424,7 @@ int string_to_idarray(const char *list, int ary[], size_t arysz,
  * as a possition in the 'ary' bit array. It means that the 'id' has to be in
  * range <0..N> where N < sizeof(ary) * NBBY.
  *
- * Returns: 0 on sucess, <0 on error.
+ * Returns: 0 on success, <0 on error.
  */
 int string_to_bitarray(const char *list,
                     char *ary,
@@ -461,6 +461,49 @@ int string_to_bitarray(const char *list,
        return 0;
 }
 
+/*
+ * Parse the lower and higher values in a string containing
+ * "lower:higher" or "lower-higher" format. Note that either
+ * the lower or the higher values may be missing.
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int parse_range(const char *str, int *lower, int *upper)
+{
+       char *end = NULL;
+
+       if (!str)
+               return 0;
+
+       *upper = *lower = 0;
+       errno = 0;
+
+       if (*str == ':') {                              /* <:N> */
+               str++;
+               *upper = strtol(str, &end, 10);
+               if (errno || !end || *end || end == str)
+                       return -1;
+       } else {
+               *upper = *lower = strtol(str, &end, 10);
+               if (errno || !end || end == str)
+                       return -1;
+
+               if (*end == ':' && !*(end + 1))         /* <M:> */
+                       *upper = 0;
+               else if (*end == '-' || *end == ':') {  /* <M:N> <M-N> */
+                       str = end + 1;
+                       end = NULL;
+                       errno = 0;
+                       *upper = strtol(str, &end, 10);
+
+                       if (errno || !end || *end || end == str)
+                               return -1;
+               }
+       }
+       return 0;
+}
+
+
 #ifdef TEST_PROGRAM
 
 int main(int argc, char *argv[])
index 3a9da9e903a840a21af8d5d61e5e3822099cc7d4..2631d1fdf3d5e6e298e14fe1f684a4bcbefbfbf8 100644 (file)
@@ -558,41 +558,6 @@ done:
        return rc;
 }
 
-static int parse_range(const char *str, int *lower, int *upper)
-{
-       char *end = NULL;
-
-       if (!str)
-               return 0;
-
-       *upper = *lower = 0;
-       errno = 0;
-
-       if (*str == ':') {                              /* <:N> */
-               str++;
-               *upper = strtol(str, &end, 10);
-               if (errno || !end || *end || end == str)
-                       return -1;
-       } else {
-               *upper = *lower = strtol(str, &end, 10);
-               if (errno || !end || end == str)
-                       return -1;
-
-               if (*end == ':' && !*(end + 1))         /* <M:> */
-                       *upper = 0;
-               else if (*end == '-' || *end == ':') {  /* <M:N> <M-N> */
-                       str = end + 1;
-                       end = NULL;
-                       errno = 0;
-                       *upper = strtol(str, &end, 10);
-
-                       if (errno || !end || *end || end == str)
-                               return -1;
-               }
-       }
-       return 0;
-}
-
 static blkid_partlist get_partlist(blkid_probe pr,
                        const char *device, char *type)
 {