aboutsummaryrefslogtreecommitdiff
path: root/miscutils/dc.c
diff options
context:
space:
mode:
Diffstat (limited to 'miscutils/dc.c')
-rw-r--r--miscutils/dc.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/miscutils/dc.c b/miscutils/dc.c
index 0d09f5e2b..c7ce2be0b 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -35,10 +35,12 @@ enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(
35 base = 10; \ 35 base = 10; \
36} while (0) 36} while (0)
37 37
38static void check_under(void) 38static unsigned check_under(void)
39{ 39{
40 if (pointer == 0) 40 unsigned p = pointer;
41 if (p == 0)
41 bb_error_msg_and_die("stack underflow"); 42 bb_error_msg_and_die("stack underflow");
43 return p - 1;
42} 44}
43 45
44static void push(double a) 46static void push(double a)
@@ -50,8 +52,9 @@ static void push(double a)
50 52
51static double pop(void) 53static double pop(void)
52{ 54{
53 check_under(); 55 unsigned p = check_under();
54 return stack[--pointer]; 56 pointer = p;
57 return stack[p];
55} 58}
56 59
57static void add(void) 60static void add(void)
@@ -91,6 +94,19 @@ static void mod(void)
91{ 94{
92 data_t d = pop(); 95 data_t d = pop();
93 96
97 /* compat with dc (GNU bc 1.07.1) 1.4.1:
98 * $ dc -e '4 0 % p'
99 * dc: remainder by zero
100 * 0
101 */
102 if (d == 0) {
103 bb_error_msg("remainder by zero");
104 pop();
105 push(0);
106 return;
107 }
108 /* ^^^^ without this, we simply get SIGFPE and die */
109
94 push((data_t) pop() % d); 110 push((data_t) pop() % d);
95} 111}
96 112
@@ -171,8 +187,7 @@ static void print_stack_no_pop(void)
171 187
172static void print_no_pop(void) 188static void print_no_pop(void)
173{ 189{
174 check_under(); 190 print_base(stack[check_under()]);
175 print_base(stack[pointer-1]);
176} 191}
177 192
178struct op { 193struct op {