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