Skip to content

Commit

Permalink
grep: don't auto matically decompress (#799)
Browse files Browse the repository at this point in the history
* When run on a binary file, grep should search it as a binary file
* Regular grep should not decompress a file (this version was decompressing based on file extension)
* Adopt the -Z flag from OpenBSD which makes grep behave as zgrep
* Sync POD manual

%gzip -9k ar # create ar.gz
%perl grep ar ar.gz # original filename stored in binary
grep: ar.gz: binary file matches
%perl grep perl ar.gz # no match
%perl grep -Z perl ar.gz # match after decompression
#!/usr/bin/perl
License: perl
  • Loading branch information
mknos authored Nov 12, 2024
1 parent d462a62 commit 0c0305a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bin/grep
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use File::Spec;
use File::Temp qw();
use Getopt::Std;

our $VERSION = '1.009';
our $VERSION = '1.010';

$| = 1; # autoflush output

Expand Down Expand Up @@ -116,6 +116,7 @@ Options:
-a treat binary files as plain text files
-s suppress errors for failed file and dir opens
-T trace files as opened
-Z force grep to behave as zgrep
May use GREP_OPTIONS environment variable to set default options.
EOF
Expand Down Expand Up @@ -247,7 +248,7 @@ sub parse_args {
@ARGV = @tmparg;

$opt{'p'} = $opt{'P'} = ''; # argument to print()
getopts('inCcwsxvHhe:f:Ll1gurpP:aqTF', \%opt) or usage();
getopts('inCcwsxvHhe:f:Ll1gurpP:aqTFZ', \%opt) or usage();

$opt{'l'} = 0 if $opt{'L'};
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );
Expand Down Expand Up @@ -419,7 +420,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
}

my ($ext) = $file =~ /\.([^.]+)$/;
if ( defined $ext && exists $Compress{$ext} ) {
if ($opt->{'Z'} && defined $ext && exists $Compress{$ext}) {
$file = "$Compress{$ext} $file |";
$compressed = 1;
}
Expand Down Expand Up @@ -675,6 +676,11 @@ for the precise definition of C<\b>.
Exact matches only. The pattern must match the entire line or paragraph.
=item B<-Z>
Operate in zgrep mode. Compressed file arguments are decompressed before
searching for the pattern.
=back
=head1 ENVIRONMENT
Expand Down

0 comments on commit 0c0305a

Please sign in to comment.