Skip to content

Commit b30249e

Browse files
authored
tar: fail early for bad options (#978)
* tar: fail early for bad options * Add missing getopts() return value check * This version of tar didn't have a usage string, so add one * Don't list -c in usage string for now because archive creation isn't implemented * Unconditionally use Compress::Zlib because it is a core library * lazy import for compression feature
1 parent 0fd0e9c commit b30249e

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

bin/tar

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,13 @@ use IO::File;
2020
use constant EX_SUCCESS => 0;
2121
use constant EX_FAILURE => 1;
2222

23-
use vars qw($opt);
24-
2523
my $Program = basename($0);
2624

27-
BEGIN
28-
{
29-
$opt = 'ctxvmf:';
30-
eval { require Compress::Zlib };
31-
if ($@)
32-
{
33-
warn "No decompression available: $@\n";
34-
}
35-
else
36-
{
37-
Compress::Zlib->import;
38-
$opt .= 'zZ';
39-
}
40-
}
41-
4225
my %opt;
43-
getopts($opt,\%opt);
26+
getopts('ctxvmf:Zz', \%opt) or do {
27+
warn "usage: tar {-tx} [-mvZz] [-f archive] [file ...]\n";
28+
exit EX_FAILURE;
29+
};
4430

4531
sub fatal
4632
{
@@ -225,7 +211,8 @@ else
225211

226212
if ($opt{'z'} || $opt{'Z'})
227213
{
228-
# quick and dirty till we sort out Compress::Zlib
214+
eval { require Compress::Zlib } or fatal('Compress::Zlib not found');
215+
Compress::Zlib->import;
229216
my $gz = gzopen($fh, 'rb') or fatal("Cannot gzopen: $Compress::Zlib::gzerrno");
230217
$read = sub { $gz->gzread($_[0],$_[1]) < 0 ? $Compress::Zlib::gzerrno : 0 };
231218
}

0 commit comments

Comments
 (0)