busybox/libbb/xatonum_template.c
<<
>>
Prefs
   1/*
   2 *
   3 * Licensed under GPLv2, see file LICENSE in this tarball for details.
   4 */
   5/*
   6You need to define the following (example):
   7
   8#define type long
   9#define xstrtou(rest) xstrtoul##rest
  10#define xstrto(rest) xstrtol##rest
  11#define xatou(rest) xatoul##rest
  12#define xato(rest) xatol##rest
  13#define XSTR_UTYPE_MAX ULONG_MAX
  14#define XSTR_TYPE_MAX LONG_MAX
  15#define XSTR_TYPE_MIN LONG_MIN
  16#define XSTR_STRTOU strtoul
  17*/
  18
  19unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
  20                unsigned type lower,
  21                unsigned type upper,
  22                const struct suffix_mult *suffixes)
  23{
  24        unsigned type r;
  25        int old_errno;
  26        char *e;
  27
  28        /* Disallow '-' and any leading whitespace. Make sure we get the
  29         * actual isspace function rather than a macro implementaion. */
  30        if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
  31                goto inval;
  32
  33        /* Since this is a lib function, we're not allowed to reset errno to 0.
  34         * Doing so could break an app that is deferring checking of errno.
  35         * So, save the old value so that we can restore it if successful. */
  36        old_errno = errno;
  37        errno = 0;
  38        r = XSTR_STRTOU(numstr, &e, base);
  39        /* Do the initial validity check.  Note: The standards do not
  40         * guarantee that errno is set if no digits were found.  So we
  41         * must test for this explicitly. */
  42        if (errno || numstr == e)
  43                goto inval; /* error / no digits / illegal trailing chars */
  44
  45        errno = old_errno;      /* Ok.  So restore errno. */
  46
  47        /* Do optional suffix parsing.  Allow 'empty' suffix tables.
  48         * Note that we also allow nul suffixes with associated multipliers,
  49         * to allow for scaling of the numstr by some default multiplier. */
  50        if (suffixes) {
  51                while (suffixes->mult) {
  52                        if (strcmp(suffixes->suffix, e) == 0) {
  53                                if (XSTR_UTYPE_MAX / suffixes->mult < r)
  54                                        goto range; /* overflow! */
  55                                r *= suffixes->mult;
  56                                goto chk_range;
  57                        }
  58                        ++suffixes;
  59                }
  60        }
  61
  62        /* Note: trailing space is an error.
  63           It would be easy enough to allow though if desired. */
  64        if (*e)
  65                goto inval;
  66 chk_range:
  67        /* Finally, check for range limits. */
  68        if (r >= lower && r <= upper)
  69                return r;
  70 range:
  71        bb_error_msg_and_die("number %s is not in %llu..%llu range",
  72                numstr, (unsigned long long)lower,
  73                (unsigned long long)upper);
  74 inval:
  75        bb_error_msg_and_die("invalid number '%s'", numstr);
  76}
  77
  78unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
  79                unsigned type lower,
  80                unsigned type upper)
  81{
  82        return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
  83}
  84
  85unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
  86                const struct suffix_mult *suffixes)
  87{
  88        return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
  89}
  90
  91unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
  92{
  93        return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
  94}
  95
  96unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
  97                unsigned type lower,
  98                unsigned type upper,
  99                const struct suffix_mult *suffixes)
 100{
 101        return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
 102}
 103
 104unsigned type FAST_FUNC xatou(_range)(const char *numstr,
 105                unsigned type lower,
 106                unsigned type upper)
 107{
 108        return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
 109}
 110
 111unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
 112                const struct suffix_mult *suffixes)
 113{
 114        return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
 115}
 116
 117unsigned type FAST_FUNC xatou()(const char *numstr)
 118{
 119        return xatou(_sfx)(numstr, NULL);
 120}
 121
 122/* Signed ones */
 123
 124type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
 125                type lower,
 126                type upper,
 127                const struct suffix_mult *suffixes)
 128{
 129        unsigned type u = XSTR_TYPE_MAX;
 130        type r;
 131        const char *p = numstr;
 132
 133        /* NB: if you'll decide to disallow '+':
 134         * at least renice applet needs to allow it */
 135        if (p[0] == '+' || p[0] == '-') {
 136                ++p;
 137                if (p[0] == '-')
 138                        ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
 139        }
 140
 141        r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
 142
 143        if (*numstr == '-') {
 144                r = -r;
 145        }
 146
 147        if (r < lower || r > upper) {
 148                bb_error_msg_and_die("number %s is not in %lld..%lld range",
 149                                numstr, (long long)lower, (long long)upper);
 150        }
 151
 152        return r;
 153}
 154
 155type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
 156{
 157        return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
 158}
 159
 160type FAST_FUNC xato(_range_sfx)(const char *numstr,
 161                type lower,
 162                type upper,
 163                const struct suffix_mult *suffixes)
 164{
 165        return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
 166}
 167
 168type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
 169{
 170        return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
 171}
 172
 173type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
 174{
 175        return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
 176}
 177
 178type FAST_FUNC xato()(const char *numstr)
 179{
 180        return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
 181}
 182
 183#undef type
 184#undef xstrtou
 185#undef xstrto
 186#undef xatou
 187#undef xato
 188#undef XSTR_UTYPE_MAX
 189#undef XSTR_TYPE_MAX
 190#undef XSTR_TYPE_MIN
 191#undef XSTR_STRTOU
 192