aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-05-25 23:48:13 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-05-25 23:48:13 +0200
commit0c3270f7e55e0ca7b8d664851468511d2c8995cf (patch)
treefdaf00c6f2db56cab992eec0d556b3ef0db61381
parent6882a933cf078be35f4eb93963365549d43cb497 (diff)
downloadbusybox-w32-0c3270f7e55e0ca7b8d664851468511d2c8995cf.tar.gz
busybox-w32-0c3270f7e55e0ca7b8d664851468511d2c8995cf.tar.bz2
busybox-w32-0c3270f7e55e0ca7b8d664851468511d2c8995cf.zip
od: stop printing extra trailing spaces
function old new delta .rodata 104598 104613 +15 display 1475 1485 +10 od_main 549 556 +7 rewrite 971 967 -4 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/1 up/down: 32/-4) Total: 28 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/od.c33
-rw-r--r--include/dump.h1
-rw-r--r--libbb/dump.c21
-rwxr-xr-xtestsuite/od.tests43
4 files changed, 51 insertions, 47 deletions
diff --git a/coreutils/od.c b/coreutils/od.c
index bd82487e5..abedb40a7 100644
--- a/coreutils/od.c
+++ b/coreutils/od.c
@@ -157,22 +157,22 @@ odoffset(dumper_t *dumper, int argc, char ***argvp)
157// The format is required and must be surrounded by " "s. 157// The format is required and must be surrounded by " "s.
158// It is a printf-style format. 158// It is a printf-style format.
159static const char *const add_strings[] ALIGN_PTR = { 159static const char *const add_strings[] ALIGN_PTR = {
160 "16/1 \"%3_u \" \"\\n\"", /* 0: a */ 160 "16/1 \" %3_u\" \"\\n\"", /* 0: a */
161 "8/2 \"%06o \" \"\\n\"", /* 1: B (undocumented in od), o */ 161 "8/2 \" %06o\" \"\\n\"", /* 1: B (undocumented in od), o */
162 "16/1 \"%03o \" \"\\n\"", /* 2: b */ 162 "16/1 \" %03o\" \"\\n\"", /* 2: b */
163 "16/1 \"%3_c \" \"\\n\"", /* 3: c */ 163 "16/1 \" %3_c\" \"\\n\"", /* 3: c */
164 "8/2 \"%5u \" \"\\n\"", /* 4: d */ 164 "8/2 \" %5u\" \"\\n\"", /* 4: d */
165 "4/4 \"%10u \" \"\\n\"", /* 5: D */ 165 "4/4 \" %10u\" \"\\n\"", /* 5: D */
166 "2/8 \"%24.14e \" \"\\n\"", /* 6: e (undocumented in od), F */ 166 "2/8 \" %24.14e\" \"\\n\"", /* 6: e (undocumented in od), F */
167 "4/4 \"%15.7e \" \"\\n\"", /* 7: f */ 167 "4/4 \" %15.7e\" \"\\n\"", /* 7: f */
168 "4/4 \"%08x \" \"\\n\"", /* 8: H, X */ 168 "4/4 \" %08x\" \"\\n\"", /* 8: H, X */
169 "8/2 \"%04x \" \"\\n\"", /* 9: h, x */ 169 "8/2 \" %04x\" \"\\n\"", /* 9: h, x */
170 /* This probably also depends on word width of the arch (what is "long"?) */ 170 /* This probably also depends on word width of the arch (what is "long"?) */
171 /* should be "2/8" or "4/4" depending on sizeof(long)? */ 171 /* should be "2/8" or "4/4" depending on sizeof(long)? */
172 "2/8 \"%20lld \" \"\\n\"", /* 10: I, L, l */ 172 "2/8 \" %20lld\" \"\\n\"", /* 10: I, L, l */
173 "4/4 \"%11d \" \"\\n\"", /* 11: i */ 173 "4/4 \" %11d\" \"\\n\"", /* 11: i */
174 "4/4 \"%011o \" \"\\n\"", /* 12: O */ 174 "4/4 \" %011o\" \"\\n\"", /* 12: O */
175 "8/2 \"%6d \" \"\\n\"", /* 13: s */ 175 "8/2 \" %6d\" \"\\n\"", /* 13: s */
176}; 176};
177 177
178static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxsv"; 178static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxsv";
@@ -199,9 +199,9 @@ int od_main(int argc, char **argv)
199 if (first) { 199 if (first) {
200 first = 0; 200 first = 0;
201 bb_dump_add(dumper, "\"%07.7_Ao\n\""); 201 bb_dump_add(dumper, "\"%07.7_Ao\n\"");
202 bb_dump_add(dumper, "\"%07.7_ao \""); 202 bb_dump_add(dumper, "\"%07.7_ao\"");
203 } else { 203 } else {
204 bb_dump_add(dumper, "\" \""); 204 bb_dump_add(dumper, "\" \"");
205 } 205 }
206 bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]); 206 bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
207 } else { /* P, p, s, w, or other unhandled */ 207 } else { /* P, p, s, w, or other unhandled */
@@ -212,6 +212,7 @@ int od_main(int argc, char **argv)
212 bb_dump_add(dumper, "\"%07.7_Ao\n\""); 212 bb_dump_add(dumper, "\"%07.7_Ao\n\"");
213 bb_dump_add(dumper, "\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); 213 bb_dump_add(dumper, "\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
214 } 214 }
215 dumper->od_eofstring = "\n";
215 216
216 argc -= optind; 217 argc -= optind;
217 argv += optind; 218 argv += optind;
diff --git a/include/dump.h b/include/dump.h
index 10fc5d900..11dcf4523 100644
--- a/include/dump.h
+++ b/include/dump.h
@@ -34,6 +34,7 @@ typedef struct dumper_t {
34 smallint dump_vflag; /*enum dump_vflag_t*/ 34 smallint dump_vflag; /*enum dump_vflag_t*/
35 FS *fshead; 35 FS *fshead;
36 const char *xxd_eofstring; 36 const char *xxd_eofstring;
37 const char *od_eofstring;
37 off_t address; /* address/offset in stream */ 38 off_t address; /* address/offset in stream */
38 long long xxd_displayoff; 39 long long xxd_displayoff;
39} dumper_t; 40} dumper_t;
diff --git a/libbb/dump.c b/libbb/dump.c
index cfb9d94f9..77d76611b 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -242,7 +242,7 @@ static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
242 pr->flags = F_P; 242 pr->flags = F_P;
243 *p1 = 'c'; 243 *p1 = 'c';
244 goto DO_BYTE_COUNT_1; 244 goto DO_BYTE_COUNT_1;
245 case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */ 245 case 'u': /* %_u: chars, 'nul', 'esc' etc for nonprintable */
246 pr->flags = F_U; 246 pr->flags = F_U;
247 /* *p1 = 'c'; set in conv_u */ 247 /* *p1 = 'c'; set in conv_u */
248 goto DO_BYTE_COUNT_1; 248 goto DO_BYTE_COUNT_1;
@@ -322,8 +322,7 @@ static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
322 p2 = NULL; 322 p2 = NULL;
323 for (p1 = pr->fmt; *p1; ++p1) 323 for (p1 = pr->fmt; *p1; ++p1)
324 p2 = isspace(*p1) ? p1 : NULL; 324 p2 = isspace(*p1) ? p1 : NULL;
325 if (p2) 325 pr->nospace = p2;
326 pr->nospace = p2;
327 } 326 }
328 } 327 }
329} 328}
@@ -477,7 +476,7 @@ static void bpad(PR *pr)
477 476
478static const char conv_str[] ALIGN1 = 477static const char conv_str[] ALIGN1 =
479 "\0" "\\""0""\0" 478 "\0" "\\""0""\0"
480 "\007""\\""a""\0" /* \a */ 479 "\007""\\""a""\0"
481 "\b" "\\""b""\0" 480 "\b" "\\""b""\0"
482 "\f" "\\""f""\0" 481 "\f" "\\""f""\0"
483 "\n" "\\""n""\0" 482 "\n" "\\""n""\0"
@@ -539,7 +538,6 @@ static void conv_u(PR *pr, unsigned char *p)
539static NOINLINE void display(priv_dumper_t* dumper) 538static NOINLINE void display(priv_dumper_t* dumper)
540{ 539{
541 unsigned char *bp; 540 unsigned char *bp;
542 unsigned char savech = '\0';
543 541
544 while ((bp = get(dumper)) != NULL) { 542 while ((bp = get(dumper)) != NULL) {
545 FS *fs; 543 FS *fs;
@@ -560,6 +558,8 @@ static NOINLINE void display(priv_dumper_t* dumper)
560 PR *pr; 558 PR *pr;
561 for (pr = fu->nextpr; pr; dumper->pub.address += pr->bcnt, 559 for (pr = fu->nextpr; pr; dumper->pub.address += pr->bcnt,
562 bp += pr->bcnt, pr = pr->nextpr) { 560 bp += pr->bcnt, pr = pr->nextpr) {
561 unsigned char savech;
562
563 if (dumper->eaddress 563 if (dumper->eaddress
564 && dumper->pub.address >= dumper->eaddress 564 && dumper->pub.address >= dumper->eaddress
565 ) { 565 ) {
@@ -568,9 +568,16 @@ static NOINLINE void display(priv_dumper_t* dumper)
568 fputs_stdout(dumper->pub.xxd_eofstring); 568 fputs_stdout(dumper->pub.xxd_eofstring);
569 return; 569 return;
570 } 570 }
571 if (dumper->pub.od_eofstring) {
572 /* od support: requested to not pad incomplete blocks */
573 /* ... but do print final offset */
574 fputs_stdout(dumper->pub.od_eofstring);
575 goto endfu;
576 }
571 if (!(pr->flags & (F_TEXT | F_BPAD))) 577 if (!(pr->flags & (F_TEXT | F_BPAD)))
572 bpad(pr); 578 bpad(pr);
573 } 579 }
580 savech = '\0';
574 if (cnt == 1 && pr->nospace) { 581 if (cnt == 1 && pr->nospace) {
575 savech = *pr->nospace; 582 savech = *pr->nospace;
576 *pr->nospace = '\0'; 583 *pr->nospace = '\0';
@@ -665,7 +672,7 @@ static NOINLINE void display(priv_dumper_t* dumper)
665 break; 672 break;
666 } 673 }
667 } 674 }
668 if (cnt == 1 && pr->nospace) { 675 if (savech) {
669 *pr->nospace = savech; 676 *pr->nospace = savech;
670 } 677 }
671 } 678 }
@@ -673,7 +680,7 @@ static NOINLINE void display(priv_dumper_t* dumper)
673 } 680 }
674 } 681 }
675 } 682 }
676 683 endfu:
677 if (dumper->endfu) { 684 if (dumper->endfu) {
678 PR *pr; 685 PR *pr;
679 /* 686 /*
diff --git a/testsuite/od.tests b/testsuite/od.tests
index 29ca829d5..4b1525620 100755
--- a/testsuite/od.tests
+++ b/testsuite/od.tests
@@ -12,14 +12,9 @@ le=false
12{ printf '\0\1' | od -s | grep -q 256; } && le=true 12{ printf '\0\1' | od -s | grep -q 256; } && le=true
13readonly le 13readonly le
14 14
15# NB: for !DESKTOP,
16# sed 's/ *$//' truncates trailing spaces.
17# This needs to be fixed properly (by not outputting them).
18# For now, the tests ignore them (do not require a match).
19
20optional !DESKTOP 15optional !DESKTOP
21testing "od -a (!DESKTOP)" \ 16testing "od -a (!DESKTOP)" \
22 "od -a | sed 's/ *$//'" \ 17 "od -a" \
23"\ 18"\
240000000 soh stx etx lf A B C fe 190000000 soh stx etx lf A B C fe
250000010 200000010
@@ -39,7 +34,7 @@ testing "od -a (DESKTOP)" \
39SKIP= 34SKIP=
40 35
41testing "od -B" \ 36testing "od -B" \
42 "od -B | sed 's/ *$//'" \ 37 "od -B" \
43"\ 38"\
440000000 001001 005003 041101 177103 390000000 001001 005003 041101 177103
450000010 400000010
@@ -49,7 +44,7 @@ SKIP=
49 44
50$le || SKIP=1 45$le || SKIP=1
51testing "od -o (little-endian)" \ 46testing "od -o (little-endian)" \
52 "od -o | sed 's/ *$//'" \ 47 "od -o" \
53"\ 48"\
540000000 001001 005003 041101 177103 490000000 001001 005003 041101 177103
550000010 500000010
@@ -58,7 +53,7 @@ testing "od -o (little-endian)" \
58SKIP= 53SKIP=
59 54
60testing "od -b" \ 55testing "od -b" \
61 "od -b | sed 's/ *$//'" \ 56 "od -b" \
62"\ 57"\
630000000 001 002 003 012 101 102 103 376 580000000 001 002 003 012 101 102 103 376
640000010 590000010
@@ -67,7 +62,7 @@ testing "od -b" \
67SKIP= 62SKIP=
68 63
69testing "od -c" \ 64testing "od -c" \
70 "od -c | sed 's/ *$//'" \ 65 "od -c" \
71"\ 66"\
720000000 001 002 003 \\\\n A B C 376 670000000 001 002 003 \\\\n A B C 376
730000010 680000010
@@ -77,7 +72,7 @@ SKIP=
77 72
78$le || SKIP=1 73$le || SKIP=1
79testing "od -d (little-endian)" \ 74testing "od -d (little-endian)" \
80 "od -d | sed 's/ *$//'" \ 75 "od -d" \
81"\ 76"\
820000000 513 2563 16961 65091 770000000 513 2563 16961 65091
830000010 780000010
@@ -88,7 +83,7 @@ SKIP=
88optional !DESKTOP #DESKTOP: unrecognized option: D 83optional !DESKTOP #DESKTOP: unrecognized option: D
89$le || SKIP=1 84$le || SKIP=1
90testing "od -D (!DESKTOP little-endian)" \ 85testing "od -D (!DESKTOP little-endian)" \
91 "od -D | sed 's/ *$//'" \ 86 "od -D" \
92"\ 87"\
930000000 167969281 4265820737 880000000 167969281 4265820737
940000010 890000010
@@ -99,7 +94,7 @@ SKIP=
99optional !DESKTOP #DESKTOP: unrecognized option: e 94optional !DESKTOP #DESKTOP: unrecognized option: e
100$le || SKIP=1 95$le || SKIP=1
101testing "od -e (!DESKTOP little-endian)" \ 96testing "od -e (!DESKTOP little-endian)" \
102 "od -e | sed 's/ *$//'" \ 97 "od -e" \
103"\ 98"\
1040000000 -1.61218556514036e+300 990000000 -1.61218556514036e+300
1050000010 1000000010
@@ -110,7 +105,7 @@ SKIP=
110optional !DESKTOP #DESKTOP: unrecognized option: F 105optional !DESKTOP #DESKTOP: unrecognized option: F
111$le || SKIP=1 106$le || SKIP=1
112testing "od -F (!DESKTOP little-endian)" \ 107testing "od -F (!DESKTOP little-endian)" \
113 "od -F | sed 's/ *$//'" \ 108 "od -F" \
114"\ 109"\
1150000000 -1.61218556514036e+300 1100000000 -1.61218556514036e+300
1160000010 1110000010
@@ -120,7 +115,7 @@ SKIP=
120 115
121$le || SKIP=1 116$le || SKIP=1
122testing "od -f (little-endian)" \ 117testing "od -f (little-endian)" \
123 "od -f | sed 's/ *$//'" \ 118 "od -f" \
124"\ 119"\
1250000000 6.3077975e-33 -6.4885867e+37 1200000000 6.3077975e-33 -6.4885867e+37
1260000010 1210000010
@@ -131,7 +126,7 @@ SKIP=
131optional !DESKTOP #DESKTOP: unrecognized option: H 126optional !DESKTOP #DESKTOP: unrecognized option: H
132$le || SKIP=1 127$le || SKIP=1
133testing "od -H (!DESKTOP little-endian)" \ 128testing "od -H (!DESKTOP little-endian)" \
134 "od -H | sed 's/ *$//'" \ 129 "od -H" \
135"\ 130"\
1360000000 0a030201 fe434241 1310000000 0a030201 fe434241
1370000010 1320000010
@@ -142,7 +137,7 @@ SKIP=
142optional !DESKTOP #DESKTOP: unrecognized option: X 137optional !DESKTOP #DESKTOP: unrecognized option: X
143$le || SKIP=1 138$le || SKIP=1
144testing "od -X (!DESKTOP little-endian)" \ 139testing "od -X (!DESKTOP little-endian)" \
145 "od -X | sed 's/ *$//'" \ 140 "od -X" \
146"\ 141"\
1470000000 0a030201 fe434241 1420000000 0a030201 fe434241
1480000010 1430000010
@@ -152,7 +147,7 @@ SKIP=
152 147
153$le || SKIP=1 148$le || SKIP=1
154testing "od -h (little-endian)" \ 149testing "od -h (little-endian)" \
155 "od -h | sed 's/ *$//'" \ 150 "od -h" \
156"\ 151"\
1570000000 0201 0a03 4241 fe43 1520000000 0201 0a03 4241 fe43
1580000010 1530000010
@@ -162,7 +157,7 @@ SKIP=
162 157
163$le || SKIP=1 158$le || SKIP=1
164testing "od -x (little-endian)" \ 159testing "od -x (little-endian)" \
165 "od -x | sed 's/ *$//'" \ 160 "od -x" \
166"\ 161"\
1670000000 0201 0a03 4241 fe43 1620000000 0201 0a03 4241 fe43
1680000010 1630000010
@@ -173,7 +168,7 @@ SKIP=
173optional !DESKTOP #DESKTOP: unrecognized option: I 168optional !DESKTOP #DESKTOP: unrecognized option: I
174$le || SKIP=1 169$le || SKIP=1
175testing "od -I (!DESKTOP little-endian)" \ 170testing "od -I (!DESKTOP little-endian)" \
176 "od -I | sed 's/ *$//'" \ 171 "od -I" \
177"\ 172"\
1780000000 -125183517527965183 1730000000 -125183517527965183
1790000010 1740000010
@@ -184,7 +179,7 @@ SKIP=
184optional !DESKTOP #DESKTOP: unrecognized option: L 179optional !DESKTOP #DESKTOP: unrecognized option: L
185$le || SKIP=1 180$le || SKIP=1
186testing "od -L (!DESKTOP little-endian)" \ 181testing "od -L (!DESKTOP little-endian)" \
187 "od -L | sed 's/ *$//'" \ 182 "od -L" \
188"\ 183"\
1890000000 -125183517527965183 1840000000 -125183517527965183
1900000010 1850000010
@@ -194,7 +189,7 @@ SKIP=
194 189
195$le || SKIP=1 190$le || SKIP=1
196testing "od -i (little-endian)" \ 191testing "od -i (little-endian)" \
197 "od -i | sed 's/ *$//'" \ 192 "od -i" \
198"\ 193"\
1990000000 167969281 -29146559 1940000000 167969281 -29146559
2000000010 1950000010
@@ -205,7 +200,7 @@ SKIP=
205optional !DESKTOP #DESKTOP: unrecognized option: O 200optional !DESKTOP #DESKTOP: unrecognized option: O
206$le || SKIP=1 201$le || SKIP=1
207testing "od -O (!DESKTOP little-endian)" \ 202testing "od -O (!DESKTOP little-endian)" \
208 "od -O | sed 's/ *$//'" \ 203 "od -O" \
209"\ 204"\
2100000000 01200601001 37620641101 2050000000 01200601001 37620641101
2110000010 2060000010
@@ -216,7 +211,7 @@ SKIP=
216# This probably also depends on word width of the arch (what is "long"?) 211# This probably also depends on word width of the arch (what is "long"?)
217$le || SKIP=1 212$le || SKIP=1
218testing "od -l (little-endian)" \ 213testing "od -l (little-endian)" \
219 "od -l | sed 's/ *$//'" \ 214 "od -l" \
220"\ 215"\
2210000000 -125183517527965183 2160000000 -125183517527965183
2220000010 2170000010