brister@decwrl.dec.com (James Brister) (11/05/90)
I'm writing a perl replacement for the lndir program that comes with X11.
The code (which follows) does a recursive walk of the directory tree and
seems to have two problems:
1) When the subroutine lndir hits the first leaf directory, the $file
variable in the foreach loop doesn't get incremented and an infinite
loops results.
2) Does readdir reuire that an NFS file system be mount read-write? When I
try this program with a read-only file system directory as the first
argument, I only get the lost+found directory displayed in the "mkdir"
line.
What's up here?
---code starts---
#!/usr/local/bin/perl -w
die "Usage: lndir sourcedir destdir" if ($#ARGV != 1) ;
$sourcedir = $ARGV[0] ;
$destdir = $ARGV [1] ;
die "$sourcedir is not a directory" if ( ! -d $sourcedir ) ;
die "$destdir is not a directory" if ( ! -d $destdir ) ;
# make sure last character is a backslash
$sourcedir .= '/' if (substr ($sourcedir,-1,1) ne '/') ;
$destdir .= '/' if (substr ($destdir,-1,1) ne '/') ;
do lndir ($sourcedir,$destdir) ;
exit 0 ;
sub lndir {
local ($source,$dest) = @_ ;
local ($dirlist,$oldfile,$newfile,$file) ;
opendir ("DIR",$source) || die "Can't opendir $source" ;
@dirlist = readdir ("DIR") ;
closedir ("DIR") ;
foreach $file (@dirlist) {
next if ($file eq "." || $file eq "..") ;
$oldfile = $source . $file ;
$newfile = $dest . $file ;
if (! -d $oldfile) {
print "Doing symlink ($oldfile,$newfile) ;\n" ;
} else {
print "Doing mkdir ($newfile) ;\n" ;
$oldfile .= '/' ;
$newfile .= '/' ;
do lndir ($oldfile,$newfile) ;
}
}
}
---code ends---
Thanks
James
--
James Brister brister@decwrl.dec.com
DEC Western Software Lab., Palo Alto, CA {uunet,sun,pyramid}!decwrl!bristerlwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/06/90)
In article <BRISTER.90Nov4133946@westworld.decwrl.dec.com> brister@decwrl.dec.com (James Brister) writes:
: I'm writing a perl replacement for the lndir program that comes with X11.
: The code (which follows) does a recursive walk of the directory tree and
: seems to have two problems:
:
: 1) When the subroutine lndir hits the first leaf directory, the $file
: variable in the foreach loop doesn't get incremented and an infinite
: loops results.
You didn't localize @dirlist, but $dirlist, which is otherwise not used.
: 2) Does readdir reuire that an NFS file system be mount read-write? When I
: try this program with a read-only file system directory as the first
: argument, I only get the lost+found directory displayed in the "mkdir"
: line.
Shouldn't matter. Directories are effectively read-only all the time anyway.
Larry
P.S. Why the quotes around "DIR"?
: opendir ("DIR",$source) || die "Can't opendir $source" ;
: @dirlist = readdir ("DIR") ;
: closedir ("DIR") ;
opendir (DIR,$source) || die "Can't opendir $source" ;
@dirlist = readdir (DIR) ;
closedir (DIR) ;
will work just fine.