aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-12-12 10:31:53 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-12-12 10:31:53 +0000
commit6d07432b2ff21f0d8537ba5fae3c402be9cb247a (patch)
tree21111cbc0beb96ef6bbb1dfdf51c52bcdd0bed52 /miscutils
parentb8dff0c2a2d2d53e6f0888568305dc17e990cddc (diff)
downloadbusybox-6d07432b2ff21f0d8537ba5fae3c402be9cb247a.tar.gz
Support the o, f and p options, patch by Magnus M�rtensson
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/dc.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/miscutils/dc.c b/miscutils/dc.c
index f9020b360..c7b43ea0a 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -11,6 +11,7 @@
static double stack[100];
static unsigned int pointer;
+static unsigned char base;
static void push(double a)
{
@@ -70,9 +71,38 @@ static void not(void)
push(~(unsigned int) pop());
}
+static void set_output_base(void)
+{
+ base=(unsigned char)pop();
+ if ((base != 10) && (base != 16)) {
+ fprintf(stderr, "Error: base = %d is not supported.\n", base);
+ base=10;
+ }
+}
+
+static void print_base(double print)
+{
+ if (base == 16)
+ printf("%x\n", (unsigned int)print);
+ else
+ printf("%g\n", print);
+}
+
+static void print_stack_no_pop(void)
+{
+ unsigned int i=pointer;
+ while (i)
+ print_base(stack[--i]);
+}
+
+static void print_no_pop(void)
+{
+ print_base(stack[pointer-1]);
+}
+
static void print(void)
{
- printf("%g\n", pop());
+ print_base(pop());
}
struct op {
@@ -93,6 +123,9 @@ static const struct op operators[] = {
{"or", or},
{"not", not},
{"eor", eor},
+ {"p", print_no_pop},
+ {"f", print_stack_no_pop},
+ {"o", set_output_base},
{0, 0}
};