lib/strutils: add default value to parse_range()
Subsystems (2)
PATCH 1/1 lib/strutils: add default value to parse_range()
From: Davidlohr Bueso <[email protected]>
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 <[email protected]>
--- include/strutils.h | 2 +- lib/strutils.c | 7 ++++--- partx/partx.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
include/strutils.h +1 -1
@@ -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
lib/strutils.c +4 -3
@@ -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> */
partx/partx.c +1 -1
@@ -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' :
Signature · 1 line
1.7.7
Quoted 4 lines · Davidlohr Bueso
include/strutils.h | 2 +- lib/strutils.c | 7 ++++--- partx/partx.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
Applied, thanks.
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com