home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _25aab0ad7ac3f27ba368b360288abfa1 < prev    next >
Text File  |  2000-03-15  |  9KB  |  314 lines

  1. #
  2. # $Id: Listing.pm,v 1.11 1999/03/20 07:37:35 gisle Exp $
  3.  
  4. package File::Listing;
  5.  
  6. sub Version { $VERSION; }
  7. $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. =head1 NAME
  10.  
  11. parse_dir - parse directory listing
  12.  
  13. =head1 SYNOPSIS
  14.  
  15.  use File::Listing;
  16.  for (parse_dir(`ls -l`)) {
  17.      ($name, $type, $size, $mtime, $mode) = @$_;
  18.      next if $type ne 'f'; # plain file
  19.      #...
  20.  }
  21.  
  22.  # directory listing can also be read from a file
  23.  open(LISTING, "zcat ls-lR.gz|");
  24.  $dir = parse_dir(\*LISTING, '+0000');
  25.  
  26. =head1 DESCRIPTION
  27.  
  28. The parse_dir() routine can be used to parse directory
  29. listings. Currently it only understand Unix C<'ls -l'> and C<'ls -lR'>
  30. format.  It should eventually be able to most things you might get
  31. back from a ftp server file listing (LIST command), i.e. VMS listings,
  32. NT listings, DOS listings,...
  33.  
  34. The first parameter to parse_dir() is the directory listing to parse.
  35. It can be a scalar, a reference to an array of directory lines or a
  36. glob representing a filehandle to read the directory listing from.
  37.  
  38. The second parameter is the time zone to use when parsing time stamps
  39. in the listing. If this value is undefined, then the local time zone is
  40. assumed.
  41.  
  42. The third parameter is the type of listing to assume.  The values will
  43. be strings like 'unix', 'vms', 'dos'.  Currently only 'unix' is
  44. implemented and this is also the default value.  Ideally, the listing
  45. type should be determined automatically.
  46.  
  47. The fourth parameter specifies how unparseable lines should be treated.
  48. Values can be 'ignore', 'warn' or a code reference.  Warn means that
  49. the perl warn() function will be called.  If a code reference is
  50. passed, then this routine will be called and the return value from it
  51. will be incorporated in the listing.  The default is 'ignore'.
  52.  
  53. Only the first parameter is mandatory.
  54.  
  55. The return value from parse_dir() is a list of directory entries.  In
  56. a scalar context the return value is a reference to the list.  The
  57. directory entries are represented by an array consisting of [
  58. $filename, $filetype, $filesize, $filetime, $filemode ].  The
  59. $filetype value is one of the letters 'f', 'd', 'l' or '?'.  The
  60. $filetime value is the seconds since Jan 1, 1970.  The
  61. $filemode is a bitmask like the mode returned by stat().
  62.  
  63. =head1 CREDITS
  64.  
  65. Based on lsparse.pl (from Lee McLoughlin's ftp mirror package) and
  66. Net::FTP's parse_dir (Graham Barr).
  67.  
  68. =cut
  69.  
  70. require Exporter;
  71. @ISA = qw(Exporter);
  72.  
  73. @EXPORT = qw(parse_dir);
  74.  
  75. use strict;
  76.  
  77. use Carp ();
  78. use HTTP::Date qw(str2time);
  79.  
  80. sub parse_dir ($;$$$)
  81. {
  82.    my($dir, $tz, $fstype, $error) = @_;
  83.  
  84.    $fstype ||= 'unix';
  85.    $fstype = "File::Listing::" . lc $fstype;
  86.  
  87.    my @args = $_[0];
  88.    push(@args, $tz) if(@_ >= 2);
  89.    push(@args, $error) if(@_ >= 4);
  90.  
  91.    $fstype->parse(@args);
  92. }
  93.  
  94. sub line { Carp::croak("Not implemented yet"); }
  95. sub init { } # Dummy sub
  96.  
  97. sub file_mode ($)
  98. {
  99.     # This routine was originally borrowed from Graham Barr's
  100.     # Net::FTP package.
  101.  
  102.     local $_ = shift;
  103.     my $mode = 0;
  104.     my($type,$ch);
  105.  
  106.     s/^(.)// and $type = $1;
  107.  
  108.     while (/(.)/g) {
  109.     $mode <<= 1;
  110.     $mode |= 1 if $1 ne "-" &&
  111.               $1 ne 'S' &&
  112.               $1 ne 't' &&
  113.               $1 ne 'T';
  114.     }
  115.  
  116.     $type eq "d" and $mode |= 0040000 or    # Directory
  117.       $type eq "l" and $mode |= 0120000 or    # Symbolic Link
  118.     $mode |= 0100000;            # Regular File
  119.  
  120.     $mode |= 0004000 if /^...s....../i;
  121.     $mode |= 0002000 if /^......s.../i;
  122.     $mode |= 0001000 if /^.........t/i;
  123.  
  124.     $mode;
  125. }
  126.  
  127. sub parse
  128. {
  129.    my($pkg, $dir, $tz, $error) = @_;
  130.  
  131.    # First let's try to determine what kind of dir parameter we have
  132.    # received.  We allow both listings, reference to arrays and
  133.    # file handles to read from.
  134.  
  135.    if (ref($dir) eq 'ARRAY') {
  136.        # Already splitted up
  137.    } elsif (ref($dir) eq 'GLOB') {
  138.        # A file handle
  139.    } elsif (ref($dir)) {
  140.       Carp::croak("Illegal argument to parse_dir()");
  141.    } elsif ($dir =~ /^\*\w+(::\w+)+$/) {
  142.       # This scalar looks like a file handle, so we assume it is
  143.    } else {
  144.       # A normal scalar listing
  145.       $dir = [ split(/\n/, $dir) ];
  146.    }
  147.  
  148.    $pkg->init();
  149.  
  150.    my @files = ();
  151.    if (ref($dir) eq 'ARRAY') {
  152.        for (@$dir) {
  153.        push(@files, $pkg->line($_, $tz, $error));
  154.        }
  155.    } else {
  156.        local($_);
  157.        while (<$dir>) {
  158.        chomp;
  159.        push(@files, $pkg->line($_, $tz, $error));
  160.        }
  161.    }
  162.    wantarray ? @files : \@files;
  163. }
  164.  
  165.  
  166. package File::Listing::unix;
  167.  
  168. use HTTP::Date qw(str2time);
  169.  
  170. # A place to remember current directory from last line parsed.
  171. use vars qw($curdir);
  172. no strict qw(vars);
  173.  
  174. @ISA = qw(File::Listing);
  175.  
  176.  
  177. sub init
  178. {
  179.     $curdir = '';
  180. }
  181.  
  182. sub line
  183. {
  184.     shift; # package name
  185.     local($_) = shift;
  186.     my($tz, $error) = @_;
  187.  
  188.     s/\015//g;
  189.     #study;
  190.  
  191.     my ($kind, $size, $date, $name);
  192.     if (($kind, $size, $date, $name) =
  193.     /^([\-FlrwxsStTdD]{10})                   # Type and permission bits
  194.      .*                                       # Graps
  195.      \D(\d+)                                  # File size
  196.      \s+                                      # Some space
  197.      (\w{3}\s+\d+\s+(?:\d{1,2}:\d{2}|\d{4}))  # Date
  198.      \s+                                      # Some more space
  199.      (.*)$                                    # File name
  200.     /x )
  201.  
  202.     {
  203.     return if $name eq '.' || $name eq '..';
  204.     $name = "$curdir/$name" if length $curdir;
  205.     my $type = '?';
  206.     if ($kind =~ /^l/ && $name =~ /(.*) -> (.*)/ ) {
  207.         $name = $1;
  208.         $type = "l $2";
  209.     } elsif ($kind =~ /^[\-F]/) { # (hopefully) a regular file
  210.         $type = 'f';
  211.     } elsif ($kind =~ /^[dD]/) {
  212.         $type = 'd';
  213.         $size = undef;  # Don't believe the reported size
  214.     }
  215.     return [$name, $type, $size, str2time($date, $tz), 
  216.               File::Listing::file_mode($kind)];
  217.  
  218.     } elsif (/^(.+):$/ && !/^[dcbsp].*\s.*\s.*:$/ ) {
  219.     my $dir = $1;
  220.     return () if $dir eq '.';
  221.     $curdir = $dir;
  222.     return ();
  223.     } elsif (/^[Tt]otal\s+(\d+)$/ || /^\s*$/) {
  224.     return ();
  225.     } elsif (/not found/    || # OSF1, HPUX, and SunOS return
  226.              # "$file not found"
  227.              /No such file/ || # IRIX returns
  228.              # "UX:ls: ERROR: Cannot access $file: No such file or directory"
  229.                                # Solaris returns
  230.              # "$file: No such file or directory"
  231.              /cannot find/     # Windows NT returns
  232.              # "The system cannot find the path specified."
  233.              ) {
  234.     return () unless defined $error;
  235.     &$error($_) if ref($error) eq 'CODE';
  236.     warn "Error: $_\n" if $error eq 'warn';
  237.     return ();
  238.     } elsif ($_ eq '') {       # AIX, and Linux return nothing
  239.     return () unless defined $error;
  240.     &$error("No such file or directory") if ref($error) eq 'CODE';
  241.     warn "Warning: No such file or directory\n" if $error eq 'warn';
  242.     return ();
  243.     } else {
  244.         # parse failed, check if the dosftp parse understands it
  245.         return(File::Listing::dosftp->line($_,$tz,$error));
  246.     }
  247.  
  248. }
  249.  
  250. package File::Listing::dosftp;
  251.  
  252. use HTTP::Date qw(str2time);
  253.  
  254. # A place to remember current directory from last line parsed.
  255. use vars qw($curdir);
  256. no strict qw(vars);
  257.  
  258. @ISA = qw(File::Listing);
  259.  
  260. sub init
  261. {
  262.     $curdir = '';
  263. }
  264.  
  265. sub line
  266. {
  267.     shift; # package name
  268.     local($_) = shift;
  269.     my($tz, $error) = @_;
  270.  
  271.     s/\015//g;
  272.  
  273.     my ($kind, $size, $date, $name);
  274.  
  275.     # 02-05-96  10:48AM                 1415 src.slf
  276.     # 09-10-96  09:18AM       <DIR>          sl_util
  277.     if (($date,$size_or_dir,$name) =
  278.         /^(\d\d-\d\d-\d\d\s+\d\d:\d\d\wM)         # Date and time info
  279.          \s+                                      # Some space
  280.          (<\w{3}>|\d+)                            # Dir or Size
  281.          \s+                                      # Some more space
  282.          (.+)$                                    # File name
  283.         /x )
  284.     {
  285.     return if $name eq '.' || $name eq '..';
  286.     $name = "$curdir/$name" if length $curdir;
  287.     my $type = '?';
  288.     if ($size_or_dir eq '<DIR>') {
  289.         $type = "d";
  290.             $size = ""; # directories have no size in the pc listing
  291.         } else {
  292.         $type = 'f';
  293.             $size = $size_or_dir;
  294.     }
  295.     return [$name, $type, $size, str2time($date, $tz),
  296.               File::Listing::file_mode($kind)];
  297.  
  298.     } else {
  299.     return () unless defined $error;
  300.     &$error($_) if ref($error) eq 'CODE';
  301.     warn "Can't parse: $_\n" if $error eq 'warn';
  302.     return ();
  303.     }
  304.  
  305. }
  306.  
  307. package File::Listing::vms;
  308. @File::Listing::unix::ISA = qw(File::Listing);
  309.  
  310. package File::Listing::netware;
  311. @File::Listing::unix::ISA = qw(File::Listing);
  312.  
  313. 1;
  314.