stty: fix columns command. closes bug 791.
[oweals/busybox.git] / libbb / bb_strtonum.c
index 525c830cdfb606382ed2f49616448dc842b85b8d..87cd744a72765cd277c10f3f21d4859ebcfc3f6b 100644 (file)
@@ -10,7 +10,7 @@
 #include "libbb.h"
 
 /* On exit: errno = 0 only if there was non-empty, '\0' terminated value
- * errno = EINVAL if value was not '\0' terminated, but othervise ok
+ * errno = EINVAL if value was not '\0' terminated, but otherwise ok
  *    Return value is still valid, caller should just check whether end[0]
  *    is a valid terminating char for particular case. OTOH, if caller
  *    requires '\0' terminated input, [s]he can just check errno == 0.
  * errno = ERANGE if value is out of range, missing, etc.
  * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok )
  *    return value is all-ones in this case.
+ *
+ * Test code:
+ * char *endptr;
+ * const char *minus = "-";
+ * errno = 0;
+ * bb_strtoi(minus, &endptr, 0); // must set ERANGE
+ * printf("minus:%p endptr:%p errno:%d EINVAL:%d\n", minus, endptr, errno, EINVAL);
+ * errno = 0;
+ * bb_strtoi("-0-", &endptr, 0); // must set EINVAL and point to second '-'
+ * printf("endptr[0]:%c errno:%d EINVAL:%d\n", endptr[0], errno, EINVAL);
  */
 
 static unsigned long long ret_ERANGE(void)
@@ -30,12 +40,6 @@ static unsigned long long handle_errors(unsigned long long v, char **endp, char
 {
        if (endp) *endp = endptr;
 
-       /* Check for the weird "feature":
-        * a "-" string is apparently a valid "number" for strto[u]l[l]!
-        * It returns zero and errno is 0! :( */
-       if (endptr[-1] == '-')
-               return ret_ERANGE();
-
        /* errno is already set to ERANGE by strtoXXX if value overflowed */
        if (endptr[0]) {
                /* "1234abcg" or out-of-range? */
@@ -68,7 +72,12 @@ long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base)
        unsigned long long v;
        char *endptr;
 
-       if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE();
+       /* Check for the weird "feature":
+        * a "-" string is apparently a valid "number" for strto[u]l[l]!
+        * It returns zero and errno is 0! :( */
+       char first = (arg[0] != '-' ? arg[0] : arg[1]);
+       if (!isalnum(first)) return ret_ERANGE();
+
        errno = 0;
        v = strtoll(arg, &endptr, base);
        return handle_errors(v, endp, endptr);
@@ -91,7 +100,9 @@ long FAST_FUNC bb_strtol(const char *arg, char **endp, int base)
        long v;
        char *endptr;
 
-       if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE();
+       char first = (arg[0] != '-' ? arg[0] : arg[1]);
+       if (!isalnum(first)) return ret_ERANGE();
+
        errno = 0;
        v = strtol(arg, &endptr, base);
        return handle_errors(v, endp, endptr);
@@ -116,7 +127,9 @@ int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
        long v;
        char *endptr;
 
-       if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE();
+       char first = (arg[0] != '-' ? arg[0] : arg[1]);
+       if (!isalnum(first)) return ret_ERANGE();
+
        errno = 0;
        v = strtol(arg, &endptr, base);
        if (v > INT_MAX) return ret_ERANGE();
@@ -124,33 +137,3 @@ int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
        return handle_errors(v, endp, endptr);
 }
 #endif
-
-/* Floating point */
-
-#if 0
-
-#include <math.h>  /* just for HUGE_VAL */
-#define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
-double FAST_FUNC bb_strtod(const char *arg, char **endp)
-{
-       double v;
-       char *endptr;
-
-       if (arg[0] != '-' && NOT_DIGIT(arg[0])) goto err;
-       errno = 0;
-       v = strtod(arg, &endptr);
-       if (endp) *endp = endptr;
-       if (endptr[0]) {
-               /* "1234abcg" or out-of-range? */
-               if (isalnum(endptr[0]) || errno) {
- err:
-                       errno = ERANGE;
-                       return HUGE_VAL;
-               }
-               /* good number, just suspicious terminator */
-               errno = EINVAL;
-       }
-       return v;
-}
-
-#endif