# Title:    where.pl
# Author:   Sean M. Burke, sburke@cpan.org, 4 Sept 1996.
# Description:
#  This is a perl script to be used under MSDOS; this emulates (and extends)
#  the behavior of the UNIX shell "where" function.  You enter a key and
#  it looks thru directories in your PATH (plus the current directory) and
#  tells you what files match that key.
#
# Usage:
#  where [search key]              (requires a where.bat, as described below)
# or
#  perl c:\path_to_script\where.pl [search key]
#
# The search key is regexp, not a glob.  (E.g., "xy*.*" doesn't mean
#  what you're used to it meaning, altho it might work in a pinch.)
#
# Example:
#  where wp
#  where \.com
#
# To use, make a batch file called where.bat, put it in a directory
# in your PATH, and then make it consist of:
#	@echo off
#	@perl -S where.pl %1
# Make sure where.pl is also in a directory in your PATH
#
# Availability & Copying:
#
# where.pl is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# where.pl is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# To see a copy of the GNU General Public License, see
# http://www.ling.nwu.edu/~sburke/gnu_release.html, or write to the
# Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# ------------------------------------------------------------


$key = $ARGV[0];
exit if ($key eq '');

$path =  $ENV{'PATH'};
$path =~ tr/A-Z/a-z/;
#print "Key '$key' in path $path\n";

foreach $dir (&uniq('.' , split(/\;+/, $path))) {
 #print "looking in $dir\n";
 opendir(DIR, $dir);
 @matches = grep(/$key/, readdir(DIR));
 closedir(DIR);
 foreach $match (sort(@matches)) {
  $match =~ tr/A-Z/a-z/;
  $match = "$dir\\$match";
  $match =~ s!\/\/+!\/!g;
  push (@all_matches, $match);
 }

}
print join("\n", @all_matches, '');

######################################################################
sub uniq {
    local (@inlist, %outlist, $listitem);

    @inlist = @_;

    foreach $listitem (@inlist) {
        $outlist{$listitem} = 1;
    }
    return(keys(%outlist));  # Note -- returned in NO PARTICULAR ORDER
}


