Skip to content

Commit

Permalink
Merge pull request #320 from mknos/printf-hex
Browse files Browse the repository at this point in the history
printf: support hex number arguments
  • Loading branch information
briandfoy authored Nov 13, 2023
2 parents c391e0b + cf039ff commit 28962ce
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions bin/printf
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ License: perl

use strict;

END {
close STDOUT || die "$0: can't close stdout: $!\n";
$? = 1 if $? == 255; # from die
}
use File::Basename qw(basename);

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $Program = basename($0);

unless (@ARGV) {
die "usage: $0 format [argument ...]\n";
warn "usage: $Program format [argument ...]\n";
exit EX_FAILURE;
}

my $format = shift;
$format =~ s/\\v/\x0b/g; # escape \v not available in printf()
$format =~ s/\%c/\%\.1s/g; # standard printf: %c == 1st char
eval qq(printf "$format", \@ARGV);
die if $@;

my @ints = map { m/\A0x/i ? hex : int } @ARGV;
eval qq(printf "$format", \@ints) or do {
warn "$Program: $@\n";
exit EX_FAILURE;
};
exit EX_SUCCESS;

__END__
Expand Down

0 comments on commit 28962ce

Please sign in to comment.