blob: 9dd5c3e7e665dcf13cb1b72ceb01c09b7baf5e66 [file] [log] [blame]
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00008 */
9
10#include "libbb.h"
11
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"
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000022
23#if ULONG_MAX != ULLONG_MAX
24#define type long
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"
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000034#endif
35
36#if UINT_MAX != ULONG_MAX
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +000037static ALWAYS_INLINE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000038unsigned bb_strtoui(const char *str, char **end, int b)
Denis Vlasenko43bddf32006-11-25 14:49:04 +000039{
40 unsigned long v = strtoul(str, end, b);
41 if (v > UINT_MAX) {
42 errno = ERANGE;
43 return UINT_MAX;
44 }
45 return v;
46}
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000047#define type int
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
Denis Vlasenko43bddf32006-11-25 14:49:04 +000055/* libc has no strtoui, so we need to create/use our own */
56#define XSTR_STRTOU bb_strtoui
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000057#include "xatonum_template.c"
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000058#endif
59
60/* A few special cases */
61
Denys Vlasenko77832482010-08-12 14:14:45 +020062int FAST_FUNC xatoi_positive(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000063{
Denis Vlasenko43bddf32006-11-25 14:49:04 +000064 return xatou_range(numstr, 0, INT_MAX);
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000065}
66
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000067uint16_t FAST_FUNC xatou16(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000068{
Denis Vlasenko43bddf32006-11-25 14:49:04 +000069 return xatou_range(numstr, 0, 0xffff);
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000070}
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020071
72const struct suffix_mult bkm_suffixes[] = {
73 { "b", 512 },
74 { "k", 1024 },
75 { "m", 1024*1024 },
76 { "", 0 }
77};
Ari Sundholm88930232015-03-04 18:46:48 +020078
79const struct suffix_mult cwbkMG_suffixes[] = {
80 { "c", 1 },
81 { "w", 2 },
82 { "b", 512 },
83 { "kB", 1000 },
84 { "kD", 1000 },
85 { "k", 1024 },
Ari Sundholm911db162015-03-22 17:55:38 +010086 { "KB", 1000 }, /* compat with coreutils dd */
87 { "KD", 1000 }, /* compat with coreutils dd */
88 { "K", 1024 }, /* compat with coreutils dd */
Ari Sundholm88930232015-03-04 18:46:48 +020089 { "MB", 1000000 },
90 { "MD", 1000000 },
91 { "M", 1024*1024 },
92 { "GB", 1000000000 },
93 { "GD", 1000000000 },
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 */
97 { "", 0 }
98};