summary refs log tree commit diff
path: root/src/mystring.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2007-10-06 22:42:14 +0800
committerHerbert Xu <herbert@gondor.apana.org.au>2007-10-06 22:42:14 +0800
commitaa82f69dea2f2d5fe4337dfb12cea54fabdab175 (patch)
tree3648d6fc0a7280d8bb7d437ac3a2e6e548705599 /src/mystring.c
parent[VAR] Remove setvarsafe (diff)
downloaddash-aa82f69dea2f2d5fe4337dfb12cea54fabdab175.tar.gz
dash-aa82f69dea2f2d5fe4337dfb12cea54fabdab175.zip
[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.
Diffstat (limited to 'src/mystring.c')
-rw-r--r--src/mystring.c34
1 files changed, 32 insertions, 2 deletions
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 <ctype.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include "shell.h"
 #include "syntax.h"
@@ -105,6 +110,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;
 }