Mercurial > hg > toybox
changeset 698:078138791b5c
Add cut from Jason Kyungwan Han.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 16 Nov 2012 12:26:48 -0600 |
parents | ca9a1d8e2531 |
children | 26ea643e1de1 |
files | lib/lib.c lib/lib.h |
diffstat | 2 files changed, 22 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/lib.c Fri Nov 16 00:46:39 2012 -0600 +++ b/lib/lib.c Fri Nov 16 12:26:48 2012 -0600 @@ -1099,3 +1099,22 @@ } return NULL; //not reached } + +/* + * used to get the interger value. + */ +unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange) +{ + unsigned long rvalue = 0; + char *ptr; + if(*numstr == '-' || *numstr == '+' || isspace(*numstr)) perror_exit("invalid number '%s'", numstr); + errno = 0; + rvalue = strtoul(numstr, &ptr, 10); + if(errno || numstr == ptr) perror_exit("invalid number '%s'", numstr); + if(*ptr) perror_exit("invalid number '%s'", numstr); + if(rvalue >= lowrange && rvalue <= highrange) return rvalue; + else { + perror_exit("invalid number '%s'", numstr); + return rvalue; //Not reachable; to avoid waring message. + } +}
--- a/lib/lib.h Fri Nov 16 00:46:39 2012 -0600 +++ b/lib/lib.h Fri Nov 16 12:26:48 2012 -0600 @@ -175,3 +175,6 @@ // du helper functions char* make_human_readable(unsigned long long size, unsigned long unit); + +// cut helper functions +unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange);