aboutsummaryrefslogtreecommitdiff
path: root/shell/math.c
diff options
context:
space:
mode:
Diffstat (limited to 'shell/math.c')
-rw-r--r--shell/math.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/shell/math.c b/shell/math.c
index 611b3beab..eaf4f2453 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -513,6 +513,46 @@ 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#else /* !ENABLE_FEATURE_SH_MATH_BASE */
549# if ENABLE_FEATURE_SH_MATH_64
550# define strto_arith_t(nptr, endptr) strtoull(nptr, endptr, 0)
551# else
552# define strto_arith_t(nptr, endptr) strtoul(nptr, endptr, 0)
553# endif
554#endif
555
516static arith_t FAST_FUNC 556static arith_t FAST_FUNC
517evaluate_string(arith_state_t *math_state, const char *expr) 557evaluate_string(arith_state_t *math_state, const char *expr)
518{ 558{
@@ -591,7 +631,7 @@ evaluate_string(arith_state_t *math_state, const char *expr)
591 /* Number */ 631 /* Number */
592 numstackptr->var = NULL; 632 numstackptr->var = NULL;
593 errno = 0; 633 errno = 0;
594 numstackptr->val = strto_arith_t(expr, (char**) &expr, 0); 634 numstackptr->val = strto_arith_t(expr, (char**) &expr);
595 if (errno) 635 if (errno)
596 numstackptr->val = 0; /* bash compat */ 636 numstackptr->val = 0; /* bash compat */
597 goto num; 637 goto num;