Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ascii-to-numbers implementations for busybox |
| 4 | * |
| 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 10 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 12 | /* Provides extern declarations of functions */ |
| 13 | #define DECLARE_STR_CONV(type, T, UT) \ |
| 14 | \ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 15 | unsigned type xstrto##UT##_range_sfx(const char *str, int b, unsigned type l, unsigned type u, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 16 | unsigned type xstrto##UT##_range(const char *str, int b, unsigned type l, unsigned type u) FAST_FUNC; \ |
| 17 | unsigned type xstrto##UT##_sfx(const char *str, int b, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 18 | unsigned type xstrto##UT(const char *str, int b) FAST_FUNC; \ |
| 19 | unsigned type xato##UT##_range_sfx(const char *str, unsigned type l, unsigned type u, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 20 | unsigned type xato##UT##_range(const char *str, unsigned type l, unsigned type u) FAST_FUNC; \ |
| 21 | unsigned type xato##UT##_sfx(const char *str, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 22 | unsigned type xato##UT(const char *str) FAST_FUNC; \ |
| 23 | type xstrto##T##_range_sfx(const char *str, int b, type l, type u, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 24 | type xstrto##T##_range(const char *str, int b, type l, type u) FAST_FUNC; \ |
| 25 | type xato##T##_range_sfx(const char *str, type l, type u, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 26 | type xato##T##_range(const char *str, type l, type u) FAST_FUNC; \ |
| 27 | type xato##T##_sfx(const char *str, const struct suffix_mult *sfx) FAST_FUNC; \ |
| 28 | type xato##T(const char *str) FAST_FUNC; \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 29 | |
| 30 | /* Unsigned long long functions always exist */ |
| 31 | DECLARE_STR_CONV(long long, ll, ull) |
| 32 | |
| 33 | |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 34 | /* Provides inline definitions of functions */ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 35 | /* (useful for mapping them to the type of the same width) */ |
| 36 | #define DEFINE_EQUIV_STR_CONV(narrow, N, W, UN, UW) \ |
| 37 | \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 38 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 39 | unsigned narrow xstrto##UN##_range_sfx(const char *str, int b, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \ |
| 40 | { return xstrto##UW##_range_sfx(str, b, l, u, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 41 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 42 | unsigned narrow xstrto##UN##_range(const char *str, int b, unsigned narrow l, unsigned narrow u) \ |
| 43 | { return xstrto##UW##_range(str, b, l, u); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 44 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 45 | unsigned narrow xstrto##UN##_sfx(const char *str, int b, const struct suffix_mult *sfx) \ |
| 46 | { return xstrto##UW##_sfx(str, b, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 47 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 48 | unsigned narrow xstrto##UN(const char *str, int b) \ |
| 49 | { return xstrto##UW(str, b); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 50 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 51 | unsigned narrow xato##UN##_range_sfx(const char *str, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \ |
| 52 | { return xato##UW##_range_sfx(str, l, u, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 53 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 54 | unsigned narrow xato##UN##_range(const char *str, unsigned narrow l, unsigned narrow u) \ |
| 55 | { return xato##UW##_range(str, l, u); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 56 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 57 | unsigned narrow xato##UN##_sfx(const char *str, const struct suffix_mult *sfx) \ |
| 58 | { return xato##UW##_sfx(str, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 59 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 60 | unsigned narrow xato##UN(const char *str) \ |
| 61 | { return xato##UW(str); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 62 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 63 | narrow xstrto##N##_range_sfx(const char *str, int b, narrow l, narrow u, const struct suffix_mult *sfx) \ |
| 64 | { return xstrto##W##_range_sfx(str, b, l, u, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 65 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 66 | narrow xstrto##N##_range(const char *str, int b, narrow l, narrow u) \ |
| 67 | { return xstrto##W##_range(str, b, l, u); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 68 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 69 | narrow xato##N##_range_sfx(const char *str, narrow l, narrow u, const struct suffix_mult *sfx) \ |
| 70 | { return xato##W##_range_sfx(str, l, u, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 71 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 72 | narrow xato##N##_range(const char *str, narrow l, narrow u) \ |
| 73 | { return xato##W##_range(str, l, u); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 74 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 75 | narrow xato##N##_sfx(const char *str, const struct suffix_mult *sfx) \ |
| 76 | { return xato##W##_sfx(str, sfx); } \ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 77 | static ALWAYS_INLINE \ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 78 | narrow xato##N(const char *str) \ |
| 79 | { return xato##W(str); } \ |
| 80 | |
| 81 | /* If long == long long, then just map them one-to-one */ |
| 82 | #if ULONG_MAX == ULLONG_MAX |
| 83 | DEFINE_EQUIV_STR_CONV(long, l, ll, ul, ull) |
| 84 | #else |
| 85 | /* Else provide extern defs */ |
| 86 | DECLARE_STR_CONV(long, l, ul) |
| 87 | #endif |
| 88 | |
| 89 | /* Same for int -> [long] long */ |
| 90 | #if UINT_MAX == ULLONG_MAX |
| 91 | DEFINE_EQUIV_STR_CONV(int, i, ll, u, ull) |
| 92 | #elif UINT_MAX == ULONG_MAX |
| 93 | DEFINE_EQUIV_STR_CONV(int, i, l, u, ul) |
| 94 | #else |
| 95 | DECLARE_STR_CONV(int, i, u) |
| 96 | #endif |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 97 | |
| 98 | /* Specialized */ |
| 99 | |
| 100 | int BUG_xatou32_unimplemented(void); |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 101 | static ALWAYS_INLINE uint32_t xatou32(const char *numstr) |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 102 | { |
| 103 | if (UINT_MAX == 0xffffffff) |
| 104 | return xatou(numstr); |
| 105 | if (ULONG_MAX == 0xffffffff) |
| 106 | return xatoul(numstr); |
| 107 | return BUG_xatou32_unimplemented(); |
| 108 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | f74194e | 2007-10-18 12:54:39 +0000 | [diff] [blame] | 110 | /* Non-aborting kind of convertors: bb_strto[u][l]l */ |
| 111 | |
| 112 | /* On exit: errno = 0 only if there was non-empty, '\0' terminated value |
Denis Vlasenko | 49a5eba | 2008-07-23 08:41:08 +0000 | [diff] [blame] | 113 | * errno = EINVAL if value was not '\0' terminated, but otherwise ok |
Denis Vlasenko | f74194e | 2007-10-18 12:54:39 +0000 | [diff] [blame] | 114 | * Return value is still valid, caller should just check whether end[0] |
| 115 | * is a valid terminating char for particular case. OTOH, if caller |
| 116 | * requires '\0' terminated input, [s]he can just check errno == 0. |
| 117 | * errno = ERANGE if value had alphanumeric terminating char ("1234abcg"). |
| 118 | * errno = ERANGE if value is out of range, missing, etc. |
| 119 | * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok ) |
| 120 | * return value is all-ones in this case. |
| 121 | */ |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 123 | unsigned long long bb_strtoull(const char *arg, char **endp, int base) FAST_FUNC; |
| 124 | long long bb_strtoll(const char *arg, char **endp, int base) FAST_FUNC; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 125 | |
| 126 | #if ULONG_MAX == ULLONG_MAX |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 127 | static ALWAYS_INLINE |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 128 | unsigned long bb_strtoul(const char *arg, char **endp, int base) |
| 129 | { return bb_strtoull(arg, endp, base); } |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 130 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 131 | long bb_strtol(const char *arg, char **endp, int base) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 132 | { return bb_strtoll(arg, endp, base); } |
| 133 | #else |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 134 | unsigned long bb_strtoul(const char *arg, char **endp, int base) FAST_FUNC; |
| 135 | long bb_strtol(const char *arg, char **endp, int base) FAST_FUNC; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | #if UINT_MAX == ULLONG_MAX |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 139 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 140 | unsigned bb_strtou(const char *arg, char **endp, int base) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 141 | { return bb_strtoull(arg, endp, base); } |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 142 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 143 | int bb_strtoi(const char *arg, char **endp, int base) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 144 | { return bb_strtoll(arg, endp, base); } |
| 145 | #elif UINT_MAX == ULONG_MAX |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 146 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 147 | unsigned bb_strtou(const char *arg, char **endp, int base) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 148 | { return bb_strtoul(arg, endp, base); } |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 149 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 150 | int bb_strtoi(const char *arg, char **endp, int base) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 151 | { return bb_strtol(arg, endp, base); } |
| 152 | #else |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 153 | unsigned bb_strtou(const char *arg, char **endp, int base) FAST_FUNC; |
| 154 | int bb_strtoi(const char *arg, char **endp, int base) FAST_FUNC; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | int BUG_bb_strtou32_unimplemented(void); |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 158 | static ALWAYS_INLINE |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 159 | uint32_t bb_strtou32(const char *arg, char **endp, int base) |
| 160 | { |
| 161 | if (sizeof(uint32_t) == sizeof(unsigned)) |
| 162 | return bb_strtou(arg, endp, base); |
| 163 | if (sizeof(uint32_t) == sizeof(unsigned long)) |
| 164 | return bb_strtoul(arg, endp, base); |
| 165 | return BUG_bb_strtou32_unimplemented(); |
| 166 | } |
| 167 | |
| 168 | /* Floating point */ |
| 169 | |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 170 | double bb_strtod(const char *arg, char **endp) FAST_FUNC; |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 172 | POP_SAVED_FUNCTION_VISIBILITY |