aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-10-30 23:25:50 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-10-30 23:25:50 +0000
commit6b38e18247882d90de5053e76a4b3c6a3914e574 (patch)
tree0fc5f46522ee8a890ed329d31edf348de80accf9
parent470dc1d7e20a079e99065e7bf55d88a07066f949 (diff)
downloadbusybox-w32-6b38e18247882d90de5053e76a4b3c6a3914e574.tar.gz
busybox-w32-6b38e18247882d90de5053e76a4b3c6a3914e574.tar.bz2
busybox-w32-6b38e18247882d90de5053e76a4b3c6a3914e574.zip
dc: support for bases 2 and 8, by Nate Case (ncase AT xes-inc.com)
function old new delta print_base 87 176 +89 set_output_base 81 95 +14 static.bases - 5 +5 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/0 up/down: 108/0) Total: 108 bytes
-rw-r--r--miscutils/dc.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/miscutils/dc.c b/miscutils/dc.c
index 7d5886eb2..6d4efa943 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -98,19 +98,45 @@ static void not(void)
98 98
99static void set_output_base(void) 99static void set_output_base(void)
100{ 100{
101 base = (unsigned)pop(); 101 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
102 if ((base != 10) && (base != 16)) { 102 unsigned b = (unsigned)pop();
103 bb_error_msg("error, base %d is not supported", base); 103
104 base = *strchrnul(bases, b);
105 if (base == 0) {
106 bb_error_msg("error, base %u is not supported", b);
104 base = 10; 107 base = 10;
105 } 108 }
106} 109}
107 110
108static void print_base(double print) 111static void print_base(double print)
109{ 112{
110 if (base == 16) 113 unsigned x, i;
111 printf("%x\n", (unsigned)print); 114
112 else 115 if (base == 10) {
113 printf("%g\n", print); 116 printf("%g\n", print);
117 return;
118 }
119
120 x = (unsigned)print;
121 switch (base) {
122 case 16:
123 printf("%x\n", x);
124 break;
125 case 8:
126 printf("%o\n", x);
127 break;
128 default: /* base 2 */
129 i = (unsigned)INT_MAX + 1;
130 do {
131 if (x & i) break;
132 i >>= 1;
133 } while (i > 1);
134 do {
135 bb_putchar('1' - !(x & i));
136 i >>= 1;
137 } while (i);
138 bb_putchar('\n');
139 }
114} 140}
115 141
116static void print_stack_no_pop(void) 142static void print_stack_no_pop(void)