X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fxstrtol.c;h=591493e50c78cc4b6bd07b48754df86f88e5012f;hb=9b8ea95435bc2ccb0882039535ccd5fac84dc935;hp=4d57d94318217a4e47eebc9daa5dca04bc76ca28;hpb=3af47ca3f672f6c43bc63843a91ecfac278d846f;p=gnulib.git diff --git a/lib/xstrtol.c b/lib/xstrtol.c index 4d57d9431..591493e50 100644 --- a/lib/xstrtol.c +++ b/lib/xstrtol.c @@ -2,14 +2,25 @@ #include #endif +#include "xstrtol.h" /* Get definition for __P before use. */ + #ifdef STDC_HEADERS #include #else -long int __strtol (const char *, char **, int base); +__unsigned long int __strtol __P ((const char *, char **, int base)); +#endif + +#ifdef HAVE_STRING_H +# include +#else +# include +# ifndef strchr +# define strchr index +# endif #endif +#define NDEBUG #include -/* FIXME: define NDEBUG before release. */ #include #ifndef errno @@ -28,19 +39,26 @@ extern int errno; #define LONG_MAX ((long int) (ULONG_MAX >> 1)) #endif -#include "xstrtol.h" +#define BKM_SCALE(x, scale_factor, error_return) \ + do \ + { \ + if ((x) > (double) __ZLONG_MAX / (scale_factor)) \ + return (error_return); \ + (x) *= (scale_factor); \ + } \ + while (0) __unsigned long int __strtol (); /* FIXME: comment. */ strtol_error -__xstrtol (s, ptr, base, val, allow_bkm_suffix) +__xstrtol (s, ptr, base, val, valid_suffixes) const char *s; char **ptr; int base; __unsigned long int *val; - int allow_bkm_suffix; + const char *valid_suffixes; { char *t_ptr; char **p; @@ -56,7 +74,7 @@ __xstrtol (s, ptr, base, val, allow_bkm_suffix) return LONGINT_OVERFLOW; if (*p == s) return LONGINT_INVALID; - if (!allow_bkm_suffix) + if (!valid_suffixes) { if (**p == '\0') { @@ -67,38 +85,39 @@ __xstrtol (s, ptr, base, val, allow_bkm_suffix) return LONGINT_INVALID_SUFFIX_CHAR; } - switch (**p) + if (**p != '\0' && strchr (valid_suffixes, **p)) { - case '\0': - break; - -#define BKM_SCALE(x, scale_factor, error_return) \ - do \ - { \ - if ((x) > (double) __ZLONG_MAX / (scale_factor)) \ - return (error_return); \ - (x) *= (scale_factor); \ - } \ - while (0) - - case 'b': - BKM_SCALE (tmp, 512, LONGINT_OVERFLOW); - ++(*p); - break; - - case 'k': - BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW); - ++(*p); - break; - - case 'm': - BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW); - ++(*p); - break; - - default: - return LONGINT_INVALID_SUFFIX_CHAR; - break; + switch (**p) + { + case 'b': + BKM_SCALE (tmp, 512, LONGINT_OVERFLOW); + ++(*p); + break; + + case 'c': + ++(*p); + break; + + case 'B': + case 'k': + BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW); + ++(*p); + break; + + case 'm': + BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW); + ++(*p); + break; + + case 'w': + BKM_SCALE (tmp, 2, LONGINT_OVERFLOW); + ++(*p); + break; + + default: + return LONGINT_INVALID_SUFFIX_CHAR; + break; + } } *val = tmp; @@ -124,7 +143,7 @@ main (int argc, char** argv) char *p; __unsigned long int val; - s_err = __xstrtol (argv[i], &p, 0, &val, 1); + s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw"); if (s_err == LONGINT_OK) { printf ("%s->%lu (%s)\n", argv[i], val, p);