From aa82f69dea2f2d5fe4337dfb12cea54fabdab175 Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Sat, 6 Oct 2007 22:42:14 +0800 Subject: [BUILTIN] Use intmax_t arithmetic in test This patch adds the function atomax10 and uses it in test(1) so that we support intmax_t comparisons. --- src/mystring.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'src/mystring.c') diff --git a/src/mystring.c b/src/mystring.c index 7d937a8..df1691b 100644 --- a/src/mystring.c +++ b/src/mystring.c @@ -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" @@ -104,6 +109,29 @@ prefix(const char *string, const char *pfx) } +/* + * Convert a string into an integer of type intmax_t. Alow trailing spaces. + */ +intmax_t atomax10(const char *s) +{ + char *p; + intmax_t r; + + errno = 0; + r = strtoimax(s, &p, 10); + + if (errno != 0) + sh_error(illnum, s); + + while (isspace((unsigned char)*p)) + p++; + + if (*p) + sh_error(illnum, s); + + return r; +} + /* * Convert a string of digits to an integer, printing an error message on * failure. @@ -112,10 +140,12 @@ prefix(const char *string, const char *pfx) int number(const char *s) { + intmax_t n = atomax10(s); - if (! is_number(s)) + if (n < 0 || n > INT_MAX) sh_error(illnum, s); - return atoi(s); + + return n; } -- cgit 1.4.1