home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / LINUX / ARCHIVE / SL.Z / SL / sbin / sl
Encoding:
Text File  |  1993-01-01  |  1.9 KB  |  82 lines

  1. #!///////////////////////////////////////////////////////////////////////////usr/STAGE/bin/perl
  2.  
  3. # Usage: sl [filenames]
  4.  
  5. die "Usage: sl [filenames]\n" unless @ARGV;
  6.  
  7. # Preliminaries.
  8.  
  9. $| = 1;
  10. chop($cwd = `pwd`) || die "Can't find current directory: $!\n"
  11.     if @ARGV > 1;
  12. print "\n";
  13.  
  14. # Do each name.
  15.  
  16. foreach $name (@ARGV) {
  17.     @indent = ();
  18.     print "$name:\n";
  19.     @path = split(m;/;, $name);
  20.  
  21.     # Make an absolute path relative to /.
  22.  
  23.     if (@path && $path[0] eq '') {
  24.         chdir '/';
  25.         shift @path;
  26.         print '/';
  27.         $indent = 1;
  28.     }
  29.     # Now follow the subdirectories and links.
  30.  
  31.     while (@path) {
  32.         $elem = shift @path;
  33.         $new = readlink($elem);
  34.         if (defined $new) {     # A symbolic link.
  35.             print "$elem \-> $new\n";
  36.             $new =~ s!^\./!!;
  37.  
  38.             # Prepend symbolic link to rest of path.
  39.  
  40.             unshift(@path,split(m;/;, $new));
  41.  
  42.             # Deal with special cases.
  43.  
  44.             if (@path && $path[0] eq '') {
  45.  
  46.                 # Absolute path starts over.
  47.  
  48.                 chdir '/';
  49.                 shift @path;
  50.                 print '/';
  51.                 $indent = 1;
  52.                 @indents = ();
  53.                 next;
  54.             }
  55.  
  56.             # Back up the tree as necessary.
  57.  
  58.             while (@indents && $path[0] eq '..') {
  59.                 $indent = pop(@indents);
  60.                 chdir '..'
  61.                     || die "\n\nCan't cd to ..: $!\n";
  62.                 shift @path;
  63.             }
  64.  
  65.             print "\t" x ($indent / 8), ' ' x ($indent % 8);
  66.         }
  67.         else {                  # An ordinary directory.
  68.             print $elem;
  69.             push(@indents,$indent);
  70.             $indent += length($elem) + 1;
  71.             if (@path) {
  72.                 print '/';
  73.                 chdir $elem
  74.                     || die "\n\nCan't cd to $elem: $!\n";
  75.             }
  76.         }
  77.     }
  78.     print "\n\n";
  79.     $indent = 0;
  80.     chdir $cwd || die "Can't cd back: $!\n" if $cwd ne '';
  81. }
  82.