Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tac: exit(1) on error #903

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions bin/tac
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $Program = basename($0);
my $VERSION = '0.18';
my $VERSION = '0.19';

my %opts;
getopts('bBrs:S:', \%opts) or usage();
Expand Down Expand Up @@ -57,6 +57,7 @@ unless ($fh) {
exit EX_FAILURE;
}
print while <$fh>;
exit EX_FAILURE if $fh->get_error;
exit EX_SUCCESS;

sub usage {
Expand Down Expand Up @@ -99,11 +100,12 @@ sub TIEHANDLE {

*$self = {
%opts,
lines => [], # Lines in memory.
scrap => '', # Incomplete line.
EOF => 0, # Finished reading current file.
count => 0, # Current line number.
ends => [], # Array of ORS for 'autoline'.
'lines' => [], # Lines in memory.
'scrap' => '', # Incomplete line.
'EOF' => 0, # Finished reading current file.
'count' => 0, # Current line number.
'ends' => [], # Array of ORS for 'autoline'.
'error' => 0,
};

# Set mode for opening file.
Expand All @@ -120,15 +122,18 @@ sub TIEHANDLE {
*$self->{'files'} = [];
foreach my $file (@files) {
if (-d $file) {
*$self->{'error'} = 1;
warn "$Program: '$file' is a directory\n";
next;
}
my $fh;
unless (sysopen $fh, $file, $mode) {
*$self->{'error'} = 1;
warn "$Program: failed to open '$file': $!\n";
next;
}
unless (sysseek $fh, 0, 2) {
*$self->{'error'} = 1;
warn "$Program: seek failed for '$file': $!\n";
next;
}
Expand Down Expand Up @@ -194,6 +199,11 @@ sub READLINE {
pop @{*$self->{lines}};
}

sub get_error {
my $self = shift;
return *$self->{'error'};
}

sub get_lines {
my $self = shift;

Expand Down
Loading