[git commit] od: fix default format, shrink

Denys Vlasenko vda.linux at googlemail.com
Fri May 26 02:27:43 UTC 2023


commit: https://git.busybox.net/busybox/commit/?id=aa4d303a3139107919f73cece4eaf85a7dc75db6
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
od_main                                              556     568     +12
.rodata                                           104613  104555     -58
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 12/-58)            Total: -46 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/od.c     | 42 ++++++++++++++++++++++--------------------
 testsuite/od.tests | 10 ++++++++++
 2 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/coreutils/od.c b/coreutils/od.c
index abedb40a7..3fd241d51 100644
--- a/coreutils/od.c
+++ b/coreutils/od.c
@@ -144,35 +144,36 @@ odoffset(dumper_t *dumper, int argc, char ***argvp)
 	}
 }
 
-// A format string contains format units separated by whitespace.
+// bb_dump_add():
+// A format string contains format units separated by [optional] whitespace.
 // A format unit contains up to three items: an iteration count, a byte count,
 // and a format.
-// The iteration count is an optional integer (default 1)
+// The iteration count is an optional integer (default 1).
 // Each format is applied iteration count times.
 // The byte count is an optional integer. It defines the number
 // of bytes to be interpreted by each iteration of the format.
 // If an iteration count and/or a byte count is specified, a slash must be
 // placed after the iteration count and/or before the byte count
 // to disambiguate them.
-// The format is required and must be surrounded by " "s.
-// It is a printf-style format.
+// The printf-style format is required and must be surrounded by " "s.
+// (Below, each string contains two format units)
 static const char *const add_strings[] ALIGN_PTR = {
-	"16/1 \" %3_u\" \"\\n\"",              /* 0: a */
-	"8/2 \" %06o\" \"\\n\"",               /* 1: B (undocumented in od), o */
-	"16/1 \" %03o\" \"\\n\"",              /* 2: b */
-	"16/1 \" %3_c\" \"\\n\"",              /* 3: c */
-	"8/2 \" %5u\" \"\\n\"",                /* 4: d */
-	"4/4 \" %10u\" \"\\n\"",               /* 5: D */
-	"2/8 \" %24.14e\" \"\\n\"",            /* 6: e (undocumented in od), F */
-	"4/4 \" %15.7e\" \"\\n\"",             /* 7: f */
-	"4/4 \" %08x\" \"\\n\"",               /* 8: H, X */
-	"8/2 \" %04x\" \"\\n\"",               /* 9: h, x */
+	"16/1 \" %3_u\""   "\"\n\"",            /* 0: a */
+	"8/2 \" %06o\""    "\"\n\"",            /* 1: B (undocumented in od), o */
+	"16/1 \" %03o\""   "\"\n\"",            /* 2: b */
+	"16/1 \" %3_c\""   "\"\n\"",            /* 3: c */
+	"8/2 \" %5u\""     "\"\n\"",            /* 4: d */
+	"4/4 \" %10u\""    "\"\n\"",            /* 5: D */
+	"2/8 \" %24.14e\"" "\"\n\"",            /* 6: e (undocumented in od), F */
+	"4/4 \" %15.7e\""  "\"\n\"",            /* 7: f */
+	"4/4 \" %08x\""    "\"\n\"",            /* 8: H, X */
+	"8/2 \" %04x\""    "\"\n\"",            /* 9: h, x */
 	/* This probably also depends on word width of the arch (what is "long"?) */
 	/* should be "2/8" or "4/4" depending on sizeof(long)? */
-	"2/8 \" %20lld\" \"\\n\"",             /* 10: I, L, l */
-	"4/4 \" %11d\" \"\\n\"",               /* 11: i */
-	"4/4 \" %011o\" \"\\n\"",              /* 12: O */
-	"8/2 \" %6d\" \"\\n\"",                /* 13: s */
+	"2/8 \" %20lld\""  "\"\n\"",            /* 10: I, L, l */
+	"4/4 \" %11d\""    "\"\n\"",            /* 11: i */
+	"4/4 \" %011o\""   "\"\n\"",            /* 12: O */
+	"8/2 \" %6d\""     "\"\n\"",            /* 13: s */
 };
 
 static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxsv";
@@ -204,13 +205,14 @@ int od_main(int argc, char **argv)
 				bb_dump_add(dumper, "\"       \"");
 			}
 			bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
-		} else {  /* P, p, s, w, or other unhandled */
+		} else {  /* P, p, w, or other unhandled */
 			bb_show_usage();
 		}
 	}
 	if (!dumper->fshead) {
 		bb_dump_add(dumper, "\"%07.7_Ao\n\"");
-		bb_dump_add(dumper, "\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
+		bb_dump_add(dumper, "\"%07.7_ao\"");
+		bb_dump_add(dumper, add_strings[1]); /* -o format is default */
 	}
 	dumper->od_eofstring = "\n";
 
diff --git a/testsuite/od.tests b/testsuite/od.tests
index 4b1525620..d6f50a206 100755
--- a/testsuite/od.tests
+++ b/testsuite/od.tests
@@ -12,6 +12,16 @@ le=false
 { printf '\0\1' | od -s | grep -q 256; } && le=true
 readonly le
 
+$le || SKIP=1
+testing "od (little-endian)" \
+        "od" \
+"\
+0000000 001001 005003 041101 177103
+0000010
+" \
+	"" "$input"
+SKIP=
+
 optional !DESKTOP
 testing "od -a (!DESKTOP)" \
         "od -a" \


More information about the busybox-cvs mailing list