lib,strutils: add default value to parse_range()

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>
This commit is contained in:
Davidlohr Bueso 2011-10-14 16:32:15 -04:00 committed by Karel Zak
parent cf48ee22d1
commit af7df9ee67
3 changed files with 6 additions and 5 deletions

View 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, extern int string_to_bitarray(const char *list, char *ary,
int (*name2bit)(const char *, size_t)); 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 #endif

View file

@ -464,18 +464,19 @@ int string_to_bitarray(const char *list,
/* /*
* Parse the lower and higher values in a string containing * Parse the lower and higher values in a string containing
* "lower:higher" or "lower-higher" format. Note that either * "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. * 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; char *end = NULL;
if (!str) if (!str)
return 0; return 0;
*upper = *lower = 0; *upper = *lower = def;
errno = 0; errno = 0;
if (*str == ':') { /* <:N> */ if (*str == ':') { /* <:N> */

View file

@ -699,7 +699,7 @@ int main(int argc, char **argv)
what = ACT_LIST; what = ACT_LIST;
break; break;
case 'n': 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")); errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
break; break;
case 'o': case 'o':