#!/usr/bin/perl -w # $Id: pgrep,v 1.11 1999/08/13 04:41:09 earl Exp $ # Copyright (c) 1999 by Earl A. Killian. # # This file 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 version 2. # # This file 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. # # You should have received a copy of the GNU General Public License # along with this file; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA package pgrep; use strict; # Command line parse use vars qw($pat $countonly $ignorecase $listonly $linenumbers $silent $test $filenames $hierarchical %exclude $excludepat $print $man $uncompress); { $::myname = 'pgrep'; die("Usage is: $::myname [options] pattern files...\n") if $#ARGV < 0; my @pat = (); $countonly = 0; $ignorecase = 0; $listonly = 0; $linenumbers = 0; $silent = 0; $test = 1; $hierarchical = 0; $print = undef; undef %exclude; $excludepat = undef; $man = 0; $uncompress = 0; while ($#ARGV >= 0 && $ARGV[0] =~ /^-/) { my $o; foreach $o (split(//, substr(shift(@ARGV), 1))) { if ($o eq 'e') { die("$::myname: no pattern for -e\n") if $#ARGV < 0; push(@pat, shift(@ARGV)); } elsif ($o eq 'c') { $countonly = 1; } elsif ($o eq 'h') { $filenames = 0; } elsif ($o eq 'i') { $ignorecase = 1; } elsif ($o eq 'l') { $listonly = 1; } elsif ($o eq 'n') { $linenumbers = 1; } elsif ($o eq 's') { $silent = 1; } elsif ($o eq 'v') { $test = 0; } elsif ($o eq 'd') { $hierarchical = 1; } elsif ($o eq 'x') { die("$::myname: no path for -x\n") if $#ARGV < 0; $exclude{shift(@ARGV)} = 1; } elsif ($o eq 'X') { die("$::myname: no pattern for -X\n") if $#ARGV < 0; $excludepat = shift(@ARGV); } elsif ($o eq 'p') { $print = shift(@ARGV); } elsif ($o eq 'm') { $man = 1; } elsif ($o eq 'z') { $uncompress = 1; } else { die ("$::myname: -$o not understood.\n"); } } } if ($#pat < 0) { die("$::myname: no pattern specified\n") if $#ARGV < 0; push(@pat, shift(@ARGV)); } $filenames = ($#ARGV > 0 || grep(-d $_, @ARGV)) && !$silent && !$listonly if !defined($filenames); $pat = join('|',@pat); } # Program use vars qw($total); { $total = 0; my $f; foreach $f (@ARGV) { if (! -d $f) { pgrep ($f); } elsif ($hierarchical) { walktree (\&pgrep, $f); } } exit ($total == 0); } sub pgrep { my ($f) = @_; open (F, $uncompress ? "zcat $f|" : "<$f") || die("$::myname: $!, opening $f.\n"); my $n = 0; my $count = 0; if ($ignorecase) { while () { $n += 1; s/.//g if $man; if (/$pat/io == $test) { $count += 1; $total += 1; return if $silent; if ($listonly) { print $f,"\n"; return; } if (!$countonly) { print $f,':' if $filenames; print $n,':' if $linenumbers; if (defined($print)) { print eval($print), "\n"; } else { print $_; } } } } } else { while () { $n += 1; s/.//g if $man; if (/$pat/o == $test) { $count += 1; $total += 1; return if $silent; if ($listonly) { print $f,"\n"; return; } if (!$countonly) { print $f,':' if $filenames; print $n,':' if $linenumbers; if (defined($print)) { print eval($print), "\n"; } else { print $_; } } } } } close (F); if ($countonly) { print $f,':' if $filenames; print $count,"\n"; } } # pgrep # walktree(function, path) # if path is a file, call function with that filename # if path is a directory, call function on all files contained in that # directory hierarchy sub walktree { my ($function, $path) = @_; if ( $exclude{$path} || (defined($excludepat) && $path =~ /$excludepat/o)) { # ignore file or directory } elsif (-l $path) { # ignore links } elsif (-f $path) { &$function ($path); } elsif (-d $path) { opendir (D, $path) || die("$::myname: Error: $!, opening directory '$_'.\n"); my @dir = grep($_ ne '.' && $_ ne '..', readdir(D)); closedir (D); my $e; foreach $e (@dir) { walktree ($function, $path . '/' . $e); } } else { print STDERR ("$::myname: Warning: '$path' is not a file, link, or directory.\n"); } } # # Local Variables: # mode:perl # perl-indent-level:2 # End: