Skip to content

Commit

Permalink
Merge pull request #270 from mknos/tac-continue
Browse files Browse the repository at this point in the history
tac: continue on error
  • Loading branch information
briandfoy authored Sep 29, 2023
2 parents c446f6a + 3b90ef2 commit b112e71
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions bin/tac
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ $opts{files} = \@ARGV;

my $fh = IO::Tac->new(%opts);
unless ($fh) {
warn "$Program: can't open file: $!\n";
exit EX_FAILURE;
}
print while <$fh>;
Expand Down Expand Up @@ -102,15 +101,31 @@ sub TIEHANDLE {
$mode |= O_BINARY if *$self->{binary};

# Open files for reading.
@files = map {
local *FH;
sysopen FH, $_, $mode or return;
sysseek FH, 0, 2 or return;
[$_, *FH];
} @files ? @files : @ARGV;

# Add files and filehandles to object.
*$self->{files} = @files ? \@files : [['-', *STDIN]];
if (scalar(@files) == 0 && scalar(@ARGV) == 0) {
*$self->{'files'} = [['-', *STDIN]];
} else {
if (scalar(@files) == 0) {
@files = @ARGV;
}
*$self->{'files'} = [];
foreach my $file (@files) {
if (-d $file) {
warn "$Program: '$file' is a directory\n";
next;
}
my $fh;
unless (sysopen $fh, $file, $mode) {
warn "$Program: failed to open '$file': $!\n";
next;
}
unless (sysseek $fh, 0, 2) {
warn "$Program: seek failed for '$file': $!\n";
next;
}
push @{ *$self->{'files'} }, [$file, $fh];
}
}
return if (scalar @{ *$self->{'files'} } == 0);

# Keep track of current file.
$ARGV = *$self->{files}[0][0];
Expand Down

0 comments on commit b112e71

Please sign in to comment.