aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
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 @@
11 11
12static double stack[100]; 12static double stack[100];
13static unsigned int pointer; 13static unsigned int pointer;
14static unsigned char base;
14 15
15static void push(double a) 16static void push(double a)
16{ 17{
@@ -70,9 +71,38 @@ static void not(void)
70 push(~(unsigned int) pop()); 71 push(~(unsigned int) pop());
71} 72}
72 73
74static void set_output_base(void)
75{
76 base=(unsigned char)pop();
77 if ((base != 10) && (base != 16)) {
78 fprintf(stderr, "Error: base = %d is not supported.\n", base);
79 base=10;
80 }
81}
82
83static void print_base(double print)
84{
85 if (base == 16)
86 printf("%x\n", (unsigned int)print);
87 else
88 printf("%g\n", print);
89}
90
91static void print_stack_no_pop(void)
92{
93 unsigned int i=pointer;
94 while (i)
95 print_base(stack[--i]);
96}
97
98static void print_no_pop(void)
99{
100 print_base(stack[pointer-1]);
101}
102
73static void print(void) 103static void print(void)
74{ 104{
75 printf("%g\n", pop()); 105 print_base(pop());
76} 106}
77 107
78struct op { 108struct op {
@@ -93,6 +123,9 @@ static const struct op operators[] = {
93 {"or", or}, 123 {"or", or},
94 {"not", not}, 124 {"not", not},
95 {"eor", eor}, 125 {"eor", eor},
126 {"p", print_no_pop},
127 {"f", print_stack_no_pop},
128 {"o", set_output_base},
96 {0, 0} 129 {0, 0}
97}; 130};
98 131