]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib,strutils: add default value to parse_range()
authorDavidlohr Bueso <dave@gnu.org>
Fri, 14 Oct 2011 20:32:15 +0000 (16:32 -0400)
committerKarel Zak <kzak@redhat.com>
Mon, 17 Oct 2011 09:06:48 +0000 (11:06 +0200)
This function currently sets the low or high values to 0 when the string doesn't
contain a value, like '123:' or ':123'. In order to make it more flexible, we allow it
to be passed an arbitrary value.

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

index 28af8b503cd09ac49d2d39efe0290432e0781dd8..9765a77477d6fb85044ae4509d95ba5a0d14497c 100644 (file)
@@ -45,6 +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);
+extern int parse_range(const char *str, int *lower, int *upper, int def);
 
 #endif
index fb1822976de37f7cbe160ebc0169758cf2198dc9..6b2ec799e77db55c24e0c4ca05cb02e35c025d08 100644 (file)
@@ -464,18 +464,19 @@ int string_to_bitarray(const char *list,
 /*
  * 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.
+ * the lower or the higher values may be missing, and the def
+ * value will be assigned to it by default.
  *
  * Returns: 0 on success, <0 on error.
  */
-int parse_range(const char *str, int *lower, int *upper)
+int parse_range(const char *str, int *lower, int *upper, int def)
 {
        char *end = NULL;
 
        if (!str)
                return 0;
 
-       *upper = *lower = 0;
+       *upper = *lower = def;
        errno = 0;
 
        if (*str == ':') {                              /* <:N> */
index 2631d1fdf3d5e6e298e14fe1f684a4bcbefbfbf8..bf18a4ebe491c30329b038df70c4c156624d1cc4 100644 (file)
@@ -699,7 +699,7 @@ int main(int argc, char **argv)
                        what = ACT_LIST;
                        break;
                case 'n':
-                       if (parse_range(optarg, &lower, &upper))
+                       if (parse_range(optarg, &lower, &upper, 0))
                                errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
                        break;
                case 'o':