[comp.lang.perl] Not Exactly find

clipper@no9sun.csd.uwo.ca (Khun Yee Fung) (11/06/90)

This is the package I used to scan a directory to do things to files
and directories recursively. I have never liked the syntax find(1). So
I used this to do compressdir(1) and uncompressdir(1). It is about
100% faster than the find(1) based compressdir(1) and uncompressdir(1)
on our machine.

It uses a breadth first search; might not be useful for your purposes.

Send your comments to clipper@csd.uwo.ca. Thanks.

Khun Yee
clipper@csd.uwo.ca

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	dir.pl
#	cdir
#
if test -f 'dir.pl'
then
	echo shar: will not over-write existing file "'dir.pl'"
else
	echo x - 'dir.pl'
	sed 's/^X//' >'dir.pl' << 'SHAR_EOF'
X#-*-perl-*-
X#
X# copyright (c) 1990 Khun Yee Fung <clipper@csd.uwo.ca>
X# See the end of this file for warranty information
X#
X# [I don't know nroff and don't intend to learn it. Anybody intends to
X# write a manual page style for LaTeX with a tex-to-ascii driver?]
X#
X# How to use this package:
X#     &dodir($dir, $file, $directory)
X# The result is the set of error messages obtained when executing $dir
X# on all the directories found, and $file on all the files found in
X# the directory $directory, recursively. The directory or file found
X# is in $FILE.
X#
X# For example, to compress all files in a directory $ARGV[0]:
X#
X# require 'dir.pl';
X#
X# $f = "if (\$FILE !~ /\\.Z\$/) {
X#          system(\"compress \$FILE\");
X#    }";
X# $d = '';
X# $, = "\n";
X# print &dodir($d, $f, $ARGV[0]);
X#
X
Xpackage scandir;
X
Xsub main'dodir {
X    local($d, $f, $argv) = @_;
X
X    if (!defined($argv)) {
X	return @a = ("No argument");
X    }
X    if ($argv eq '.') {
X	opendir($dir, ".");
X	@files = readdir($dir);
X	closedir($dir);
X	foreach $file (@files) {
X	    push(@FILES, "./$file");
X	}
X    }
X    elsif ($argv eq '..') {
X	opendir($dir, "..");
X	@files = readdir($dir);
X	closedir($dir);
X	foreach $file (@files) {
X	    push(@FILES, "../$file");
X	}
X    }
X    else {
X	push(@FILES, $argv);
X    }
X    select(STDOUT); $| = 1;
X    while ($FILE = shift(@FILES)) {
X	next if ($FILE eq '..' || $FILE eq '.' || $FILE =~ m-.*/\.\.- ||
X		 $FILE =~ m-.*/\.-);
X	@stat = lstat($FILE);
X	$type = $stat[2] & 0170000;
X	if ($type == 040000) {
X	    # found a directory
X	    if (!opendir(DIR, $FILE)) {
X		push (@a, "Can't open $FILE");
X	    }
X	    else {
X		eval $d;
X		if ($@ ne '') {
X		    push(@a, "[$FILE]:$@");
X		}
X		@files = readdir(DIR);
X		closedir(DIR);
X		foreach $file (@files) {
X		    push(@FILES, "$FILE/$file");
X		}
X	    }
X	}
X	elsif ($type == 0100000) {
X	    # a regular file
X	    eval $f;
X	    if ($@ ne '') {
X		push(@a, "[$FILE]:$@");
X	    }
X	}
X    }
X    @a;
X}
X1;
X
X__END__
X
X# Copyright 1990 Khun Yee Fung <clipper@csd.uwo.ca>
X#
X# Permission to use, copy, modify, and distribute, this software and its
X# documentation for any purpose is hereby granted without fee, provided that
X# the above copyright notice appear in all copies and that both that
X# copyright notice and this permission notice appear in supporting
X# documentation, and that the name of the copyright holder be used in
X# advertising or publicity pertaining to distribution of the software with
X# specific, written prior permission, and that no fee is charged for further
X# distribution of this software, or any modifications thereof.  The copyright
X# holder makes no representations about the suitability of this software for
X# any purpose.  It is provided "as is" without express or implied warranty.
X#
X# THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
X# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
X# EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
X# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
X# DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
X# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
X# PERFORMANCE OF THIS SOFTWARE.
SHAR_EOF
if test 3214 -ne "`wc -c < 'dir.pl'`"
then
	echo shar: error transmitting "'dir.pl'" '(should have been 3214 characters)'
fi
fi
if test -f 'cdir'
then
	echo shar: will not over-write existing file "'cdir'"
else
	echo x - 'cdir'
	sed 's/^X//' >'cdir' << 'SHAR_EOF'
X#!/usr2/new/bin/perl                                    -*-perl-*-
X
Xrequire 'dir.pl';
X
X$f = "if (\$FILE !~ /\\.Z\$/) {
X          system(\"compress \$FILE\");
X    }";
X$d = '';
X$, = "\n";
Xprint &dodir($d, $f, $ARGV[0]);
SHAR_EOF
if test 218 -ne "`wc -c < 'cdir'`"
then
	echo shar: error transmitting "'cdir'" '(should have been 218 characters)'
fi
fi
echo Done
exit 0
--
----
In Real life: Khun Yee Fung    clipper@csd.uwo.ca (Internet) 
Alternative: 4054_3267@UWOVAX.BITNET
UUCP: ...!{ihnp4|decvax|seismo}!{watmath|utzoo}!ria!csd!clipper
Department of Computer Science
Middlesex College
The University of Western Ontario
London, Ontario, N6A 5B7  CANADA