Path: utzoo!attcan!uunet!jarthur!usc!zaphod.mps.ohio-state.edu!wuarchive!udel!haven!decuac!bacchus.pa.dec.com!decwrl!brister From: brister@decwrl.dec.com (James Brister) Newsgroups: comp.lang.perl Subject: Recursion problem Message-ID: Date: 4 Nov 90 18:39:46 GMT Sender: news@wrl.dec.com (News) Organization: DEC Western Software Lab Lines: 67 X-Checksum-Snefru: 3516ae01 b86d1121 1fed07c1 8f81b3e1 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!brister