2008-12-07 00:52:58 +00:00
|
|
|
/*
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-12-07 00:52:58 +00:00
|
|
|
*/
|
2006-11-25 14:44:13 +00:00
|
|
|
/*
|
|
|
|
You need to define the following (example):
|
|
|
|
|
|
|
|
#define type long
|
|
|
|
#define xstrtou(rest) xstrtoul##rest
|
|
|
|
#define xstrto(rest) xstrtol##rest
|
|
|
|
#define xatou(rest) xatoul##rest
|
|
|
|
#define xato(rest) xatol##rest
|
|
|
|
#define XSTR_UTYPE_MAX ULONG_MAX
|
|
|
|
#define XSTR_TYPE_MAX LONG_MAX
|
|
|
|
#define XSTR_TYPE_MIN LONG_MIN
|
|
|
|
#define XSTR_STRTOU strtoul
|
|
|
|
*/
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
|
2006-11-25 14:44:13 +00:00
|
|
|
unsigned type lower,
|
|
|
|
unsigned type upper,
|
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
unsigned type r;
|
|
|
|
int old_errno;
|
|
|
|
char *e;
|
|
|
|
|
2009-10-22 20:28:08 +00:00
|
|
|
/* Disallow '-' and any leading whitespace. */
|
|
|
|
if (*numstr == '-' || *numstr == '+' || isspace(*numstr))
|
2006-11-25 14:44:13 +00:00
|
|
|
goto inval;
|
|
|
|
|
|
|
|
/* Since this is a lib function, we're not allowed to reset errno to 0.
|
|
|
|
* Doing so could break an app that is deferring checking of errno.
|
|
|
|
* So, save the old value so that we can restore it if successful. */
|
|
|
|
old_errno = errno;
|
|
|
|
errno = 0;
|
|
|
|
r = XSTR_STRTOU(numstr, &e, base);
|
|
|
|
/* Do the initial validity check. Note: The standards do not
|
|
|
|
* guarantee that errno is set if no digits were found. So we
|
|
|
|
* must test for this explicitly. */
|
2006-12-16 23:48:13 +00:00
|
|
|
if (errno || numstr == e)
|
2006-11-25 14:44:13 +00:00
|
|
|
goto inval; /* error / no digits / illegal trailing chars */
|
|
|
|
|
2010-10-28 16:57:19 +00:00
|
|
|
errno = old_errno; /* Ok. So restore errno. */
|
2006-11-25 14:44:13 +00:00
|
|
|
|
|
|
|
/* Do optional suffix parsing. Allow 'empty' suffix tables.
|
|
|
|
* Note that we also allow nul suffixes with associated multipliers,
|
|
|
|
* to allow for scaling of the numstr by some default multiplier. */
|
|
|
|
if (suffixes) {
|
2007-07-27 15:30:39 +00:00
|
|
|
while (suffixes->mult) {
|
2006-11-25 14:44:13 +00:00
|
|
|
if (strcmp(suffixes->suffix, e) == 0) {
|
|
|
|
if (XSTR_UTYPE_MAX / suffixes->mult < r)
|
|
|
|
goto range; /* overflow! */
|
|
|
|
r *= suffixes->mult;
|
2007-06-16 00:30:52 +00:00
|
|
|
goto chk_range;
|
2006-11-25 14:44:13 +00:00
|
|
|
}
|
|
|
|
++suffixes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: trailing space is an error.
|
2013-01-15 12:58:01 +00:00
|
|
|
* It would be easy enough to allow though if desired. */
|
2006-11-25 14:44:13 +00:00
|
|
|
if (*e)
|
|
|
|
goto inval;
|
2007-06-16 00:30:52 +00:00
|
|
|
chk_range:
|
2006-11-25 14:44:13 +00:00
|
|
|
/* Finally, check for range limits. */
|
|
|
|
if (r >= lower && r <= upper)
|
|
|
|
return r;
|
|
|
|
range:
|
|
|
|
bb_error_msg_and_die("number %s is not in %llu..%llu range",
|
|
|
|
numstr, (unsigned long long)lower,
|
|
|
|
(unsigned long long)upper);
|
|
|
|
inval:
|
|
|
|
bb_error_msg_and_die("invalid number '%s'", numstr);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
|
2006-11-25 14:44:13 +00:00
|
|
|
unsigned type lower,
|
|
|
|
unsigned type upper)
|
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
|
2006-11-25 14:44:13 +00:00
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
|
2006-11-25 14:44:13 +00:00
|
|
|
unsigned type lower,
|
|
|
|
unsigned type upper,
|
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xatou(_range)(const char *numstr,
|
2006-11-25 14:44:13 +00:00
|
|
|
unsigned type lower,
|
|
|
|
unsigned type upper)
|
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
|
2006-11-25 14:44:13 +00:00
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
unsigned type FAST_FUNC xatou()(const char *numstr)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xatou(_sfx)(numstr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signed ones */
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
|
2006-11-25 14:44:13 +00:00
|
|
|
type lower,
|
|
|
|
type upper,
|
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
unsigned type u = XSTR_TYPE_MAX;
|
|
|
|
type r;
|
|
|
|
const char *p = numstr;
|
|
|
|
|
2007-12-11 13:15:11 +00:00
|
|
|
/* NB: if you'll decide to disallow '+':
|
|
|
|
* at least renice applet needs to allow it */
|
|
|
|
if (p[0] == '+' || p[0] == '-') {
|
2006-11-25 14:44:13 +00:00
|
|
|
++p;
|
2007-12-11 13:15:11 +00:00
|
|
|
if (p[0] == '-')
|
|
|
|
++u; /* = <type>_MIN (01111... + 1 == 10000...) */
|
2006-11-25 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
|
|
|
|
|
|
|
|
if (*numstr == '-') {
|
|
|
|
r = -r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < lower || r > upper) {
|
|
|
|
bb_error_msg_and_die("number %s is not in %lld..%lld range",
|
|
|
|
numstr, (long long)lower, (long long)upper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
|
|
|
|
}
|
|
|
|
|
2009-05-25 02:15:37 +00:00
|
|
|
type FAST_FUNC xstrto()(const char *numstr, int base)
|
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, base, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xato(_range_sfx)(const char *numstr,
|
2006-11-25 14:44:13 +00:00
|
|
|
type lower,
|
|
|
|
type upper,
|
|
|
|
const struct suffix_mult *suffixes)
|
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
type FAST_FUNC xato()(const char *numstr)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
|
|
|
return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
|
|
|
|
}
|
2006-11-27 14:43:21 +00:00
|
|
|
|
|
|
|
#undef type
|
|
|
|
#undef xstrtou
|
|
|
|
#undef xstrto
|
|
|
|
#undef xatou
|
|
|
|
#undef xato
|
|
|
|
#undef XSTR_UTYPE_MAX
|
|
|
|
#undef XSTR_TYPE_MAX
|
|
|
|
#undef XSTR_TYPE_MIN
|
|
|
|
#undef XSTR_STRTOU
|