Skip to content

Commit

Permalink
grep: no match for empty pattern file (#821)
Browse files Browse the repository at this point in the history
* Testing against old commit 504caa9, "grep -f empty" was handled by exit(1), i.e. no match
* A recent commit introduced a bug where ARGV would be shifted incorrectly if an empty file was passed as argument to -f
* Follow GNU grep and bypass the search in this case because we know there cannot be any matches

%touch empty && perl grep -f empty ar
%echo $?
1
  • Loading branch information
mknos authored Nov 19, 2024
1 parent 4427b73 commit 822219a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions bin/grep
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ use File::Spec;
use File::Temp qw();
use Getopt::Std;

our $VERSION = '1.017';
use constant EX_MATCHED => 0;
use constant EX_NOMATCH => 1;
use constant EX_FAILURE => 2;

our $VERSION = '1.018';

$| = 1; # autoflush output

Expand All @@ -75,15 +79,15 @@ my %Compress = (
my ($opt, $matcher) = parse_args(); # get command line options and patterns
matchfile( $opt, $matcher, @ARGV ); # process files

exit(2) if $Errors;
exit(0) if $Grand_Total;
exit(1);
exit(EX_FAILURE) if $Errors;
exit(EX_MATCHED) if $Grand_Total;
exit(EX_NOMATCH);

###################################

sub VERSION_MESSAGE {
print "$Me version $VERSION\n";
exit 0;
exit EX_MATCHED;
}

sub usage {
Expand Down Expand Up @@ -214,6 +218,7 @@ sub parse_args {
}

# multiple -e/-f options
my $opt_f = 0;
my @tmparg;
while (@ARGV) {
my $arg = shift @ARGV;
Expand All @@ -223,6 +228,7 @@ sub parse_args {
push @patterns, $pattern;
}
elsif ($arg =~ s/\A\-f//) {
$opt_f = 1;
my $file = length($arg) ? $arg : shift(@ARGV);
usage() unless defined $file;
die "$Me: $file: is a directory\n" if -d $file;
Expand Down Expand Up @@ -260,6 +266,9 @@ sub parse_args {
$opt{'l'} = 0 if $opt{'L'};
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );

if ($opt_f && scalar(@patterns) == 0) { # empty -f file allowed
exit EX_NOMATCH;
}
unless (length $pattern) {
$pattern = shift @ARGV;
usage() unless defined $pattern;
Expand Down

0 comments on commit 822219a

Please sign in to comment.