blob: 36b06c8496ab0c21265e8cd3f01f4b5d0d0b2884 [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 */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00009#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 Vlasenkoed836cd2006-11-25 14:44:13 +000021
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 Vlasenkoed836cd2006-11-25 14:44:13 +000033#endif
34
35#if UINT_MAX != ULONG_MAX
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +000036static ALWAYS_INLINE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000037unsigned bb_strtoui(const char *str, char **end, int b)
Denis Vlasenko43bddf32006-11-25 14:49:04 +000038{
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 Vlasenkoed836cd2006-11-25 14:44:13 +000046#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 Vlasenko43bddf32006-11-25 14:49:04 +000054/* libc has no strtoui, so we need to create/use our own */
55#define XSTR_STRTOU bb_strtoui
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000056#include "xatonum_template.c"
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000057#endif
58
59/* A few special cases */
60
Denys Vlasenko77832482010-08-12 14:14:45 +020061int FAST_FUNC xatoi_positive(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000062{
Denis Vlasenko43bddf32006-11-25 14:49:04 +000063 return xatou_range(numstr, 0, INT_MAX);
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000064}
65
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000066uint16_t FAST_FUNC xatou16(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000067{
Denis Vlasenko43bddf32006-11-25 14:49:04 +000068 return xatou_range(numstr, 0, 0xffff);
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000069}
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020070
Denys Vlasenko965b7952020-11-30 13:03:03 +010071const struct suffix_mult bkm_suffixes[] ALIGN_SUFFIX = {
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020072 { "b", 512 },
73 { "k", 1024 },
74 { "m", 1024*1024 },
75 { "", 0 }
76};
Ari Sundholm88930232015-03-04 18:46:48 +020077
Denys Vlasenko965b7952020-11-30 13:03:03 +010078const struct suffix_mult cwbkMG_suffixes[] ALIGN_SUFFIX = {
Ari Sundholm88930232015-03-04 18:46:48 +020079 { "c", 1 },
80 { "w", 2 },
81 { "b", 512 },
82 { "kB", 1000 },
83 { "kD", 1000 },
84 { "k", 1024 },
Ari Sundholm911db162015-03-22 17:55:38 +010085 { "KB", 1000 }, /* compat with coreutils dd */
86 { "KD", 1000 }, /* compat with coreutils dd */
87 { "K", 1024 }, /* compat with coreutils dd */
Ari Sundholm88930232015-03-04 18:46:48 +020088 { "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 Vlasenkoe184a882016-12-12 19:56:31 +010098
Denys Vlasenko965b7952020-11-30 13:03:03 +010099const struct suffix_mult kmg_i_suffixes[] ALIGN_SUFFIX = {
Denys Vlasenkoe184a882016-12-12 19:56:31 +0100100 { "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};