#!/usr/bin/perl -w ## ## tree.pl ## A quickie hack to display a directory tree, either as HTML or not. ## Sat May 26 15:09:20 EDT 2001 ## ## (C) 2001 Dave O'Neill ## You may use and distribute this under the terms of the GPL, version ## 2 or later. ## use strict; use Getopt::Long; my $parent_dir = '.'; my $want_size = 0; my $want_date = 0; my $want_html = 0; my $only_dirs = 0; ## Only make sense with -h or --html my $html_base; #= 'http://www.evilplot.org/'; my $doc_root; # = '/www/evilplot/'; Getopt::Long::Configure ("bundling"); GetOptions( "directory|d" => \$only_dirs, "showsize|s" => \$want_size, "showdate" => \$want_date, "html|h" => \$want_html, "base=s" => \$html_base, "docroot=s" => \$doc_root, ); if (@ARGV > 1) { die "Usage: $0 [options] [directory]\n"; } elsif (@ARGV == 1) { $parent_dir = shift; } if($want_html) { print ""; print "
\n";
}

print "$parent_dir\n"; 

recursive_print_directory($parent_dir,0,0); 

if($want_html) {
	print "\n
\n"; print "\n"; } sub recursive_print_directory { my ($this_dir,$x,$v) = @_; opendir (ARGV, $this_dir) or warn "Can't read $this_dir: $!"; my @dir_entries = sort readdir ARGV; closedir ARGV; my $last_entry = $dir_entries[-1]; if($only_dirs) { ## Ensure that the last entry is a directory. my $i = -1; while(! -d "$this_dir/$dir_entries[$i]") { $i--; } $last_entry = $dir_entries[$i]; } for(@dir_entries) { next if ($_ eq "." || $_ eq ".."); my $full_path = "$this_dir/$_"; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($full_path); next if ($only_dirs && !-d _); my $printable = $_; if($want_html) { my $html_full = $full_path; $html_full =~ s/^$doc_root/$html_base/og; $printable = "$_"; } print map {$_ ? "| ":" "} (split(//, unpack("b*", $v)))[0..($x - 1)]; print $_ eq $last_entry ? '`' : '|', "-- $printable"; if($want_date) { print " [",scalar(localtime($ctime)),"]" unless -l _; } if($want_size) { if($size >= 1024) { $size = int($size / 1024) . 'k'; } else { $size .= 'b'; } print " ($size)" unless -l _; } if(-l _) { my $ln = readlink($full_path); print " -> ", -e $ln ?"$ln" : "($ln)"; lstat($full_path); } print "\n"; if(-d _ && !-l _){ vec($v,$x,1) = !($_ eq $last_entry); recursive_print_directory($full_path,$x+1,$v); } } }