blob: c66c774f4f8a462c4528a9cc3d41b85c4cbc4855 [file] [log] [blame]
Denis Vlasenko3ece72d2006-11-27 15:12:16 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko3ece72d2006-11-27 15:12:16 +00008 */
9
10#include "libbb.h"
11
12/* On exit: errno = 0 only if there was non-empty, '\0' terminated value
Denis Vlasenko49a5eba2008-07-23 08:41:08 +000013 * errno = EINVAL if value was not '\0' terminated, but otherwise ok
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000014 * Return value is still valid, caller should just check whether end[0]
15 * is a valid terminating char for particular case. OTOH, if caller
16 * requires '\0' terminated input, [s]he can just check errno == 0.
17 * errno = ERANGE if value had alphanumeric terminating char ("1234abcg").
18 * errno = ERANGE if value is out of range, missing, etc.
19 * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok )
Denis Vlasenko666da5e2006-12-26 18:17:42 +000020 * return value is all-ones in this case.
Denis Vlasenkof19817d2008-07-18 18:17:10 +000021 *
22 * Test code:
23 * char *endptr;
24 * const char *minus = "-";
25 * errno = 0;
26 * bb_strtoi(minus, &endptr, 0); // must set ERANGE
27 * printf("minus:%p endptr:%p errno:%d EINVAL:%d\n", minus, endptr, errno, EINVAL);
28 * errno = 0;
29 * bb_strtoi("-0-", &endptr, 0); // must set EINVAL and point to second '-'
30 * printf("endptr[0]:%c errno:%d EINVAL:%d\n", endptr[0], errno, EINVAL);
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000031 */
32
33static unsigned long long ret_ERANGE(void)
34{
35 errno = ERANGE; /* this ain't as small as it looks (on glibc) */
36 return ULLONG_MAX;
37}
38
39static unsigned long long handle_errors(unsigned long long v, char **endp, char *endptr)
40{
41 if (endp) *endp = endptr;
42
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000043 /* errno is already set to ERANGE by strtoXXX if value overflowed */
44 if (endptr[0]) {
45 /* "1234abcg" or out-of-range? */
46 if (isalnum(endptr[0]) || errno)
47 return ret_ERANGE();
48 /* good number, just suspicious terminator */
49 errno = EINVAL;
50 }
51 return v;
52}
53
54
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000055unsigned long long FAST_FUNC bb_strtoull(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000056{
Denis Vlasenkodf381882006-11-28 10:54:16 +000057 unsigned long long v;
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000058 char *endptr;
59
60 /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */
61 /* I don't think that this is right. Preventing this... */
62 if (!isalnum(arg[0])) return ret_ERANGE();
63
64 /* not 100% correct for lib func, but convenient for the caller */
65 errno = 0;
Denis Vlasenkodf381882006-11-28 10:54:16 +000066 v = strtoull(arg, &endptr, base);
67 return handle_errors(v, endp, endptr);
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000068}
69
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000070long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000071{
Denis Vlasenkodf381882006-11-28 10:54:16 +000072 unsigned long long v;
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000073 char *endptr;
74
Denis Vlasenkof19817d2008-07-18 18:17:10 +000075 /* Check for the weird "feature":
76 * a "-" string is apparently a valid "number" for strto[u]l[l]!
77 * It returns zero and errno is 0! :( */
78 char first = (arg[0] != '-' ? arg[0] : arg[1]);
79 if (!isalnum(first)) return ret_ERANGE();
80
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000081 errno = 0;
Denis Vlasenkodf381882006-11-28 10:54:16 +000082 v = strtoll(arg, &endptr, base);
83 return handle_errors(v, endp, endptr);
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000084}
85
86#if ULONG_MAX != ULLONG_MAX
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000087unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000088{
Denis Vlasenkodf381882006-11-28 10:54:16 +000089 unsigned long v;
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000090 char *endptr;
91
92 if (!isalnum(arg[0])) return ret_ERANGE();
93 errno = 0;
Denis Vlasenkodf381882006-11-28 10:54:16 +000094 v = strtoul(arg, &endptr, base);
95 return handle_errors(v, endp, endptr);
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000096}
97
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000098long FAST_FUNC bb_strtol(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +000099{
Denis Vlasenkodf381882006-11-28 10:54:16 +0000100 long v;
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000101 char *endptr;
102
Denis Vlasenkof19817d2008-07-18 18:17:10 +0000103 char first = (arg[0] != '-' ? arg[0] : arg[1]);
104 if (!isalnum(first)) return ret_ERANGE();
105
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000106 errno = 0;
Denis Vlasenkodf381882006-11-28 10:54:16 +0000107 v = strtol(arg, &endptr, base);
108 return handle_errors(v, endp, endptr);
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000109}
110#endif
111
112#if UINT_MAX != ULONG_MAX
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000113unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000114{
115 unsigned long v;
116 char *endptr;
117
118 if (!isalnum(arg[0])) return ret_ERANGE();
119 errno = 0;
120 v = strtoul(arg, &endptr, base);
121 if (v > UINT_MAX) return ret_ERANGE();
122 return handle_errors(v, endp, endptr);
123}
124
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000125int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000126{
127 long v;
128 char *endptr;
129
Denis Vlasenkof19817d2008-07-18 18:17:10 +0000130 char first = (arg[0] != '-' ? arg[0] : arg[1]);
131 if (!isalnum(first)) return ret_ERANGE();
132
Denis Vlasenko3ece72d2006-11-27 15:12:16 +0000133 errno = 0;
134 v = strtol(arg, &endptr, base);
135 if (v > INT_MAX) return ret_ERANGE();
136 if (v < INT_MIN) return ret_ERANGE();
137 return handle_errors(v, endp, endptr);
138}
139#endif