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.
12 #define type long long
13 #define xstrtou(rest) xstrtoull##rest
14 #define xstrto(rest) xstrtoll##rest
15 #define xatou(rest) xatoull##rest
16 #define xato(rest) xatoll##rest
17 #define XSTR_UTYPE_MAX ULLONG_MAX
18 #define XSTR_TYPE_MAX LLONG_MAX
19 #define XSTR_TYPE_MIN LLONG_MIN
20 #define XSTR_STRTOU strtoull
21 #include "xatonum_template.c"
23 #if ULONG_MAX != ULLONG_MAX
25 #define xstrtou(rest) xstrtoul##rest
26 #define xstrto(rest) xstrtol##rest
27 #define xatou(rest) xatoul##rest
28 #define xato(rest) xatol##rest
29 #define XSTR_UTYPE_MAX ULONG_MAX
30 #define XSTR_TYPE_MAX LONG_MAX
31 #define XSTR_TYPE_MIN LONG_MIN
32 #define XSTR_STRTOU strtoul
33 #include "xatonum_template.c"
36 #if UINT_MAX != ULONG_MAX
38 unsigned bb_strtoui(const char *str, char **end, int b)
40 unsigned long v = strtoul(str, end, b);
48 #define xstrtou(rest) xstrtou##rest
49 #define xstrto(rest) xstrtoi##rest
50 #define xatou(rest) xatou##rest
51 #define xato(rest) xatoi##rest
52 #define XSTR_UTYPE_MAX UINT_MAX
53 #define XSTR_TYPE_MAX INT_MAX
54 #define XSTR_TYPE_MIN INT_MIN
55 /* libc has no strtoui, so we need to create/use our own */
56 #define XSTR_STRTOU bb_strtoui
57 #include "xatonum_template.c"
60 /* A few special cases */
62 int FAST_FUNC xatoi_positive(const char *numstr)
64 return xatou_range(numstr, 0, INT_MAX);
67 uint16_t FAST_FUNC xatou16(const char *numstr)
69 return xatou_range(numstr, 0, 0xffff);
72 const struct suffix_mult bkm_suffixes[] = {
79 const struct suffix_mult cwbkMG_suffixes[] = {
86 { "KB", 1000 }, /* compat with coreutils dd */
87 { "KD", 1000 }, /* compat with coreutils dd */
88 { "K", 1024 }, /* compat with coreutils dd */
94 { "G", 1024*1024*1024 },
95 /* "D" suffix for decimal is not in coreutils manpage, looks like it's deprecated */
96 /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */
100 const struct suffix_mult kmg_i_suffixes[] = {
109 { "GiB", 1073741824 },
110 { "giB", 1073741824 },
115 { "GB", 1000000000 },