util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Davidlohr Bueso <dave@gnu.org>
To: Karel Zak <kzak@redhat.com>
Cc: util-linux <util-linux@vger.kernel.org>
Subject: [PATCH] lib/strutils: add default value to parse_range()
Date: Fri, 14 Oct 2011 16:32:15 -0400	[thread overview]
Message-ID: <1318624335.1923.14.camel@offworld> (raw)

From: Davidlohr Bueso <dave@gnu.org>

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 |    2 +-
 lib/strutils.c     |    7 ++++---
 partx/partx.c      |    2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/include/strutils.h b/include/strutils.h
index 28af8b5..9765a77 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -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
diff --git a/lib/strutils.c b/lib/strutils.c
index fb18229..6b2ec79 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -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> */
diff --git a/partx/partx.c b/partx/partx.c
index 2631d1f..bf18a4e 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -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':
-- 
1.7.7

             reply	other threads:[~2011-10-14 20:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-14 20:32 Davidlohr Bueso [this message]
2011-10-17  9:16 ` [PATCH] lib/strutils: add default value to parse_range() Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1318624335.1923.14.camel@offworld \
    --to=dave@gnu.org \
    --cc=kzak@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).