Skip to content

Commit

Permalink
tar: don't read stdin by default (#575)
Browse files Browse the repository at this point in the history
* tar: don't read stdin by default

* "perl tar" with no argument would read stdin but this is not desirable
* GNU tar prints usage in this case, and OpenBSD tar reads default tape device
* Make file option (-f) mandatory; follow OpenBSD version where "tar -f -" indicates an archive is read from stdin

* tar: avoid die() and exit directly

* Remove unused variable $dh
* Delete commented code
* Introduce symbolic exit codes and replace calls to die with custom fatal() function
  • Loading branch information
mknos committed Apr 23, 2024
1 parent 302bad2 commit dd14275
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions bin/tar
Expand Up @@ -11,13 +11,19 @@ License:
=cut

use strict;

use 5.004;
# use strict;
use File::Basename qw(basename);
use Getopt::Std;
use IO::File;

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

use vars qw($opt);

my $Program = basename($0);

BEGIN
{
$opt = 'ctxvmf:';
Expand All @@ -36,12 +42,19 @@ BEGIN
my %opt;
getopts($opt,\%opt);

sub fatal
{
my $msg = shift;
warn "$Program: $msg\n";
exit EX_FAILURE;
}

sub read_header
{
my $read = shift;
my $buf = '';
my $err = &$read($buf,512);
die "Cannot read:$err" if $err;
fatal("Cannot read:$err") if $err;
if (length($buf) == 512)
{
return undef if $buf =~ /^\0{512}/;
Expand Down Expand Up @@ -71,7 +84,7 @@ sub read_header
}
else
{
die "size is ".length($buf)." not 512" if (length($buf));
fatal("size is " . length($buf) . " not 512") if (length($buf));
}
return undef;
}
Expand All @@ -81,17 +94,16 @@ sub read_data
my ($read,$hdr,$fh) = @_;
my $size = $hdr->{'size'};
my $blocks = int(($size+511)/512);
# print "$size => $blocks\n";
my $first = 1;
while ($blocks--)
{
my $buf = '';
my $err = &$read($buf,512);
die "Cannot read:$err" if ($err);
fatal("Cannot read: $err") if $err;
my $len = length($buf);
if ($len != 512)
{
die "Size is $len not 512:$!";
fatal("Size is $len not 512: $!");
}
if ($fh)
{
Expand Down Expand Up @@ -123,7 +135,7 @@ sub make_dir
make_dir($1) if ($name =~ m#^(.*)/[^/]+#);
unless (-d $name)
{
mkdir($name,0777) || die "Cannot mkdir($name):$!";
mkdir($name, 0777) or fatal("Cannot create directory '$name': $!");
warn "mkdir $name\n" if ($opt{'v'});
}
}
Expand Down Expand Up @@ -183,7 +195,7 @@ sub list_entry

if ($opt{'c'})
{
die "-c not implemeted\n";
fatal('-c not implemeted');
}
else
{
Expand All @@ -195,19 +207,26 @@ else

if (defined $opt{'f'})
{
die("Cannot open '$opt{'f'}': is a directory\n") if (-d $opt{'f'});
open($fh, '<', $opt{'f'}) || die "Cannot open $opt{'f'}:$!";
if ($opt{'f'} eq '-')
{
$fh = *STDIN;
}
else
{
fatal("Cannot open '$opt{'f'}': is a directory") if (-d $opt{'f'});
open($fh, '<', $opt{'f'}) or fatal("Cannot open $opt{'f'}: $!");
}
}
else
{
$fh = *STDIN;
fatal("No archive file specified; -f must be set");
}
binmode $fh;

if ($opt{'z'} || $opt{'Z'})
{
# quick and dirty till we sort out Compress::Zlib
my $gz = gzopen($fh, 'rb') or die "Cannot gzopen:$gzerrno";
my $gz = gzopen($fh, 'rb') or fatal("Cannot gzopen: $gzerrno");
$read = sub { $gz->gzread($_[0],$_[1]) < 0 ? $gzerrno : 0 };
}
else
Expand All @@ -216,7 +235,6 @@ else
}
while ($hdr = read_header($read))
{
my $dh;
if ($opt{'x'})
{
extract_entry($read,$hdr);
Expand All @@ -233,9 +251,10 @@ else
{
print $hdr->{'archname'},"\n";
}
#last;
}
}
exit EX_SUCCESS;

__END__
=encoding utf8
Expand Down

0 comments on commit dd14275

Please sign in to comment.