bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: od -t b?


From: Daniel Janus
Subject: Re: od -t b?
Date: Fri, 3 Jun 2005 07:22:36 +0200 (CEST)


Paul Eggert wrote:

Daniel Janus <address@hidden> writes:

My question: is it just an overlooking, or is the lack of this feature
deliberate?

I think it'd be a reasonable feature to add, if someone took the time
to compose a complete patch for it.  (Documentation is often the
hardest part.)


I've toyed a bit with the source of od, and the following patch is what I came up with. It should be complete -- the changes are documented in Texinfo, --help and ChangeLog (albeit not in .po's -- I'm not much of a polyglot :-))

The patch works for me on Slackware 10.1. Feedback and tests would be greatly appreciated.

Sincerely,
Daniel Janus

----- CUT HERE -----

Index: ChangeLog
===================================================================
RCS file: /cvsroot/coreutils/coreutils/ChangeLog,v
retrieving revision 1.1314
diff -u -r1.1314 ChangeLog
--- ChangeLog   2 Jun 2005 10:04:32 -0000       1.1314
+++ ChangeLog   3 Jun 2005 05:13:49 -0000
@@ -1,3 +1,12 @@
+2005-06-03  Daniel Janus  <address@hidden>
+
+       * NEWS: od now supports binary dumping with "-t b".
+       * src/od.c (print_binary_char, print_binary_short, print_binary_int,
+       print_binary_long, print_binary_long_long): New functions.
+       (decode_one_format): Support for "-t b".
+       (usage): Modified to reflect the new feature.
+       * doc/coreutils.texi (od invocation): Documented.
+
 2005-06-02  Jim Meyering  <address@hidden>

        * Version 5.3.1.
Index: doc/coreutils.texi
===================================================================
RCS file: /cvsroot/coreutils/coreutils/doc/coreutils.texi,v
retrieving revision 1.259
diff -u -r1.259 coreutils.texi
--- doc/coreutils.texi  2 Jun 2005 05:00:24 -0000       1.259
+++ doc/coreutils.texi  3 Jun 2005 05:14:27 -0000
@@ -1643,6 +1643,8 @@
 @table @samp
 @item a
 named character
address@hidden b
+binary
 @item c
 @acronym{ASCII} character or backslash escape,
 @item d
@@ -1667,8 +1669,8 @@
 by following the type indicator character with a decimal integer.
 Alternately, you can specify the size of one of the C compiler's
 built-in data types by following the type indicator character with
-one of the following characters.  For integers (@samp{d}, @samp{o},
address@hidden, @samp{x}):
+one of the following characters. For integers (@samp{b}, @samp{d}, address@hidden, @samp{u}, @samp{x}):

 @table @samp
 @item C
Index: src/od.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/src/od.c,v
retrieving revision 1.157
diff -u -r1.157 od.c
--- src/od.c    14 May 2005 07:58:37 -0000      1.157
+++ src/od.c    3 Jun 2005 05:14:34 -0000
@@ -90,6 +90,7 @@
     UNSIGNED_DECIMAL,
     OCTAL,
     HEXADECIMAL,
+    BINARY,
     FLOATING_POINT,
     NAMED_CHARACTER,
     CHARACTER
@@ -140,6 +141,9 @@
 static char const bytes_to_signed_dec_digits[] =
 {1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};

+static char const bytes_to_bits[] =
+{0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128};
+
 static char const bytes_to_unsigned_dec_digits[] =
 {0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};

@@ -355,6 +359,7 @@
   c          ASCII character or backslash escape\n\
 "), stdout);
       fputs (_("\
+  b[SIZE]    binary, SIZE bytes per integer\n\
   d[SIZE]    signed decimal, SIZE bytes per integer\n\
   f[SIZE]    floating point, SIZE bytes per integer\n\
   o[SIZE]    octal, SIZE bytes per integer\n\
@@ -494,6 +499,81 @@
 }

 static void
+print_binary_char (size_t n_bytes, void const *block,
+                   const char *unused_fmt_string ATTRIBUTE_UNUSED)
+{
+  unsigned char const *p = block;
+  size_t i;
+  int j;
+  for (i = n_bytes / sizeof *p; i != 0; i--) {
+    putchar (' ');
+ for (j = sizeof *p * CHAR_BIT - 1; j >= 0; j--) + putchar (*p & (1 << j) ? '1' : '0');
+    p++;
+  }
+}
+
+static void
+print_binary_short (size_t n_bytes, void const *block,
+                    const char *unused_fmt_string ATTRIBUTE_UNUSED)
+{
+  unsigned short const *p = block;
+  size_t i;
+  int j;
+  for (i = n_bytes / sizeof *p; i != 0; i--) {
+    putchar (' ');
+ for (j = sizeof *p * CHAR_BIT - 1; j >= 0; j--) + putchar (*p & (1 << j) ? '1' : '0');
+    p++;
+  }
+}
+
+static void
+print_binary_int (size_t n_bytes, void const *block,
+                  const char *unused_fmt_string ATTRIBUTE_UNUSED)
+{
+  unsigned int const *p = block;
+  size_t i;
+  int j;
+  for (i = n_bytes / sizeof *p; i != 0; i--) {
+    putchar (' ');
+ for (j = sizeof *p * CHAR_BIT - 1; j >= 0; j--) + putchar (*p & (1 << j) ? '1' : '0');
+    p++;
+  }
+}
+
+static void
+print_binary_long (size_t n_bytes, void const *block,
+                   const char *unused_fmt_string ATTRIBUTE_UNUSED)
+{
+  unsigned long const *p = block;
+  size_t i;
+  int j;
+  for (i = n_bytes / sizeof *p; i != 0; i--) {
+    putchar (' ');
+ for (j = sizeof *p * CHAR_BIT - 1; j >= 0; j--) + putchar (*p & (1UL << j) ? '1' : '0');
+    p++;
+  }
+}
+
+static void
+print_binary_long_long (size_t n_bytes, void const *block,
+                        const char *unused_fmt_string ATTRIBUTE_UNUSED)
+{
+  unsigned long long const *p = block;
+  size_t i;
+  int j;
+  for (i = n_bytes / sizeof *p; i != 0; i--) {
+    putchar (' ');
+ for (j = sizeof *p * CHAR_BIT - 1; j >= 0; j--) + putchar (*p & (1ULL << j) ? '1' : '0');
+    p++;
+  }
+}
+
+static void
 print_named_ascii (size_t n_bytes, void const *block,
                   const char *unused_fmt_string ATTRIBUTE_UNUSED)
 {
@@ -634,6 +714,7 @@

   switch (*s)
     {
+    case 'b':
     case 'd':
     case 'o':
     case 'u':
@@ -696,6 +777,11 @@

       switch (c)
        {
+        case 'b':
+          fmt = BINARY;
+          field_width = bytes_to_bits[size];
+          break;
+
        case 'd':
          fmt = SIGNED_DECIMAL;
          sprintf (tspec->fmt_string, " %%%d%s",
@@ -735,25 +821,35 @@
        case CHAR:
          print_function = (fmt == SIGNED_DECIMAL
                            ? print_s_char
-                           : print_char);
+                           : (fmt == BINARY
+ ? print_binary_char + : print_char));
          break;

        case SHORT:
          print_function = (fmt == SIGNED_DECIMAL
                            ? print_s_short
-                           : print_short);
+                           : (fmt == BINARY
+                               ? print_binary_short
+                               : print_short));
          break;

        case INT:
-         print_function = print_int;
+         print_function = (fmt == BINARY
+                            ? print_binary_int
+                            : print_int);
          break;

        case LONG:
-         print_function = print_long;
+         print_function = (fmt == BINARY
+                            ? print_binary_long
+                            : print_long);
          break;

        case LONG_LONG:
-         print_function = print_long_long;
+         print_function = (fmt == BINARY
+                            ? print_binary_long_long
+                            : print_long_long);
          break;

        default:




reply via email to

[Prev in Thread] Current Thread [Next in Thread]