Skip to content

Commit

Permalink
tac: validate input for -S option (#529)
Browse files Browse the repository at this point in the history
* tac -S option takes a number of bytes to read; this option does not exist in GNU version
* It doesn't make sense for -S value to be negative or zero so enforce a minimum of 1
* Printing usage string seemed slightly helpful, so create a usage() function
* The -S option might provide no benefit to the user and could be removed in future
  • Loading branch information
mknos committed Apr 3, 2024
1 parent 5bc24e5 commit d9d72ca
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions bin/tac
Expand Up @@ -28,11 +28,7 @@ my $Program = basename($0);
my $VERSION = '0.18';

my %opts;
unless (getopts('bBrs:S:', \%opts)) {
warn "$Program version $VERSION\n";
warn "usage: $Program [-br] [-s separator] [-B] [-S bytes] [file...]\n";
exit EX_FAILURE;
}
getopts('bBrs:S:', \%opts) or usage();
my %long = qw/
b before
B binary
Expand All @@ -48,7 +44,12 @@ if (defined $opts{separator} && $opts{regex}) {
$_ = qr/$_/;
}
}

if (defined $opts{'size'}) {
if ($opts{'size'} !~ m/\A[0-9]+\Z/ || $opts{'size'} == 0) {
warn "$Program: option -S expects a number >= 1\n";
usage();
}
}
$opts{files} = \@ARGV;

my $fh = IO::Tac->new(%opts);
Expand All @@ -58,6 +59,12 @@ unless ($fh) {
print while <$fh>;
exit EX_SUCCESS;

sub usage {
warn "$Program version $VERSION\n";
warn "usage: $Program [-br] [-s separator] [-B] [-S bytes] [file...]\n";
exit EX_FAILURE;
}

END {
close STDOUT || die "$Program: can't close stdout: $!\n";
$? = 1 if $? == 255; # from die
Expand Down

0 comments on commit d9d72ca

Please sign in to comment.