Skip to content

Commit 1bd92f5

Browse files
authored
od: support 1-byte hex format (briandfoy#927)
* od is expected to support a -t option for specifying output formats that don't exist as a single letter option * 2- and 4-byte hex output is possible without -t, but "-t x1" is the normal way to request 1-byte hex * Other format specifiers can be added to -t later, but for now add x1, x2 and x4 * Update pod and usage string
1 parent c29470d commit 1bd92f5

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

bin/od

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ use constant LINESZ => 16;
2424
use constant PRINTMAX => 126;
2525

2626
use vars qw/ $opt_A $opt_a $opt_B $opt_b $opt_c $opt_d $opt_e $opt_F $opt_f
27-
$opt_H $opt_i $opt_j $opt_l $opt_N $opt_O $opt_o $opt_s $opt_v $opt_X $opt_x /;
27+
$opt_H $opt_i $opt_j $opt_l $opt_N $opt_O $opt_o $opt_s $opt_t $opt_v
28+
$opt_X $opt_x /;
2829

29-
our $VERSION = '1.1';
30+
our $VERSION = '1.2';
3031

3132
my ($offset1, $radix, $data, @arr, $lim);
3233
my ($lastline, $strfmt, $ml);
@@ -85,7 +86,7 @@ $lastline = '';
8586

8687
my $Program = basename($0);
8788

88-
getopts('A:aBbcdeFfHij:lN:OosvXx') or help();
89+
getopts('A:aBbcdeFfHij:lN:Oost:vXx') or help();
8990
if (defined $opt_A) {
9091
if ($opt_A !~ m/\A[doxn]\z/) {
9192
warn "$Program: unexpected radix: '$opt_A'\n";
@@ -154,6 +155,19 @@ else {
154155
$fmt = \&octal2;
155156
}
156157

158+
if (defined $opt_t) {
159+
if ($opt_t eq 'x1') {
160+
$fmt = \&hex1;
161+
} elsif ($opt_t eq 'x2') {
162+
$fmt = \&hex2;
163+
} elsif ($opt_t eq 'x4') {
164+
$fmt = \&hex4;
165+
} else {
166+
warn "$Program: unexpected output format specifier\n";
167+
exit EX_FAILURE;
168+
}
169+
}
170+
157171
my $nread = 0;
158172
my $rc = EX_SUCCESS;
159173
foreach my $file (@ARGV) {
@@ -258,6 +272,11 @@ sub octal1 {
258272
$strfmt = '%.3o ' x (scalar @arr);
259273
}
260274

275+
sub hex1 {
276+
@arr = unpack 'C*', $data;
277+
$strfmt = '%.2x ' x (scalar @arr);
278+
}
279+
261280
sub char1 {
262281
@arr = ();
263282
my @arr1 = unpack 'C*', $data;
@@ -348,7 +367,8 @@ sub diffdata {
348367
}
349368

350369
sub help {
351-
print "usage: od [-aBbcdeFfHilOosXxv] [-A radix] [-j skip_bytes] [-N limit_bytes] [file]...\n";
370+
print "usage: od [-aBbcdeFfHilOosXxv] [-A radix] [-j skip_bytes] ",
371+
"[-N limit_bytes] [-t type] [file]...\n";
352372
exit EX_FAILURE;
353373
}
354374
__END__
@@ -359,7 +379,8 @@ od - dump files in octal and other formats
359379
360380
=head1 SYNOPSIS
361381
362-
B<od> [ I<-aBbcdeFfHilOosXxv> ] [I<-j skip_n_bytes>] [I<-N read_n_bytes>] [ I<-A radix> ] [ F<file>... ]
382+
B<od> [ I<-aBbcdeFfHilOosXxv> ] [I<-j skip_bytes>] [I<-N limit_bytes>]
383+
[ I<-A radix> ] [ I<-t type> ] [ F<file>... ]
363384
364385
=head1 DESCRIPTION
365386
@@ -445,6 +466,11 @@ Format input as two-byte octal numbers.
445466
446467
Same as -i
447468
469+
=item -t Type
470+
471+
Select hexadecimal output size as either "x1", "x2" or "x4".
472+
This option overrides other formatting options.
473+
448474
=item -X
449475
450476
Same as -H

0 commit comments

Comments
 (0)