1 /* vi: set sw=4 ts=4: */
3 * ascii-to-numbers implementations for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
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"
22 #if ULONG_MAX != ULLONG_MAX
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"
35 #if UINT_MAX != ULONG_MAX
37 unsigned bb_strtoui(const char *str, char **end, int b)
39 unsigned long v = strtoul(str, end, b);
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
54 /* libc has no strtoui, so we need to create/use our own */
55 #define XSTR_STRTOU bb_strtoui
56 #include "xatonum_template.c"
59 /* A few special cases */
61 int FAST_FUNC xatoi_positive(const char *numstr)
63 return xatou_range(numstr, 0, INT_MAX);
66 uint16_t FAST_FUNC xatou16(const char *numstr)
68 return xatou_range(numstr, 0, 0xffff);
71 const struct suffix_mult bkm_suffixes[] = {
78 const struct suffix_mult cwbkMG_suffixes[] = {
85 { "KB", 1000 }, /* compat with coreutils dd */
86 { "KD", 1000 }, /* compat with coreutils dd */
87 { "K", 1024 }, /* compat with coreutils dd */
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 */
99 const struct suffix_mult kmg_i_suffixes[] = {
108 { "GiB", 1073741824 },
109 { "giB", 1073741824 },
114 { "GB", 1000000000 },