Skip to content

Commit c3cd548

Browse files
authored
od: support more output formats with -t
* Add other integer format options (u1,u2,u4,d1,d2,d4,o1,o2,o4) as well as the aliases for options -a and -c * 8-byte integer formats could be added in future (these are supported by GNU version) * Make more of an effort to align output for -a and -c modes
1 parent f9614ab commit c3cd548

File tree

1 file changed

+80
-22
lines changed

1 file changed

+80
-22
lines changed

bin/od

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ my %charname = (
5353
5 => 'enq',
5454
6 => 'ack',
5555
7 => 'bel',
56-
8 => 'bs',
57-
9 => 'ht',
58-
10 => 'nl',
59-
11 => 'vt',
60-
12 => 'ff',
61-
13 => 'cr',
62-
14 => 'so',
63-
15 => 'si',
56+
8 => ' bs',
57+
9 => ' ht',
58+
10 => ' nl',
59+
11 => ' vt',
60+
12 => ' ff',
61+
13 => ' cr',
62+
14 => ' so',
63+
15 => ' si',
6464
16 => 'dle',
6565
17 => 'dc1',
6666
18 => 'dc2',
@@ -70,14 +70,14 @@ my %charname = (
7070
22 => 'syn',
7171
23 => 'etb',
7272
24 => 'can',
73-
25 => 'em',
73+
25 => ' em',
7474
26 => 'sub',
7575
27 => 'esc',
76-
28 => 'fs',
77-
29 => 'gs',
78-
30 => 'rs',
79-
31 => 'us',
80-
32 => 'sp',
76+
28 => ' fs',
77+
29 => ' gs',
78+
30 => ' rs',
79+
31 => ' us',
80+
32 => ' sp',
8181
127 => 'del',
8282
);
8383

@@ -125,7 +125,7 @@ elsif ($opt_c) {
125125
$fmt = \&char1;
126126
}
127127
elsif ($opt_d) {
128-
$fmt = \&udecimal;
128+
$fmt = \&udecimal2;
129129
}
130130
elsif ($opt_e || $opt_F) {
131131
$fmt = \&float8;
@@ -137,7 +137,7 @@ elsif ($opt_H || $opt_X) {
137137
$fmt = \&hex4;
138138
}
139139
elsif ($opt_i || $opt_s) {
140-
$fmt = \&decimal;
140+
$fmt = \&decimal2;
141141
}
142142
elsif ($opt_l) {
143143
$fmt = \&long;
@@ -162,6 +162,28 @@ if (defined $opt_t) {
162162
$fmt = \&hex2;
163163
} elsif ($opt_t eq 'x4') {
164164
$fmt = \&hex4;
165+
} elsif ($opt_t eq 'o1') {
166+
$fmt = \&octal1;
167+
} elsif ($opt_t eq 'o2') {
168+
$fmt = \&octal2;
169+
} elsif ($opt_t eq 'o4') {
170+
$fmt = \&octal4;
171+
} elsif ($opt_t eq 'd1') {
172+
$fmt = \&decimal1;
173+
} elsif ($opt_t eq 'd2') {
174+
$fmt = \&decimal2;
175+
} elsif ($opt_t eq 'd4') {
176+
$fmt = \&decimal4;
177+
} elsif ($opt_t eq 'u1') {
178+
$fmt = \&udecimal1;
179+
} elsif ($opt_t eq 'u2') {
180+
$fmt = \&udecimal2;
181+
} elsif ($opt_t eq 'u4') {
182+
$fmt = \&udecimal4;
183+
} elsif ($opt_t eq 'a') {
184+
$fmt = \&char7bit;
185+
} elsif ($opt_t eq 'c') {
186+
$fmt = \&char1;
165187
} else {
166188
warn "$Program: unexpected output format specifier\n";
167189
exit EX_FAILURE;
@@ -272,6 +294,16 @@ sub octal1 {
272294
$strfmt = '%.3o ' x (scalar @arr);
273295
}
274296

297+
sub decimal1 {
298+
@arr = unpack 'C*', $data;
299+
$strfmt = '%4d ' x (scalar @arr);
300+
}
301+
302+
sub udecimal1 {
303+
@arr = unpack 'C*', $data;
304+
$strfmt = '%3u ' x (scalar @arr);
305+
}
306+
275307
sub hex1 {
276308
@arr = unpack 'C*', $data;
277309
$strfmt = '%.2x ' x (scalar @arr);
@@ -285,7 +317,7 @@ sub char1 {
285317
$arr[0] .= $charescs{$val} . " ";
286318
}
287319
elsif ($val > PRINTMAX || chr($val) !~ m/[[:print:]]/) {
288-
$arr[0] .= sprintf(' %03o', $val);
320+
$arr[0] .= sprintf('%03o ', $val);
289321
}
290322
else {
291323
$arr[0] .= " " . chr($val) . " ";
@@ -300,7 +332,7 @@ sub char7bit {
300332
for my $val (@arr1) {
301333
my $n = $val & 0x7f;
302334
if (exists $charname{$n}) {
303-
$arr[0] .= sprintf '%4s', $charname{$n};
335+
$arr[0] .= $charname{$n} . " ";
304336
}
305337
else {
306338
$arr[0] .= " " . chr($n) . " ";
@@ -309,7 +341,7 @@ sub char7bit {
309341
$strfmt = '%s';
310342
}
311343

312-
sub udecimal {
344+
sub udecimal2 {
313345
@arr = unpack 'S*', $data . zeropad(length($data), 2);
314346
$strfmt = '%5u ' x (scalar @arr);
315347
}
@@ -324,9 +356,9 @@ sub float8 {
324356
$strfmt = '%24.16e ' x (scalar @arr);
325357
}
326358

327-
sub decimal {
359+
sub decimal2 {
328360
@arr = unpack 's*', $data . zeropad(length($data), 2);
329-
$strfmt = '%5d ' x (scalar @arr);
361+
$strfmt = '%6d ' x (scalar @arr);
330362
}
331363

332364
sub long {
@@ -344,6 +376,16 @@ sub octal4 {
344376
$strfmt = '%.11o ' x (scalar @arr);
345377
}
346378

379+
sub decimal4 {
380+
@arr = unpack 'L*', $data . zeropad(length($data), 4);
381+
$strfmt = '%11d ' x (scalar @arr);
382+
}
383+
384+
sub udecimal4 {
385+
@arr = unpack 'L*', $data . zeropad(length($data), 4);
386+
$strfmt = '%11u ' x (scalar @arr);
387+
}
388+
347389
sub hex2 {
348390
@arr = unpack 'S*', $data . zeropad(length($data), 2);
349391
$strfmt = '%.4x ' x (scalar @arr);
@@ -468,7 +510,23 @@ Same as -i
468510
469511
=item -t Type
470512
471-
Select hexadecimal output size as either "x1", "x2" or "x4".
513+
Select output format as one of the following:
514+
515+
a ASCII character names. Same as -a
516+
c Characters with C escapes. Same as -c
517+
o1 1-byte unsigned octal
518+
o2 2-byte unsigned octal
519+
o4 4-byte unsigned octal
520+
d1 1-byte signed decimal
521+
d2 2-byte signed decimal
522+
d4 4-byte signed decimal
523+
u1 1-byte unsigned decimal
524+
u2 2-byte unsigned decimal
525+
u4 4-byte unsigned decimal
526+
x1 1-byte unsigned hexadecimal
527+
x2 2-byte unsigned hexadecimal
528+
x4 4-byte unsigned hexadecimal
529+
472530
This option overrides other formatting options.
473531
474532
=item -X

0 commit comments

Comments
 (0)