--- trunk/mkinitrd-magellan/klibc/usr/dash/mystring.c 2010/07/22 07:47:49 1121 +++ trunk/mkinitrd-magellan/klibc/usr/dash/mystring.c 2010/08/18 21:11:40 1122 @@ -42,6 +42,11 @@ * is_number(s) Return true if s is a string of digits. */ +#include +#include +#include +#include +#include #include #include "shell.h" #include "syntax.h" @@ -55,7 +60,8 @@ char nullstr[1]; /* zero length string */ const char spcstr[] = " "; const char snlfmt[] = "%s\n"; -const char dolatstr[] = { CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' }; +const char dolatstr[] = { CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', + CTLQUOTEMARK, '\0' }; const char illnum[] = "Illegal number: %s"; const char homestr[] = "HOME"; @@ -102,6 +108,45 @@ return (char *) string; } +void badnum(const char *s) +{ + sh_error(illnum, s); +} + +/* + * Convert a string into an integer of type intmax_t. Alow trailing spaces. + */ +intmax_t atomax(const char *s, int base) +{ + char *p; + intmax_t r; + + errno = 0; + r = strtoimax(s, &p, base); + + if (errno != 0) + badnum(s); + + /* + * Disallow completely blank strings in non-arithmetic (base != 0) + * contexts. + */ + if (p == s && base) + badnum(s); + + while (isspace((unsigned char)*p)) + p++; + + if (*p) + badnum(s); + + return r; +} + +intmax_t atomax10(const char *s) +{ + return atomax(s, 10); +} /* * Convert a string of digits to an integer, printing an error message on @@ -111,10 +156,12 @@ int number(const char *s) { + intmax_t n = atomax10(s); + + if (n < 0 || n > INT_MAX) + badnum(s); - if (! is_number(s)) - sh_error(illnum, s); - return atoi(s); + return n; }