Skip to content

Commit b9346ec

Browse files
authored
grep -r should not sort directory entries
* A directory might have 1000s of entries and sorting would make grep -r slower (there is no standard option to toggle sort behaviour) * Be consistent with OpenBSD and GNU versions by processing directory entries in the order they appear * As part of verification, I compared output of grep -r on OpenBSD (filenames not sorted) with output of "ls -1a" (filenames sorted) * Clarification: list-list is only for setting up argument list for recursive matchfile() call. Declare it within if-block scope to avoid potential of old entries appearing between loop iterations.
1 parent 04d90b4 commit b9346ec

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

bin/grep

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use constant EX_MATCHED => 0;
5858
use constant EX_NOMATCH => 1;
5959
use constant EX_FAILURE => 2;
6060

61-
our $VERSION = '1.018';
61+
our $VERSION = '1.019';
6262

6363
$| = 1; # autoflush output
6464

@@ -367,7 +367,7 @@ sub matchfile {
367367
$opt = shift; # reference to option hash
368368
$matcher = shift; # reference to matching sub
369369

370-
my ( $file, @list, $total, $name );
370+
my ( $file, $total, $name );
371371
local ($_);
372372
$total = 0;
373373

@@ -397,13 +397,12 @@ FILE: while ( defined( $file = shift(@_) ) ) {
397397
}
398398
next FILE;
399399
}
400-
@list = ();
400+
my @list;
401401
for (readdir $dh) {
402402
next if $_ eq '.' or $_ eq '..';
403403
push @list, File::Spec->catfile($file, $_);
404404
}
405405
closedir $dh;
406-
@list = sort @list;
407406
matchfile( $opt, $matcher, @list ); # process files
408407
next FILE;
409408
}

0 commit comments

Comments
 (0)