Add support for base 2 as an output base in dc, so that you can
do things like 'echo "1234 2 o p" | dc' to convert to binary.
Signed-off-by: Nate Case <[EMAIL PROTECTED]>
---
miscutils/Config.in | 7 +++++++
miscutils/dc.c | 31 +++++++++++++++++++++++++++++--
2 files changed, 36 insertions(+), 2 deletions(-)
'make bloatcheck' with CONFIG_ENABLE_FEATURE_DC_BASE2=n, where
baseline is unpatched:
function old new delta
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0) Total: 0 bytes
text data bss dec hex filename
382228 1898 8916 393042 5ff52 busybox_old
382228 1898 8916 393042 5ff52 busybox_unstripped
'make bloatcheck' with CONFIG_ENABLE_FEATURE_DC_BASE2=y, where
baseline is unpatched:
function old new delta
print_base 87 222 +135
set_output_base 81 86 +5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 140/0) Total: 140 bytes
text data bss dec hex filename
382228 1898 8916 393042 5ff52 busybox_old
382368 1898 8916 393182 5ffde busybox_unstripped
Index: miscutils/dc.c
===================================================================
--- miscutils/dc.c (revision 23861)
+++ miscutils/dc.c (working copy)
@@ -99,7 +99,11 @@
static void set_output_base(void)
{
base = (unsigned)pop();
+#if ENABLE_FEATURE_DC_BASE2
+ if ((base != 10) && (base != 16) && (base != 2)) {
+#else
if ((base != 10) && (base != 16)) {
+#endif
bb_error_msg("error, base %d is not supported", base);
base = 10;
}
@@ -107,10 +111,33 @@
static void print_base(double print)
{
- if (base == 16)
+ switch (base) {
+ case 16:
printf("%x\n", (unsigned)print);
- else
+ break;
+#if ENABLE_FEATURE_DC_BASE2
+ case 2:
+ {
+ int i, foundone=0;
+ unsigned x = (unsigned)print;
+ for (i = sizeof(unsigned) * 8 - 1; i >= 0; i--) {
+ if (x & (1 << i)) {
+ foundone = 1;
+ printf("1");
+ } else {
+ if (foundone)
+ printf("0");
+ }
+ }
+ if (!foundone)
+ printf("0");
+ printf("\n");
+ }
+ break;
+#endif
+ default:
printf("%g\n", print);
+ }
}
static void print_stack_no_pop(void)
Index: miscutils/Config.in
===================================================================
--- miscutils/Config.in (revision 23861)
+++ miscutils/Config.in (working copy)
@@ -146,6 +146,13 @@
Enable power and exp functions.
NOTE: This will require libm to be present for linking.
+config FEATURE_DC_BASE2
+ bool "Enable base 2 support"
+ default n
+ depends on DC
+ help
+ Enable binary (base 2) as an output base in dc.
+
config DEVFSD
bool "devfsd (obsolete)"
default n
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox