diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-10-22 11:24:39 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-10-22 11:24:39 +0000 |
commit | a92877403ac522d17602ae24782b44a99a268539 (patch) | |
tree | b0c182fed4a45640f50a725d8e248aad7effb4b8 | |
parent | a48b0a3af71958c1cea6389893371664a47b1a39 (diff) | |
download | busybox-w32-a92877403ac522d17602ae24782b44a99a268539.tar.gz busybox-w32-a92877403ac522d17602ae24782b44a99a268539.tar.bz2 busybox-w32-a92877403ac522d17602ae24782b44a99a268539.zip |
Goetz Bock writes:
Dear list,
during my quest do pack busybox into an RPM, I've fixed a small bug
(missing \n) in dc's usage. And added two additional operations: mod and
exp/power.
Feel free to drop them.
-rw-r--r-- | include/usage.h | 6 | ||||
-rw-r--r-- | miscutils/dc.c | 20 |
2 files changed, 24 insertions, 2 deletions
diff --git a/include/usage.h b/include/usage.h index 851023ea5..268c23cac 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -280,8 +280,10 @@ | |||
280 | "expression ..." | 280 | "expression ..." |
281 | #define dc_full_usage \ | 281 | #define dc_full_usage \ |
282 | "This is a Tiny RPN calculator that understands the\n" \ | 282 | "This is a Tiny RPN calculator that understands the\n" \ |
283 | "following operations: +, -, /, *, and, or, not, eor.\n" \ | 283 | "following operations: +, add, -, sub, *, mul, /, div, %, mod, "\ |
284 | "i.e., 'dc 2 2 add' -> 4, and 'dc 8 8 \\* 2 2 + /' -> 16" \ | 284 | "**, exp, and, or, not, eor.\n" \ |
285 | "For example: 'dc 2 2 add' -> 4, and 'dc 8 8 \\* 2 2 + /' -> 16.\n" \ | ||
286 | "\nOptions:\n" \ | ||
285 | "p - Prints the value on the top of the stack, without altering the stack.\n" \ | 287 | "p - Prints the value on the top of the stack, without altering the stack.\n" \ |
286 | "f - Prints the entire contents of the stack without altering anything.\n" \ | 288 | "f - Prints the entire contents of the stack without altering anything.\n" \ |
287 | "o - Pops the value off the top of the stack and uses it to set the output radix.\n" \ | 289 | "o - Pops the value off the top of the stack and uses it to set the output radix.\n" \ |
diff --git a/miscutils/dc.c b/miscutils/dc.c index 451423c62..f574ae4a0 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c | |||
@@ -44,6 +44,13 @@ static void mul(void) | |||
44 | push(pop() * pop()); | 44 | push(pop() * pop()); |
45 | } | 45 | } |
46 | 46 | ||
47 | static void power(void) | ||
48 | { | ||
49 | double topower = pop(); | ||
50 | |||
51 | push(pow(pop(), topower)); | ||
52 | } | ||
53 | |||
47 | static void divide(void) | 54 | static void divide(void) |
48 | { | 55 | { |
49 | double divisor = pop(); | 56 | double divisor = pop(); |
@@ -51,6 +58,13 @@ static void divide(void) | |||
51 | push(pop() / divisor); | 58 | push(pop() / divisor); |
52 | } | 59 | } |
53 | 60 | ||
61 | static void mod(void) | ||
62 | { | ||
63 | unsigned int d = pop(); | ||
64 | |||
65 | push((unsigned int) pop() % d); | ||
66 | } | ||
67 | |||
54 | static void and(void) | 68 | static void and(void) |
55 | { | 69 | { |
56 | push((unsigned int) pop() & (unsigned int) pop()); | 70 | push((unsigned int) pop() & (unsigned int) pop()); |
@@ -119,10 +133,16 @@ static const struct op operators[] = { | |||
119 | {"mul", mul}, | 133 | {"mul", mul}, |
120 | {"/", divide}, | 134 | {"/", divide}, |
121 | {"div", divide}, | 135 | {"div", divide}, |
136 | {"**", power}, | ||
137 | {"exp", power}, | ||
138 | {"pow", power}, | ||
139 | {"%", mod}, | ||
140 | {"mod", mod}, | ||
122 | {"and", and}, | 141 | {"and", and}, |
123 | {"or", or}, | 142 | {"or", or}, |
124 | {"not", not}, | 143 | {"not", not}, |
125 | {"eor", eor}, | 144 | {"eor", eor}, |
145 | {"xor", eor}, | ||
126 | {"p", print_no_pop}, | 146 | {"p", print_no_pop}, |
127 | {"f", print_stack_no_pop}, | 147 | {"f", print_stack_no_pop}, |
128 | {"o", set_output_base}, | 148 | {"o", set_output_base}, |