aboutsummaryrefslogtreecommitdiff
path: root/shell/math.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-05-19 17:23:31 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-05-19 17:23:31 +0200
commit9edd268bad93128bcadfbdde28bb978a4b4c5bab (patch)
tree0163e19dd3fb1dc0b16c3c694f4ba39fee0c5cb8 /shell/math.c
parent30a4c32a4d21728a7e25025f70fcc1d7cd722fe0 (diff)
downloadbusybox-w32-9edd268bad93128bcadfbdde28bb978a4b4c5bab.tar.gz
busybox-w32-9edd268bad93128bcadfbdde28bb978a4b4c5bab.tar.bz2
busybox-w32-9edd268bad93128bcadfbdde28bb978a4b4c5bab.zip
shell: implement optional "BASE#nnnn" numeric literals
function old new delta evaluate_string 729 851 +122 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/math.c')
-rw-r--r--shell/math.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/shell/math.c b/shell/math.c
index 611b3beab..2ea0317e9 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -513,6 +513,42 @@ static const char op_tokens[] ALIGN1 = {
513}; 513};
514#define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7]) 514#define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7])
515 515
516#if ENABLE_FEATURE_SH_MATH_BASE
517static arith_t strto_arith_t(const char *nptr, char **endptr)
518{
519 unsigned base;
520 arith_t n;
521
522# if ENABLE_FEATURE_SH_MATH_64
523 n = strtoull(nptr, endptr, 0);
524# else
525 n = strtoul(nptr, endptr, 0);
526# endif
527 if (**endptr != '#'
528 || (*nptr < '1' || *nptr > '9')
529 || (n < 2 || n > 64)
530 ) {
531 return n;
532 }
533
534 /* It's "N#nnnn" or "NN#nnnn" syntax, NN can't start with 0,
535 * NN is in 2..64 range.
536 */
537 base = (unsigned)n;
538 n = 0;
539 nptr = *endptr + 1;
540 /* bash allows "N#" (empty "nnnn" part) */
541 while (isdigit(*nptr)) {
542 /* bash does not check for overflows */
543 n = n * base + (*nptr++ - '0');
544 }
545 *endptr = (char*)nptr;
546 return n;
547}
548#define strto_arith_t(nptr, endptr, base_is_always_0) \
549 strto_arith_t(nptr, endptr)
550#endif
551
516static arith_t FAST_FUNC 552static arith_t FAST_FUNC
517evaluate_string(arith_state_t *math_state, const char *expr) 553evaluate_string(arith_state_t *math_state, const char *expr)
518{ 554{