aboutsummaryrefslogtreecommitdiff
path: root/miscutils/dc.c
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 /miscutils/dc.c
parent470dc1d7e20a079e99065e7bf55d88a07066f949 (diff)
downloadbusybox-6b38e18247882d90de5053e76a4b3c6a3914e574.tar.gz
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
Diffstat (limited to 'miscutils/dc.c')
-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)
static void set_output_base(void)
{
- base = (unsigned)pop();
- if ((base != 10) && (base != 16)) {
- bb_error_msg("error, base %d is not supported", base);
+ static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
+ unsigned b = (unsigned)pop();
+
+ base = *strchrnul(bases, b);
+ if (base == 0) {
+ bb_error_msg("error, base %u is not supported", b);
base = 10;
}
}
static void print_base(double print)
{
- if (base == 16)
- printf("%x\n", (unsigned)print);
- else
+ unsigned x, i;
+
+ if (base == 10) {
printf("%g\n", print);
+ return;
+ }
+
+ x = (unsigned)print;
+ switch (base) {
+ case 16:
+ printf("%x\n", x);
+ break;
+ case 8:
+ printf("%o\n", x);
+ break;
+ default: /* base 2 */
+ i = (unsigned)INT_MAX + 1;
+ do {
+ if (x & i) break;
+ i >>= 1;
+ } while (i > 1);
+ do {
+ bb_putchar('1' - !(x & i));
+ i >>= 1;
+ } while (i);
+ bb_putchar('\n');
+ }
}
static void print_stack_no_pop(void)