* [PATCH] lib/strutils: share parse_range()
@ 2011-10-12 2:39 Davidlohr Bueso
0 siblings, 0 replies; 4+ messages in thread
From: Davidlohr Bueso @ 2011-10-12 2:39 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
From: Davidlohr Bueso <dave@gnu.org>
Date: Tue, 11 Oct 2011 22:32:35 -0400
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 | 2 ++
lib/strutils.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
partx/partx.c | 35 -----------------------------------
3 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/include/strutils.h b/include/strutils.h
index dbcc3d9..28af8b5 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -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
diff --git a/lib/strutils.c b/lib/strutils.c
index aad9f77..fc9ec68 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -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[])
diff --git a/partx/partx.c b/partx/partx.c
index 3a9da9e..2631d1f 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -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)
{
--
1.7.7
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] lib/strutils: share parse_range()
@ 2011-10-12 3:03 Davidlohr Bueso
0 siblings, 0 replies; 4+ messages in thread
From: Davidlohr Bueso @ 2011-10-12 3:03 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
From: Davidlohr Bueso <dave@gnu.org>
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 | 2 ++
lib/strutils.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
partx/partx.c | 35 -----------------------------------
3 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/include/strutils.h b/include/strutils.h
index dbcc3d9..28af8b5 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -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
diff --git a/lib/strutils.c b/lib/strutils.c
index aad9f77..fc9ec68 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -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[])
diff --git a/partx/partx.c b/partx/partx.c
index 3a9da9e..2631d1f 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -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)
{
--
1.7.7
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] lib/strutils: share parse_range()
@ 2011-10-12 3:18 Davidlohr Bueso
2011-10-12 8:26 ` Karel Zak
0 siblings, 1 reply; 4+ messages in thread
From: Davidlohr Bueso @ 2011-10-12 3:18 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux, dave
From: Davidlohr Bueso <dave@gnu.org>
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>
---
If you have received this email already, sorry for the noise.
include/strutils.h | 2 ++
lib/strutils.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
partx/partx.c | 35 -----------------------------------
3 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/include/strutils.h b/include/strutils.h
index dbcc3d9..28af8b5 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -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
diff --git a/lib/strutils.c b/lib/strutils.c
index aad9f77..fc9ec68 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -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[])
diff --git a/partx/partx.c b/partx/partx.c
index 3a9da9e..2631d1f 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -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)
{
--
1.7.7
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] lib/strutils: share parse_range()
2011-10-12 3:18 Davidlohr Bueso
@ 2011-10-12 8:26 ` Karel Zak
0 siblings, 0 replies; 4+ messages in thread
From: Karel Zak @ 2011-10-12 8:26 UTC (permalink / raw)
To: Davidlohr Bueso; +Cc: util-linux, dave
On Tue, Oct 11, 2011 at 11:18:12PM -0400, Davidlohr Bueso wrote:
> include/strutils.h | 2 ++
> lib/strutils.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
> partx/partx.c | 35 -----------------------------------
> 3 files changed, 46 insertions(+), 36 deletions(-)
Applied, thanks.
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-10-12 8:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-12 3:03 [PATCH] lib/strutils: share parse_range() Davidlohr Bueso
-- strict thread matches above, loose matches on Subject: below --
2011-10-12 3:18 Davidlohr Bueso
2011-10-12 8:26 ` Karel Zak
2011-10-12 2:39 Davidlohr Bueso
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).