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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
| 11 | #define type long long |
| 12 | #define xstrtou(rest) xstrtoull##rest |
| 13 | #define xstrto(rest) xstrtoll##rest |
| 14 | #define xatou(rest) xatoull##rest |
| 15 | #define xato(rest) xatoll##rest |
| 16 | #define XSTR_UTYPE_MAX ULLONG_MAX |
| 17 | #define XSTR_TYPE_MAX LLONG_MAX |
| 18 | #define XSTR_TYPE_MIN LLONG_MIN |
| 19 | #define XSTR_STRTOU strtoull |
| 20 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 21 | |
| 22 | #if ULONG_MAX != ULLONG_MAX |
| 23 | #define type long |
| 24 | #define xstrtou(rest) xstrtoul##rest |
| 25 | #define xstrto(rest) xstrtol##rest |
| 26 | #define xatou(rest) xatoul##rest |
| 27 | #define xato(rest) xatol##rest |
| 28 | #define XSTR_UTYPE_MAX ULONG_MAX |
| 29 | #define XSTR_TYPE_MAX LONG_MAX |
| 30 | #define XSTR_TYPE_MIN LONG_MIN |
| 31 | #define XSTR_STRTOU strtoul |
| 32 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #if UINT_MAX != ULONG_MAX |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 36 | static ALWAYS_INLINE |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 37 | unsigned bb_strtoui(const char *str, char **end, int b) |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 38 | { |
| 39 | unsigned long v = strtoul(str, end, b); |
| 40 | if (v > UINT_MAX) { |
| 41 | errno = ERANGE; |
| 42 | return UINT_MAX; |
| 43 | } |
| 44 | return v; |
| 45 | } |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 46 | #define type int |
| 47 | #define xstrtou(rest) xstrtou##rest |
| 48 | #define xstrto(rest) xstrtoi##rest |
| 49 | #define xatou(rest) xatou##rest |
| 50 | #define xato(rest) xatoi##rest |
| 51 | #define XSTR_UTYPE_MAX UINT_MAX |
| 52 | #define XSTR_TYPE_MAX INT_MAX |
| 53 | #define XSTR_TYPE_MIN INT_MIN |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 54 | /* libc has no strtoui, so we need to create/use our own */ |
| 55 | #define XSTR_STRTOU bb_strtoui |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 56 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | /* A few special cases */ |
| 60 | |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 61 | int FAST_FUNC xatoi_positive(const char *numstr) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 62 | { |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 63 | return xatou_range(numstr, 0, INT_MAX); |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 66 | uint16_t FAST_FUNC xatou16(const char *numstr) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 67 | { |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 68 | return xatou_range(numstr, 0, 0xffff); |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 69 | } |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 70 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 71 | const struct suffix_mult bkm_suffixes[] ALIGN_SUFFIX = { |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 72 | { "b", 512 }, |
| 73 | { "k", 1024 }, |
| 74 | { "m", 1024*1024 }, |
| 75 | { "", 0 } |
| 76 | }; |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 77 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 78 | const struct suffix_mult cwbkMG_suffixes[] ALIGN_SUFFIX = { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 79 | { "c", 1 }, |
| 80 | { "w", 2 }, |
| 81 | { "b", 512 }, |
| 82 | { "kB", 1000 }, |
| 83 | { "kD", 1000 }, |
| 84 | { "k", 1024 }, |
Ari Sundholm | 911db16 | 2015-03-22 17:55:38 +0100 | [diff] [blame] | 85 | { "KB", 1000 }, /* compat with coreutils dd */ |
| 86 | { "KD", 1000 }, /* compat with coreutils dd */ |
| 87 | { "K", 1024 }, /* compat with coreutils dd */ |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 88 | { "MB", 1000000 }, |
| 89 | { "MD", 1000000 }, |
| 90 | { "M", 1024*1024 }, |
| 91 | { "GB", 1000000000 }, |
| 92 | { "GD", 1000000000 }, |
| 93 | { "G", 1024*1024*1024 }, |
| 94 | /* "D" suffix for decimal is not in coreutils manpage, looks like it's deprecated */ |
| 95 | /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */ |
| 96 | { "", 0 } |
| 97 | }; |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 98 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 99 | const struct suffix_mult kmg_i_suffixes[] ALIGN_SUFFIX = { |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 100 | { "KiB", 1024 }, |
| 101 | { "kiB", 1024 }, |
| 102 | { "K", 1024 }, |
| 103 | { "k", 1024 }, |
| 104 | { "MiB", 1048576 }, |
| 105 | { "miB", 1048576 }, |
| 106 | { "M", 1048576 }, |
| 107 | { "m", 1048576 }, |
| 108 | { "GiB", 1073741824 }, |
| 109 | { "giB", 1073741824 }, |
| 110 | { "G", 1073741824 }, |
| 111 | { "g", 1073741824 }, |
| 112 | { "KB", 1000 }, |
| 113 | { "MB", 1000000 }, |
| 114 | { "GB", 1000000000 }, |
| 115 | { "", 0 } |
| 116 | }; |