Bump to version 1.22.1
[platform/upstream/busybox.git] / libbb / xatonum_template.c
index ce01995..e047198 100644 (file)
@@ -1,4 +1,8 @@
 /*
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+/*
 You need to define the following (example):
 
 #define type long
@@ -12,7 +16,7 @@ You need to define the following (example):
 #define XSTR_STRTOU strtoul
 */
 
-unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
+unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
                unsigned type lower,
                unsigned type upper,
                const struct suffix_mult *suffixes)
@@ -21,10 +25,8 @@ unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
        int old_errno;
        char *e;
 
-       /* Disallow '-' and any leading whitespace.  Speed isn't critical here
-        * since we're parsing commandline args.  So make sure we get the
-        * actual isspace function rather than a lnumstrer macro implementaion. */
-       if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
+       /* Disallow '-' and any leading whitespace. */
+       if (*numstr == '-' || *numstr == '+' || isspace(*numstr))
                goto inval;
 
        /* Since this is a lib function, we're not allowed to reset errno to 0.
@@ -39,28 +41,28 @@ unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
        if (errno || numstr == e)
                goto inval; /* error / no digits / illegal trailing chars */
 
-       errno = old_errno;      /* Ok.  So restore errno. */
+       errno = old_errno;  /* Ok.  So restore errno. */
 
        /* 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) {
-               while (suffixes->suffix) {
+               while (suffixes->mult) {
                        if (strcmp(suffixes->suffix, e) == 0) {
                                if (XSTR_UTYPE_MAX / suffixes->mult < r)
                                        goto range; /* overflow! */
-                               ++e;
                                r *= suffixes->mult;
-                               break;
+                               goto chk_range;
                        }
                        ++suffixes;
                }
        }
 
        /* Note: trailing space is an error.
-          It would be easy enough to allow though if desired. */
+        * It would be easy enough to allow though if desired. */
        if (*e)
                goto inval;
+ chk_range:
        /* Finally, check for range limits. */
        if (r >= lower && r <= upper)
                return r;
@@ -72,25 +74,25 @@ unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
        bb_error_msg_and_die("invalid number '%s'", numstr);
 }
 
-unsigned type xstrtou(_range)(const char *numstr, int base,
+unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
                unsigned type lower,
                unsigned type upper)
 {
        return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
 }
 
-unsigned type xstrtou(_sfx)(const char *numstr, int base,
+unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
                const struct suffix_mult *suffixes)
 {
        return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
 }
 
-unsigned type xstrtou()(const char *numstr, int base)
+unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
 {
        return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
 }
 
-unsigned type xatou(_range_sfx)(const char *numstr,
+unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
                unsigned type lower,
                unsigned type upper,
                const struct suffix_mult *suffixes)
@@ -98,27 +100,27 @@ unsigned type xatou(_range_sfx)(const char *numstr,
        return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
 }
 
-unsigned type xatou(_range)(const char *numstr,
+unsigned type FAST_FUNC xatou(_range)(const char *numstr,
                unsigned type lower,
                unsigned type upper)
 {
        return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
 }
 
-unsigned type xatou(_sfx)(const char *numstr,
+unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
                const struct suffix_mult *suffixes)
 {
        return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
 }
 
-unsigned type xatou()(const char *numstr)
+unsigned type FAST_FUNC xatou()(const char *numstr)
 {
        return xatou(_sfx)(numstr, NULL);
 }
 
 /* Signed ones */
 
-type xstrto(_range_sfx)(const char *numstr, int base,
+type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
                type lower,
                type upper,
                const struct suffix_mult *suffixes)
@@ -127,9 +129,12 @@ type xstrto(_range_sfx)(const char *numstr, int base,
        type r;
        const char *p = numstr;
 
-       if (p[0] == '-') {
+       /* NB: if you'll decide to disallow '+':
+        * at least renice applet needs to allow it */
+       if (p[0] == '+' || p[0] == '-') {
                ++p;
-               ++u;    /* two's complement */
+               if (p[0] == '-')
+                       ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
        }
 
        r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
@@ -146,12 +151,17 @@ type xstrto(_range_sfx)(const char *numstr, int base,
        return r;
 }
 
-type xstrto(_range)(const char *numstr, int base, type lower, type upper)
+type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
 {
        return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
 }
 
-type xato(_range_sfx)(const char *numstr,
+type FAST_FUNC xstrto()(const char *numstr, int base)
+{
+       return xstrto(_range_sfx)(numstr, base, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
+}
+
+type FAST_FUNC xato(_range_sfx)(const char *numstr,
                type lower,
                type upper,
                const struct suffix_mult *suffixes)
@@ -159,17 +169,17 @@ type xato(_range_sfx)(const char *numstr,
        return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
 }
 
-type xato(_range)(const char *numstr, type lower, type upper)
+type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
 {
        return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
 }
 
-type xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
+type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
 {
        return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
 }
 
-type xato()(const char *numstr)
+type FAST_FUNC xato()(const char *numstr)
 {
        return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
 }