#!/usr/local/bin/new/perl -w
use strict;
use vars qw($file);
use Data::Dumper;

use File::Find;
use Getopt::Std;

my %opt;
getopts("mlce:",\%opt);

my $expr = (defined $opt{'e'}) ? $opt{'e'} : shift;
print "Matching '$expr'\n";
        
my $count = 0;

sub match
{
 if (/$expr/o)
  {
   if ($opt{'l'})
    {
     print "$File::Find::name\n";
     return 1;
    }
   $count++;
   unless ($opt{'c'})
    {
     print "$File::Find::name:$.: $_" 
    }
  }
 return 0;
}

sub wanted
{
 $File::Find::prune = 0;
 if (-T $_ && !/%$/ && /\.([chC]|cpp)$/)
  {
   local $file   = ($_);
   local ($_);
   no strict 'refs'; 
   open($file,"<$file") || die "Cannot open $file:$!";
   while (<$file>)
    {
     last if &match;
    }
   close($file);
   if ($opt{'c'} && $count)
    {
     print "$File::Find::name: $count\n" 
    }
  }
 elsif (-d $_)
  {
   $File::Find::prune = 1 if ($_ eq 'mTk' && $opt{'m'});
  }
}

@ARGV = '.' unless (@ARGV);

find(\&wanted,@ARGV);
