home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / lib / Net / FTP.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-19  |  41.4 KB  |  1,716 lines

  1. # Net::FTP.pm
  2. #
  3. # Copyright (c) 1995-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. #
  7. # Documentation (at end) improved 1996 by Nathan Torkington <gnat@frii.com>.
  8.  
  9. package Net::FTP;
  10.  
  11. require 5.001;
  12.  
  13. use strict;
  14. use vars qw(@ISA $VERSION);
  15. use Carp;
  16.  
  17. use Socket 1.3;
  18. use IO::Socket;
  19. use Time::Local;
  20. use Net::Cmd;
  21. use Net::Config;
  22. use Fcntl qw(O_WRONLY O_RDONLY O_APPEND O_CREAT O_TRUNC);
  23. # use AutoLoader qw(AUTOLOAD);
  24.  
  25. $VERSION = "2.65"; # $Id: //depot/libnet/Net/FTP.pm#68 $
  26. @ISA     = qw(Exporter Net::Cmd IO::Socket::INET);
  27.  
  28. # Someday I will "use constant", when I am not bothered to much about
  29. # compatability with older releases of perl
  30.  
  31. use vars qw($TELNET_IAC $TELNET_IP $TELNET_DM);
  32. ($TELNET_IAC,$TELNET_IP,$TELNET_DM) = (255,244,242);
  33.  
  34. # Name is too long for AutoLoad, it clashes with pasv_xfer
  35. sub pasv_xfer_unique {
  36.     my($sftp,$sfile,$dftp,$dfile) = @_;
  37.     $sftp->pasv_xfer($sfile,$dftp,$dfile,1);
  38. }
  39.  
  40. BEGIN {
  41.   # make a constant so code is fast'ish
  42.   my $is_os390 = $^O eq 'os390';
  43.   *trEBCDIC = sub () { $is_os390 }
  44. }
  45.  
  46. 1;
  47. # Having problems with AutoLoader
  48. #__END__
  49.  
  50. sub new
  51. {
  52.  my $pkg  = shift;
  53.  my $peer = shift;
  54.  my %arg  = @_; 
  55.  
  56.  my $host = $peer;
  57.  my $fire = undef;
  58.  my $fire_type = undef;
  59.  
  60.  if(exists($arg{Firewall}) || Net::Config->requires_firewall($peer))
  61.   {
  62.    $fire = $arg{Firewall}
  63.     || $ENV{FTP_FIREWALL}
  64.     || $NetConfig{ftp_firewall}
  65.     || undef;
  66.  
  67.    if(defined $fire)
  68.     {
  69.      $peer = $fire;
  70.      delete $arg{Port};
  71.      $fire_type = $arg{FirewallType}
  72.      || $ENV{FTP_FIREWALL_TYPE}
  73.      || undef;
  74.     }
  75.   }
  76.  
  77.  my $ftp = $pkg->SUPER::new(PeerAddr => $peer, 
  78.                 PeerPort => $arg{Port} || 'ftp(21)',
  79.                 Proto    => 'tcp',
  80.                 Timeout  => defined $arg{Timeout}
  81.                         ? $arg{Timeout}
  82.                         : 120
  83.                ) or return undef;
  84.  
  85.  ${*$ftp}{'net_ftp_host'}     = $host;        # Remote hostname
  86.  ${*$ftp}{'net_ftp_type'}     = 'A';        # ASCII/binary/etc mode
  87.  ${*$ftp}{'net_ftp_blksize'}  = abs($arg{'BlockSize'} || 10240);
  88.  
  89.  ${*$ftp}{'net_ftp_firewall'} = $fire
  90.     if(defined $fire);
  91.  ${*$ftp}{'net_ftp_firewall_type'} = $fire_type
  92.     if(defined $fire_type);
  93.  
  94.  ${*$ftp}{'net_ftp_passive'} = int
  95.     exists $arg{Passive}
  96.         ? $arg{Passive}
  97.         : exists $ENV{FTP_PASSIVE}
  98.         ? $ENV{FTP_PASSIVE}
  99.         : defined $fire
  100.             ? $NetConfig{ftp_ext_passive}
  101.             : $NetConfig{ftp_int_passive};    # Whew! :-)
  102.  
  103.  $ftp->hash(exists $arg{Hash} ? $arg{Hash} : 0, 1024);
  104.  
  105.  $ftp->autoflush(1);
  106.  
  107.  $ftp->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  108.  
  109.  unless ($ftp->response() == CMD_OK)
  110.   {
  111.    $ftp->close();
  112.    $@ = $ftp->message;
  113.    undef $ftp;
  114.   }
  115.  
  116.  $ftp;
  117. }
  118.  
  119. ##
  120. ## User interface methods
  121. ##
  122.  
  123. sub hash {
  124.     my $ftp = shift;        # self
  125.  
  126.     my($h,$b) = @_;
  127.     unless($h) {
  128.       delete ${*$ftp}{'net_ftp_hash'};
  129.       return [\*STDERR,0];
  130.     }
  131.     ($h,$b) = (ref($h)? $h : \*STDERR, $b || 1024);
  132.     select((select($h), $|=1)[0]);
  133.     $b = 512 if $b < 512;
  134.     ${*$ftp}{'net_ftp_hash'} = [$h, $b];
  135. }        
  136.  
  137. sub quit
  138. {
  139.  my $ftp = shift;
  140.  
  141.  $ftp->_QUIT;
  142.  $ftp->close;
  143. }
  144.  
  145. sub DESTROY {}
  146.  
  147. sub ascii  { shift->type('A',@_); }
  148. sub binary { shift->type('I',@_); }
  149.  
  150. sub ebcdic
  151. {
  152.  carp "TYPE E is unsupported, shall default to I";
  153.  shift->type('E',@_);
  154. }
  155.  
  156. sub byte
  157. {
  158.  carp "TYPE L is unsupported, shall default to I";
  159.  shift->type('L',@_);
  160. }
  161.  
  162. # Allow the user to send a command directly, BE CAREFUL !!
  163.  
  164. sub quot
  165.  my $ftp = shift;
  166.  my $cmd = shift;
  167.  
  168.  $ftp->command( uc $cmd, @_);
  169.  $ftp->response();
  170. }
  171.  
  172. sub site
  173. {
  174.  my $ftp = shift;
  175.  
  176.  $ftp->command("SITE", @_);
  177.  $ftp->response();
  178. }
  179.  
  180. sub mdtm
  181. {
  182.  my $ftp  = shift;
  183.  my $file = shift;
  184.  
  185.  # Server Y2K bug workaround
  186.  #
  187.  # sigh; some idiotic FTP servers use ("19%d",tm.tm_year) instead of 
  188.  # ("%d",tm.tm_year+1900).  This results in an extra digit in the
  189.  # string returned. To account for this we allow an optional extra
  190.  # digit in the year. Then if the first two digits are 19 we use the
  191.  # remainder, otherwise we subtract 1900 from the whole year.
  192.  
  193.  $ftp->_MDTM($file) && $ftp->message =~ /((\d\d)(\d\d\d?))(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/
  194.     ? timegm($8,$7,$6,$5,$4-1,$2 eq '19' ? $3 : ($1-1900))
  195.     : undef;
  196. }
  197.  
  198. sub size {
  199.   my $ftp  = shift;
  200.   my $file = shift;
  201.   my $io;
  202.   if($ftp->supported("SIZE")) {
  203.     return $ftp->_SIZE($file)
  204.     ? ($ftp->message =~ /(\d+)$/)[0]
  205.     : undef;
  206.  }
  207.  elsif($ftp->supported("STAT")) {
  208.    my @msg;
  209.    return undef
  210.        unless $ftp->_STAT($file) && (@msg = $ftp->message) == 3;
  211.    my $line;
  212.    foreach $line (@msg) {
  213.      return (split(/\s+/,$line))[4]
  214.      if $line =~ /^[-rwx]{10}/
  215.    }
  216.  }
  217.  else {
  218.    my @files = $ftp->dir($file);
  219.    if(@files) {
  220.      return (split(/\s+/,$1))[4]
  221.      if $files[0] =~ /^([-rwx]{10}.*)$/;
  222.    }
  223.  }
  224.  undef;
  225. }
  226.  
  227. sub login {
  228.   my($ftp,$user,$pass,$acct) = @_;
  229.   my($ok,$ruser,$fwtype);
  230.  
  231.   unless (defined $user) {
  232.     require Net::Netrc;
  233.  
  234.     my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_host'});
  235.  
  236.     ($user,$pass,$acct) = $rc->lpa()
  237.      if ($rc);
  238.    }
  239.  
  240.   $user ||= "anonymous";
  241.   $ruser = $user;
  242.  
  243.   $fwtype = ${*$ftp}{'net_ftp_firewall_type'}
  244.   || $NetConfig{'ftp_firewall_type'}
  245.   || 0;
  246.  
  247.   if ($fwtype && defined ${*$ftp}{'net_ftp_firewall'}) {
  248.     if ($fwtype == 1 || $fwtype == 7) {
  249.       $user .= '@' . ${*$ftp}{'net_ftp_host'};
  250.     }
  251.     else {
  252.       require Net::Netrc;
  253.  
  254.       my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_firewall'});
  255.  
  256.       my($fwuser,$fwpass,$fwacct) = $rc ? $rc->lpa() : ();
  257.  
  258.       if ($fwtype == 5) {
  259.     $user = join('@',$user,$fwuser,${*$ftp}{'net_ftp_host'});
  260.     $pass = $pass . '@' . $fwpass;
  261.       }
  262.       else {
  263.     if ($fwtype == 2) {
  264.       $user .= '@' . ${*$ftp}{'net_ftp_host'};
  265.     }
  266.     elsif ($fwtype == 6) {
  267.       $fwuser .= '@' . ${*$ftp}{'net_ftp_host'};
  268.     }
  269.  
  270.     $ok = $ftp->_USER($fwuser);
  271.  
  272.     return 0 unless $ok == CMD_OK || $ok == CMD_MORE;
  273.  
  274.     $ok = $ftp->_PASS($fwpass || "");
  275.  
  276.     return 0 unless $ok == CMD_OK || $ok == CMD_MORE;
  277.  
  278.     $ok = $ftp->_ACCT($fwacct)
  279.       if defined($fwacct);
  280.  
  281.     if ($fwtype == 3) {
  282.           $ok = $ftp->command("SITE",${*$ftp}{'net_ftp_host'})->response;
  283.     }
  284.     elsif ($fwtype == 4) {
  285.           $ok = $ftp->command("OPEN",${*$ftp}{'net_ftp_host'})->response;
  286.     }
  287.  
  288.     return 0 unless $ok == CMD_OK || $ok == CMD_MORE;
  289.       }
  290.     }
  291.   }
  292.  
  293.   $ok = $ftp->_USER($user);
  294.  
  295.   # Some dumb firewalls don't prefix the connection messages
  296.   $ok = $ftp->response()
  297.      if ($ok == CMD_OK && $ftp->code == 220 && $user =~ /\@/);
  298.  
  299.   if ($ok == CMD_MORE) {
  300.     unless(defined $pass) {
  301.       require Net::Netrc;
  302.  
  303.       my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_host'}, $ruser);
  304.  
  305.       ($ruser,$pass,$acct) = $rc->lpa()
  306.      if ($rc);
  307.  
  308.       $pass = '-anonymous@'
  309.          if (!defined $pass && (!defined($ruser) || $ruser =~ /^anonymous/o));
  310.     }
  311.  
  312.     $ok = $ftp->_PASS($pass || "");
  313.   }
  314.  
  315.   $ok = $ftp->_ACCT($acct)
  316.      if (defined($acct) && ($ok == CMD_MORE || $ok == CMD_OK));
  317.  
  318.   if ($fwtype == 7 && $ok == CMD_OK && defined ${*$ftp}{'net_ftp_firewall'}) {
  319.     my($f,$auth,$resp) = _auth_id($ftp);
  320.     $ftp->authorize($auth,$resp) if defined($resp);
  321.   }
  322.  
  323.   $ok == CMD_OK;
  324. }
  325.  
  326. sub account
  327. {
  328.  @_ == 2 or croak 'usage: $ftp->account( ACCT )';
  329.  my $ftp = shift;
  330.  my $acct = shift;
  331.  $ftp->_ACCT($acct) == CMD_OK;
  332. }
  333.  
  334. sub _auth_id {
  335.  my($ftp,$auth,$resp) = @_;
  336.  
  337.  unless(defined $resp)
  338.   {
  339.    require Net::Netrc;
  340.  
  341.    $auth ||= eval { (getpwuid($>))[0] } || $ENV{NAME};
  342.  
  343.    my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_firewall'}, $auth)
  344.         || Net::Netrc->lookup(${*$ftp}{'net_ftp_firewall'});
  345.  
  346.    ($auth,$resp) = $rc->lpa()
  347.      if ($rc);
  348.   }
  349.   ($ftp,$auth,$resp);
  350. }
  351.  
  352. sub authorize
  353. {
  354.  @_ >= 1 || @_ <= 3 or croak 'usage: $ftp->authorize( [AUTH [, RESP]])';
  355.  
  356.  my($ftp,$auth,$resp) = &_auth_id;
  357.  
  358.  my $ok = $ftp->_AUTH($auth || "");
  359.  
  360.  $ok = $ftp->_RESP($resp || "")
  361.     if ($ok == CMD_MORE);
  362.  
  363.  $ok == CMD_OK;
  364. }
  365.  
  366. sub rename
  367. {
  368.  @_ == 3 or croak 'usage: $ftp->rename(FROM, TO)';
  369.  
  370.  my($ftp,$from,$to) = @_;
  371.  
  372.  $ftp->_RNFR($from)
  373.     && $ftp->_RNTO($to);
  374. }
  375.  
  376. sub type
  377. {
  378.  my $ftp = shift;
  379.  my $type = shift;
  380.  my $oldval = ${*$ftp}{'net_ftp_type'};
  381.  
  382.  return $oldval
  383.     unless (defined $type);
  384.  
  385.  return undef
  386.     unless ($ftp->_TYPE($type,@_));
  387.  
  388.  ${*$ftp}{'net_ftp_type'} = join(" ",$type,@_);
  389.  
  390.  $oldval;
  391. }
  392.  
  393. sub abort
  394. {
  395.  my $ftp = shift;
  396.  
  397.  send($ftp,pack("CCC", $TELNET_IAC, $TELNET_IP, $TELNET_IAC),MSG_OOB);
  398.  
  399.  $ftp->command(pack("C",$TELNET_DM) . "ABOR");
  400.  
  401.  ${*$ftp}{'net_ftp_dataconn'}->close()
  402.     if defined ${*$ftp}{'net_ftp_dataconn'};
  403.  
  404.  $ftp->response();
  405.  
  406.  $ftp->status == CMD_OK;
  407. }
  408.  
  409. sub get
  410. {
  411.  my($ftp,$remote,$local,$where) = @_;
  412.  
  413.  my($loc,$len,$buf,$resp,$data);
  414.  local *FD;
  415.  
  416.  my $localfd = ref($local) || ref(\$local) eq "GLOB";
  417.  
  418.  ($local = $remote) =~ s#^.*/##
  419.     unless(defined $local);
  420.  
  421.  croak("Bad remote filename '$remote'\n")
  422.     if $remote =~ /[\r\n]/s;
  423.  
  424.  ${*$ftp}{'net_ftp_rest'} = $where
  425.     if ($where);
  426.  
  427.  delete ${*$ftp}{'net_ftp_port'};
  428.  delete ${*$ftp}{'net_ftp_pasv'};
  429.  
  430.  $data = $ftp->retr($remote) or
  431.     return undef;
  432.  
  433.  if($localfd)
  434.   {
  435.    $loc = $local;
  436.   }
  437.  else
  438.   {
  439.    $loc = \*FD;
  440.  
  441.    unless(sysopen($loc, $local, O_CREAT | O_WRONLY | ($where ? O_APPEND : O_TRUNC)))
  442.     {
  443.      carp "Cannot open Local file $local: $!\n";
  444.      $data->abort;
  445.      return undef;
  446.     }
  447.   }
  448.  
  449.  if($ftp->type eq 'I' && !binmode($loc))
  450.   {
  451.    carp "Cannot binmode Local file $local: $!\n";
  452.    $data->abort;
  453.    close($loc) unless $localfd;
  454.    return undef;
  455.   }
  456.  
  457.  $buf = '';
  458.  my($count,$hashh,$hashb,$ref) = (0);
  459.  
  460.  ($hashh,$hashb) = @$ref
  461.    if($ref = ${*$ftp}{'net_ftp_hash'});
  462.  
  463.  my $blksize = ${*$ftp}{'net_ftp_blksize'};
  464.  
  465.  while(1)
  466.   {
  467.    last unless $len = $data->read($buf,$blksize);
  468.  
  469.    if (trEBCDIC && $ftp->type ne 'I')
  470.     {
  471.      $buf = $ftp->toebcdic($buf);
  472.      $len = length($buf);
  473.     }
  474.  
  475.    if($hashh) {
  476.     $count += $len;
  477.     print $hashh "#" x (int($count / $hashb));
  478.     $count %= $hashb;
  479.    }
  480.    my $written = syswrite($loc,$buf,$len);
  481.    unless(defined($written) && $written == $len)
  482.     {
  483.      carp "Cannot write to Local file $local: $!\n";
  484.      $data->abort;
  485.      close($loc)
  486.         unless $localfd;
  487.      return undef;
  488.     }
  489.   }
  490.  
  491.  print $hashh "\n" if $hashh;
  492.  
  493.  unless ($localfd)
  494.   {
  495.    unless (close($loc))
  496.     {
  497.      carp "Cannot close file $local (perhaps disk space) $!\n";
  498.      return undef;
  499.     }
  500.   }
  501.  
  502.  unless ($data->close()) # implied $ftp->response
  503.   {
  504.    carp "Unable to close datastream";
  505.    return undef;
  506.   }
  507.  
  508.  return $local;
  509. }
  510.  
  511. sub cwd
  512. {
  513.  @_ == 1 || @_ == 2 or croak 'usage: $ftp->cwd( [ DIR ] )';
  514.  
  515.  my($ftp,$dir) = @_;
  516.  
  517.  $dir = "/" unless defined($dir) && $dir =~ /\S/;
  518.  
  519.  $dir eq ".."
  520.     ? $ftp->_CDUP()
  521.     : $ftp->_CWD($dir);
  522. }
  523.  
  524. sub cdup
  525. {
  526.  @_ == 1 or croak 'usage: $ftp->cdup()';
  527.  $_[0]->_CDUP;
  528. }
  529.  
  530. sub pwd
  531. {
  532.  @_ == 1 || croak 'usage: $ftp->pwd()';
  533.  my $ftp = shift;
  534.  
  535.  $ftp->_PWD();
  536.  $ftp->_extract_path;
  537. }
  538.  
  539. # rmdir( $ftp, $dir, [ $recurse ] )
  540. #
  541. # Removes $dir on remote host via FTP.
  542. # $ftp is handle for remote host
  543. #
  544. # If $recurse is TRUE, the directory and deleted recursively.
  545. # This means all of its contents and subdirectories.
  546. #
  547. # Initial version contributed by Dinkum Software
  548. #
  549. sub rmdir
  550. {
  551.     @_ == 2 || @_ == 3 or croak('usage: $ftp->rmdir( DIR [, RECURSE ] )');
  552.  
  553.     # Pick off the args
  554.     my ($ftp, $dir, $recurse) = @_ ;
  555.     my $ok;
  556.  
  557.     return $ok
  558.     if $ok = $ftp->_RMD( $dir ) or !$recurse;
  559.  
  560.     # Try to delete the contents
  561.     # Get a list of all the files in the directory
  562.     my $filelist = $ftp->ls($dir);
  563.  
  564.     return undef
  565.     unless $filelist && @$filelist; # failed, it is probably not a directory
  566.  
  567.     # Go thru and delete each file or the directory
  568.     my $file;
  569.     foreach $file (map { m,/, ? $_ : "$dir/$_" } @$filelist)
  570.     {
  571.     next  # successfully deleted the file
  572.         if $ftp->delete($file);
  573.  
  574.     # Failed to delete it, assume its a directory
  575.     # Recurse and ignore errors, the final rmdir() will
  576.     # fail on any errors here
  577.     return $ok
  578.         unless $ok = $ftp->rmdir($file, 1) ;
  579.     }
  580.  
  581.     # Directory should be empty
  582.     # Try to remove the directory again
  583.     # Pass results directly to caller
  584.     # If any of the prior deletes failed, this
  585.     # rmdir() will fail because directory is not empty
  586.     return $ftp->_RMD($dir) ;
  587. }
  588.  
  589. sub restart
  590. {
  591.   @_ == 2 || croak 'usage: $ftp->restart( BYTE_OFFSET )';
  592.  
  593.   my($ftp,$where) = @_;
  594.  
  595.   ${*$ftp}{'net_ftp_rest'} = $where;
  596.  
  597.   return undef;
  598. }
  599.  
  600.  
  601. sub mkdir
  602. {
  603.  @_ == 2 || @_ == 3 or croak 'usage: $ftp->mkdir( DIR [, RECURSE ] )';
  604.  
  605.  my($ftp,$dir,$recurse) = @_;
  606.  
  607.  $ftp->_MKD($dir) || $recurse or
  608.     return undef;
  609.  
  610.  my $path = $dir;
  611.  
  612.  unless($ftp->ok)
  613.   {
  614.    my @path = split(m#(?=/+)#, $dir);
  615.  
  616.    $path = "";
  617.  
  618.    while(@path)
  619.     {
  620.      $path .= shift @path;
  621.  
  622.      $ftp->_MKD($path);
  623.  
  624.      $path = $ftp->_extract_path($path);
  625.     }
  626.  
  627.    # If the creation of the last element was not sucessful, see if we
  628.    # can cd to it, if so then return path
  629.  
  630.    unless($ftp->ok)
  631.     {
  632.      my($status,$message) = ($ftp->status,$ftp->message);
  633.      my $pwd = $ftp->pwd;
  634.  
  635.      if($pwd && $ftp->cwd($dir))
  636.       {
  637.        $path = $dir;
  638.        $ftp->cwd($pwd);
  639.       }
  640.      else
  641.       {
  642.        undef $path;
  643.       }
  644.      $ftp->set_status($status,$message);
  645.     }
  646.   }
  647.  
  648.  $path;
  649. }
  650.  
  651. sub delete
  652. {
  653.  @_ == 2 || croak 'usage: $ftp->delete( FILENAME )';
  654.  
  655.  $_[0]->_DELE($_[1]);
  656. }
  657.  
  658. sub put        { shift->_store_cmd("stor",@_) }
  659. sub put_unique { shift->_store_cmd("stou",@_) }
  660. sub append     { shift->_store_cmd("appe",@_) }
  661.  
  662. sub nlst { shift->_data_cmd("NLST",@_) }
  663. sub list { shift->_data_cmd("LIST",@_) }
  664. sub retr { shift->_data_cmd("RETR",@_) }
  665. sub stor { shift->_data_cmd("STOR",@_) }
  666. sub stou { shift->_data_cmd("STOU",@_) }
  667. sub appe { shift->_data_cmd("APPE",@_) }
  668.  
  669. sub _store_cmd 
  670. {
  671.  my($ftp,$cmd,$local,$remote) = @_;
  672.  my($loc,$sock,$len,$buf);
  673.  local *FD;
  674.  
  675.  my $localfd = ref($local) || ref(\$local) eq "GLOB";
  676.  
  677.  unless(defined $remote)
  678.   {
  679.    croak 'Must specify remote filename with stream input'
  680.     if $localfd;
  681.  
  682.    require File::Basename;
  683.    $remote = File::Basename::basename($local);
  684.   }
  685.  
  686.  croak("Bad remote filename '$remote'\n")
  687.     if $remote =~ /[\r\n]/s;
  688.  
  689.  if($localfd)
  690.   {
  691.    $loc = $local;
  692.   }
  693.  else
  694.   {
  695.    $loc = \*FD;
  696.  
  697.    unless(sysopen($loc, $local, O_RDONLY))
  698.     {
  699.      carp "Cannot open Local file $local: $!\n";
  700.      return undef;
  701.     }
  702.   }
  703.  
  704.  if($ftp->type eq 'I' && !binmode($loc))
  705.   {
  706.    carp "Cannot binmode Local file $local: $!\n";
  707.    return undef;
  708.   }
  709.  
  710.  delete ${*$ftp}{'net_ftp_port'};
  711.  delete ${*$ftp}{'net_ftp_pasv'};
  712.  
  713.  $sock = $ftp->_data_cmd($cmd, $remote) or 
  714.     return undef;
  715.  
  716.  my $blksize = ${*$ftp}{'net_ftp_blksize'};
  717.  
  718.  my($count,$hashh,$hashb,$ref) = (0);
  719.  
  720.  ($hashh,$hashb) = @$ref
  721.    if($ref = ${*$ftp}{'net_ftp_hash'});
  722.  
  723.  while(1)
  724.   {
  725.    last unless $len = sysread($loc,$buf="",$blksize);
  726.  
  727.    if (trEBCDIC && $ftp->type ne 'I')
  728.     {
  729.      $buf = $ftp->toascii($buf); 
  730.      $len = length($buf);
  731.     }
  732.  
  733.    if($hashh) {
  734.     $count += $len;
  735.     print $hashh "#" x (int($count / $hashb));
  736.     $count %= $hashb;
  737.    }
  738.  
  739.    my $wlen;
  740.    unless(defined($wlen = $sock->write($buf,$len)) && $wlen == $len)
  741.     {
  742.      $sock->abort;
  743.      close($loc)
  744.     unless $localfd;
  745.      print $hashh "\n" if $hashh;
  746.      return undef;
  747.     }
  748.   }
  749.  
  750.  print $hashh "\n" if $hashh;
  751.  
  752.  close($loc)
  753.     unless $localfd;
  754.  
  755.  $sock->close() or
  756.     return undef;
  757.  
  758.  if ('STOU' eq uc $cmd and $ftp->message =~ m/unique\s+file\s*name\s*:\s*(.*)\)|"(.*)"/)
  759.   {
  760.    require File::Basename;
  761.    $remote = File::Basename::basename($+) 
  762.   }
  763.  
  764.  return $remote;
  765. }
  766.  
  767. sub port
  768. {
  769.  @_ == 1 || @_ == 2 or croak 'usage: $ftp->port([PORT])';
  770.  
  771.  my($ftp,$port) = @_;
  772.  my $ok;
  773.  
  774.  delete ${*$ftp}{'net_ftp_intern_port'};
  775.  
  776.  unless(defined $port)
  777.   {
  778.    # create a Listen socket at same address as the command socket
  779.  
  780.    ${*$ftp}{'net_ftp_listen'} ||= IO::Socket::INET->new(Listen    => 5,
  781.                                         Proto     => 'tcp',
  782.                             Timeout   => $ftp->timeout,
  783.                             LocalAddr => $ftp->sockhost,
  784.                                        );
  785.  
  786.    my $listen = ${*$ftp}{'net_ftp_listen'};
  787.  
  788.    my($myport, @myaddr) = ($listen->sockport, split(/\./,$listen->sockhost));
  789.  
  790.    $port = join(',', @myaddr, $myport >> 8, $myport & 0xff);
  791.  
  792.    ${*$ftp}{'net_ftp_intern_port'} = 1;
  793.   }
  794.  
  795.  $ok = $ftp->_PORT($port);
  796.  
  797.  ${*$ftp}{'net_ftp_port'} = $port;
  798.  
  799.  $ok;
  800. }
  801.  
  802. sub ls  { shift->_list_cmd("NLST",@_); }
  803. sub dir { shift->_list_cmd("LIST",@_); }
  804.  
  805. sub pasv
  806. {
  807.  @_ == 1 or croak 'usage: $ftp->pasv()';
  808.  
  809.  my $ftp = shift;
  810.  
  811.  delete ${*$ftp}{'net_ftp_intern_port'};
  812.  
  813.  $ftp->_PASV && $ftp->message =~ /(\d+(,\d+)+)/
  814.     ? ${*$ftp}{'net_ftp_pasv'} = $1
  815.     : undef;    
  816. }
  817.  
  818. sub unique_name
  819. {
  820.  my $ftp = shift;
  821.  ${*$ftp}{'net_ftp_unique'} || undef;
  822. }
  823.  
  824. sub supported {
  825.     @_ == 2 or croak 'usage: $ftp->supported( CMD )';
  826.     my $ftp = shift;
  827.     my $cmd = uc shift;
  828.     my $hash = ${*$ftp}{'net_ftp_supported'} ||= {};
  829.  
  830.     return $hash->{$cmd}
  831.         if exists $hash->{$cmd};
  832.  
  833.     return $hash->{$cmd} = 0
  834.     unless $ftp->_HELP($cmd);
  835.  
  836.     my $text = $ftp->message;
  837.     if($text =~ /following\s+commands/i) {
  838.     $text =~ s/^.*\n//;
  839.         while($text =~ /(\*?)(\w+)(\*?)/sg) {
  840.             $hash->{"\U$2"} = !length("$1$3");
  841.         }
  842.     }
  843.     else {
  844.     $hash->{$cmd} = $text !~ /unimplemented/i;
  845.     }
  846.  
  847.     $hash->{$cmd} ||= 0;
  848. }
  849.  
  850. ##
  851. ## Deprecated methods
  852. ##
  853.  
  854. sub lsl
  855. {
  856.  carp "Use of Net::FTP::lsl deprecated, use 'dir'"
  857.     if $^W;
  858.  goto &dir;
  859. }
  860.  
  861. sub authorise
  862. {
  863.  carp "Use of Net::FTP::authorise deprecated, use 'authorize'"
  864.     if $^W;
  865.  goto &authorize;
  866. }
  867.  
  868.  
  869. ##
  870. ## Private methods
  871. ##
  872.  
  873. sub _extract_path
  874. {
  875.  my($ftp, $path) = @_;
  876.  
  877.  # This tries to work both with and without the quote doubling
  878.  # convention (RFC 959 requires it, but the first 3 servers I checked
  879.  # didn't implement it).  It will fail on a server which uses a quote in
  880.  # the message which isn't a part of or surrounding the path.
  881.  $ftp->ok &&
  882.     $ftp->message =~ /(?:^|\s)\"(.*)\"(?:$|\s)/ &&
  883.     ($path = $1) =~ s/\"\"/\"/g;
  884.  
  885.  $path;
  886. }
  887.  
  888. ##
  889. ## Communication methods
  890. ##
  891.  
  892. sub _dataconn
  893. {
  894.  my $ftp = shift;
  895.  my $data = undef;
  896.  my $pkg = "Net::FTP::" . $ftp->type;
  897.  
  898.  eval "require " . $pkg;
  899.  
  900.  $pkg =~ s/ /_/g;
  901.  
  902.  delete ${*$ftp}{'net_ftp_dataconn'};
  903.  
  904.  if(defined ${*$ftp}{'net_ftp_pasv'})
  905.   {
  906.    my @port = split(/,/,${*$ftp}{'net_ftp_pasv'});
  907.  
  908.    $data = $pkg->new(PeerAddr => join(".",@port[0..3]),
  909.                      PeerPort => $port[4] * 256 + $port[5],
  910.                      Proto    => 'tcp'
  911.                     );
  912.   }
  913.  elsif(defined ${*$ftp}{'net_ftp_listen'})
  914.   {
  915.    $data = ${*$ftp}{'net_ftp_listen'}->accept($pkg);
  916.    close(delete ${*$ftp}{'net_ftp_listen'});
  917.   }
  918.  
  919.  if($data)
  920.   {
  921.    ${*$data} = "";
  922.    $data->timeout($ftp->timeout);
  923.    ${*$ftp}{'net_ftp_dataconn'} = $data;
  924.    ${*$data}{'net_ftp_cmd'} = $ftp;
  925.    ${*$data}{'net_ftp_blksize'} = ${*$ftp}{'net_ftp_blksize'};
  926.   }
  927.  
  928.  $data;
  929. }
  930.  
  931. sub _list_cmd
  932. {
  933.  my $ftp = shift;
  934.  my $cmd = uc shift;
  935.  
  936.  delete ${*$ftp}{'net_ftp_port'};
  937.  delete ${*$ftp}{'net_ftp_pasv'};
  938.  
  939.  my $data = $ftp->_data_cmd($cmd,@_);
  940.  
  941.  return
  942.     unless(defined $data);
  943.  
  944.  require Net::FTP::A;
  945.  bless $data, "Net::FTP::A"; # Force ASCII mode
  946.  
  947.  my $databuf = '';
  948.  my $buf = '';
  949.  my $blksize = ${*$ftp}{'net_ftp_blksize'};
  950.  
  951.  while($data->read($databuf,$blksize)) {
  952.    $buf .= $databuf;
  953.  }
  954.  
  955.  my $list = [ split(/\n/,$buf) ];
  956.  
  957.  $data->close();
  958.  
  959.  if (trEBCDIC)
  960.   {
  961.    for (@$list) { $_ = $ftp->toebcdic($_) }
  962.   }
  963.  
  964.  wantarray ? @{$list}
  965.            : $list;
  966. }
  967.  
  968. sub _data_cmd
  969. {
  970.  my $ftp = shift;
  971.  my $cmd = uc shift;
  972.  my $ok = 1;
  973.  my $where = delete ${*$ftp}{'net_ftp_rest'} || 0;
  974.  my $arg;
  975.  
  976.  for $arg (@_) {
  977.    croak("Bad argument '$arg'\n")
  978.     if $arg =~ /[\r\n]/s;
  979.  }
  980.  
  981.  if(${*$ftp}{'net_ftp_passive'} &&
  982.      !defined ${*$ftp}{'net_ftp_pasv'} &&
  983.      !defined ${*$ftp}{'net_ftp_port'})
  984.   {
  985.    my $data = undef;
  986.  
  987.    $ok = defined $ftp->pasv;
  988.    $ok = $ftp->_REST($where)
  989.     if $ok && $where;
  990.  
  991.    if($ok)
  992.     {
  993.      $ftp->command($cmd,@_);
  994.      $data = $ftp->_dataconn();
  995.      $ok = CMD_INFO == $ftp->response();
  996.      if($ok) 
  997.       {
  998.        $data->reading
  999.          if $data && $cmd =~ /RETR|LIST|NLST/;
  1000.        return $data
  1001.       }
  1002.      $data->_close
  1003.     if $data;
  1004.     }
  1005.    return undef;
  1006.   }
  1007.  
  1008.  $ok = $ftp->port
  1009.     unless (defined ${*$ftp}{'net_ftp_port'} ||
  1010.             defined ${*$ftp}{'net_ftp_pasv'});
  1011.  
  1012.  $ok = $ftp->_REST($where)
  1013.     if $ok && $where;
  1014.  
  1015.  return undef
  1016.     unless $ok;
  1017.  
  1018.  $ftp->command($cmd,@_);
  1019.  
  1020.  return 1
  1021.     if(defined ${*$ftp}{'net_ftp_pasv'});
  1022.  
  1023.  $ok = CMD_INFO == $ftp->response();
  1024.  
  1025.  return $ok 
  1026.     unless exists ${*$ftp}{'net_ftp_intern_port'};
  1027.  
  1028.  if($ok) {
  1029.    my $data = $ftp->_dataconn();
  1030.  
  1031.    $data->reading
  1032.          if $data && $cmd =~ /RETR|LIST|NLST/;
  1033.  
  1034.    return $data;
  1035.  }
  1036.  
  1037.  
  1038.  close(delete ${*$ftp}{'net_ftp_listen'});
  1039.  
  1040.  return undef;
  1041. }
  1042.  
  1043. ##
  1044. ## Over-ride methods (Net::Cmd)
  1045. ##
  1046.  
  1047. sub debug_text { $_[2] =~ /^(pass|resp|acct)/i ? "$1 ....\n" : $_[2]; }
  1048.  
  1049. sub command
  1050. {
  1051.  my $ftp = shift;
  1052.  
  1053.  delete ${*$ftp}{'net_ftp_port'};
  1054.  $ftp->SUPER::command(@_);
  1055. }
  1056.  
  1057. sub response
  1058. {
  1059.  my $ftp = shift;
  1060.  my $code = $ftp->SUPER::response();
  1061.  
  1062.  delete ${*$ftp}{'net_ftp_pasv'}
  1063.     if ($code != CMD_MORE && $code != CMD_INFO);
  1064.  
  1065.  $code;
  1066. }
  1067.  
  1068. sub parse_response
  1069. {
  1070.  return ($1, $2 eq "-")
  1071.     if $_[1] =~ s/^(\d\d\d)(.?)//o;
  1072.  
  1073.  my $ftp = shift;
  1074.  
  1075.  # Darn MS FTP server is a load of CRAP !!!!
  1076.  return ()
  1077.     unless ${*$ftp}{'net_cmd_code'} + 0;
  1078.  
  1079.  (${*$ftp}{'net_cmd_code'},1);
  1080. }
  1081.  
  1082. ##
  1083. ## Allow 2 servers to talk directly
  1084. ##
  1085.  
  1086. sub pasv_xfer {
  1087.     my($sftp,$sfile,$dftp,$dfile,$unique) = @_;
  1088.  
  1089.     ($dfile = $sfile) =~ s#.*/##
  1090.     unless(defined $dfile);
  1091.  
  1092.     my $port = $sftp->pasv or
  1093.     return undef;
  1094.  
  1095.     $dftp->port($port) or
  1096.     return undef;
  1097.  
  1098.     return undef
  1099.     unless($unique ? $dftp->stou($dfile) : $dftp->stor($dfile));
  1100.  
  1101.     unless($sftp->retr($sfile) && $sftp->response == CMD_INFO) {
  1102.     $sftp->retr($sfile);
  1103.     $dftp->abort;
  1104.     $dftp->response();
  1105.     return undef;
  1106.     }
  1107.  
  1108.     $dftp->pasv_wait($sftp);
  1109. }
  1110.  
  1111. sub pasv_wait
  1112. {
  1113.  @_ == 2 or croak 'usage: $ftp->pasv_wait(NON_PASV_FTP)';
  1114.  
  1115.  my($ftp, $non_pasv) = @_;
  1116.  my($file,$rin,$rout);
  1117.  
  1118.  vec($rin='',fileno($ftp),1) = 1;
  1119.  select($rout=$rin, undef, undef, undef);
  1120.  
  1121.  $ftp->response();
  1122.  $non_pasv->response();
  1123.  
  1124.  return undef
  1125.     unless $ftp->ok() && $non_pasv->ok();
  1126.  
  1127.  return $1
  1128.     if $ftp->message =~ /unique file name:\s*(\S*)\s*\)/;
  1129.  
  1130.  return $1
  1131.     if $non_pasv->message =~ /unique file name:\s*(\S*)\s*\)/;
  1132.  
  1133.  return 1;
  1134. }
  1135.  
  1136. sub cmd { shift->command(@_)->response() }
  1137.  
  1138. ########################################
  1139. #
  1140. # RFC959 commands
  1141. #
  1142.  
  1143. sub _ABOR { shift->command("ABOR")->response()     == CMD_OK }
  1144. sub _CDUP { shift->command("CDUP")->response()     == CMD_OK }
  1145. sub _NOOP { shift->command("NOOP")->response()     == CMD_OK }
  1146. sub _PASV { shift->command("PASV")->response()     == CMD_OK }
  1147. sub _QUIT { shift->command("QUIT")->response()     == CMD_OK }
  1148. sub _DELE { shift->command("DELE",@_)->response() == CMD_OK }
  1149. sub _CWD  { shift->command("CWD", @_)->response() == CMD_OK }
  1150. sub _PORT { shift->command("PORT",@_)->response() == CMD_OK }
  1151. sub _RMD  { shift->command("RMD", @_)->response() == CMD_OK }
  1152. sub _MKD  { shift->command("MKD", @_)->response() == CMD_OK }
  1153. sub _PWD  { shift->command("PWD", @_)->response() == CMD_OK }
  1154. sub _TYPE { shift->command("TYPE",@_)->response() == CMD_OK }
  1155. sub _RNTO { shift->command("RNTO",@_)->response() == CMD_OK }
  1156. sub _RESP { shift->command("RESP",@_)->response() == CMD_OK }
  1157. sub _MDTM { shift->command("MDTM",@_)->response() == CMD_OK }
  1158. sub _SIZE { shift->command("SIZE",@_)->response() == CMD_OK }
  1159. sub _HELP { shift->command("HELP",@_)->response() == CMD_OK }
  1160. sub _STAT { shift->command("STAT",@_)->response() == CMD_OK }
  1161. sub _APPE { shift->command("APPE",@_)->response() == CMD_INFO }
  1162. sub _LIST { shift->command("LIST",@_)->response() == CMD_INFO }
  1163. sub _NLST { shift->command("NLST",@_)->response() == CMD_INFO }
  1164. sub _RETR { shift->command("RETR",@_)->response() == CMD_INFO }
  1165. sub _STOR { shift->command("STOR",@_)->response() == CMD_INFO }
  1166. sub _STOU { shift->command("STOU",@_)->response() == CMD_INFO }
  1167. sub _RNFR { shift->command("RNFR",@_)->response() == CMD_MORE }
  1168. sub _REST { shift->command("REST",@_)->response() == CMD_MORE }
  1169. sub _USER { shift->command("user",@_)->response() } # A certain brain dead firewall :-)
  1170. sub _PASS { shift->command("PASS",@_)->response() }
  1171. sub _ACCT { shift->command("ACCT",@_)->response() }
  1172. sub _AUTH { shift->command("AUTH",@_)->response() }
  1173.  
  1174. sub _ALLO { shift->unsupported(@_) }
  1175. sub _SMNT { shift->unsupported(@_) }
  1176. sub _MODE { shift->unsupported(@_) }
  1177. sub _SYST { shift->unsupported(@_) }
  1178. sub _STRU { shift->unsupported(@_) }
  1179. sub _REIN { shift->unsupported(@_) }
  1180.  
  1181. 1;
  1182.  
  1183. __END__
  1184.  
  1185. =head1 NAME
  1186.  
  1187. Net::FTP - FTP Client class
  1188.  
  1189. =head1 SYNOPSIS
  1190.  
  1191.     use Net::FTP;
  1192.  
  1193.     $ftp = Net::FTP->new("some.host.name", Debug => 0);
  1194.     $ftp->login("anonymous",'-anonymous@');
  1195.     $ftp->cwd("/pub");
  1196.     $ftp->get("that.file");
  1197.     $ftp->quit;
  1198.  
  1199. =head1 DESCRIPTION
  1200.  
  1201. C<Net::FTP> is a class implementing a simple FTP client in Perl as
  1202. described in RFC959.  It provides wrappers for a subset of the RFC959
  1203. commands.
  1204.  
  1205. =head1 OVERVIEW
  1206.  
  1207. FTP stands for File Transfer Protocol.  It is a way of transferring
  1208. files between networked machines.  The protocol defines a client
  1209. (whose commands are provided by this module) and a server (not
  1210. implemented in this module).  Communication is always initiated by the
  1211. client, and the server responds with a message and a status code (and
  1212. sometimes with data).
  1213.  
  1214. The FTP protocol allows files to be sent to or fetched from the
  1215. server.  Each transfer involves a B<local file> (on the client) and a
  1216. B<remote file> (on the server).  In this module, the same file name
  1217. will be used for both local and remote if only one is specified.  This
  1218. means that transferring remote file C</path/to/file> will try to put
  1219. that file in C</path/to/file> locally, unless you specify a local file
  1220. name.
  1221.  
  1222. The protocol also defines several standard B<translations> which the
  1223. file can undergo during transfer.  These are ASCII, EBCDIC, binary,
  1224. and byte.  ASCII is the default type, and indicates that the sender of
  1225. files will translate the ends of lines to a standard representation
  1226. which the receiver will then translate back into their local
  1227. representation.  EBCDIC indicates the file being transferred is in
  1228. EBCDIC format.  Binary (also known as image) format sends the data as
  1229. a contiguous bit stream.  Byte format transfers the data as bytes, the
  1230. values of which remain the same regardless of differences in byte size
  1231. between the two machines (in theory - in practice you should only use
  1232. this if you really know what you're doing).
  1233.  
  1234. =head1 CONSTRUCTOR
  1235.  
  1236. =over 4
  1237.  
  1238. =item new (HOST [,OPTIONS])
  1239.  
  1240. This is the constructor for a new Net::FTP object. C<HOST> is the
  1241. name of the remote host to which an FTP connection is required.
  1242.  
  1243. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  1244. Possible options are:
  1245.  
  1246. B<Firewall> - The name of a machine which acts as an FTP firewall. This can be
  1247. overridden by an environment variable C<FTP_FIREWALL>. If specified, and the
  1248. given host cannot be directly connected to, then the
  1249. connection is made to the firewall machine and the string C<@hostname> is
  1250. appended to the login identifier. This kind of setup is also refered to
  1251. as an ftp proxy.
  1252.  
  1253. B<FirewallType> - The type of firewall running on the machine indicated by
  1254. B<Firewall>. This can be overridden by an environment variable
  1255. C<FTP_FIREWALL_TYPE>. For a list of permissible types, see the description of
  1256. ftp_firewall_type in L<Net::Config>.
  1257.  
  1258. B<BlockSize> - This is the block size that Net::FTP will use when doing
  1259. transfers. (defaults to 10240)
  1260.  
  1261. B<Port> - The port number to connect to on the remote machine for the
  1262. FTP connection
  1263.  
  1264. B<Timeout> - Set a timeout value (defaults to 120)
  1265.  
  1266. B<Debug> - debug level (see the debug method in L<Net::Cmd>)
  1267.  
  1268. B<Passive> - If set to a non-zero value then all data transfers will be done
  1269. using passive mode. This is not usually required except for some I<dumb>
  1270. servers, and some firewall configurations. This can also be set by the
  1271. environment variable C<FTP_PASSIVE>.
  1272.  
  1273. B<Hash> - If given a reference to a file handle (e.g., C<\*STDERR>),
  1274. print hash marks (#) on that filehandle every 1024 bytes.  This
  1275. simply invokes the C<hash()> method for you, so that hash marks
  1276. are displayed for all transfers.  You can, of course, call C<hash()>
  1277. explicitly whenever you'd like.
  1278.  
  1279. If the constructor fails undef will be returned and an error message will
  1280. be in $@
  1281.  
  1282. =back
  1283.  
  1284. =head1 METHODS
  1285.  
  1286. Unless otherwise stated all methods return either a I<true> or I<false>
  1287. value, with I<true> meaning that the operation was a success. When a method
  1288. states that it returns a value, failure will be returned as I<undef> or an
  1289. empty list.
  1290.  
  1291. =over 4
  1292.  
  1293. =item login ([LOGIN [,PASSWORD [, ACCOUNT] ] ])
  1294.  
  1295. Log into the remote FTP server with the given login information. If
  1296. no arguments are given then the C<Net::FTP> uses the C<Net::Netrc>
  1297. package to lookup the login information for the connected host.
  1298. If no information is found then a login of I<anonymous> is used.
  1299. If no password is given and the login is I<anonymous> then I<anonymous@>
  1300. will be used for password.
  1301.  
  1302. If the connection is via a firewall then the C<authorize> method will
  1303. be called with no arguments.
  1304.  
  1305. =item authorize ( [AUTH [, RESP]])
  1306.  
  1307. This is a protocol used by some firewall ftp proxies. It is used
  1308. to authorise the user to send data out.  If both arguments are not specified
  1309. then C<authorize> uses C<Net::Netrc> to do a lookup.
  1310.  
  1311. =item site (ARGS)
  1312.  
  1313. Send a SITE command to the remote server and wait for a response.
  1314.  
  1315. Returns most significant digit of the response code.
  1316.  
  1317. =item type (TYPE [, ARGS])
  1318.  
  1319. This method will send the TYPE command to the remote FTP server
  1320. to change the type of data transfer. The return value is the previous
  1321. value.
  1322.  
  1323. =item ascii ([ARGS]) binary([ARGS]) ebcdic([ARGS]) byte([ARGS])
  1324.  
  1325. Synonyms for C<type> with the first arguments set correctly
  1326.  
  1327. B<NOTE> ebcdic and byte are not fully supported.
  1328.  
  1329. =item rename ( OLDNAME, NEWNAME )
  1330.  
  1331. Rename a file on the remote FTP server from C<OLDNAME> to C<NEWNAME>. This
  1332. is done by sending the RNFR and RNTO commands.
  1333.  
  1334. =item delete ( FILENAME )
  1335.  
  1336. Send a request to the server to delete C<FILENAME>.
  1337.  
  1338. =item cwd ( [ DIR ] )
  1339.  
  1340. Attempt to change directory to the directory given in C<$dir>.  If
  1341. C<$dir> is C<"..">, the FTP C<CDUP> command is used to attempt to
  1342. move up one directory. If no directory is given then an attempt is made
  1343. to change the directory to the root directory.
  1344.  
  1345. =item cdup ()
  1346.  
  1347. Change directory to the parent of the current directory.
  1348.  
  1349. =item pwd ()
  1350.  
  1351. Returns the full pathname of the current directory.
  1352.  
  1353. =item restart ( WHERE )
  1354.  
  1355. Set the byte offset at which to begin the next data transfer. Net::FTP simply
  1356. records this value and uses it when during the next data transfer. For this
  1357. reason this method will not return an error, but setting it may cause
  1358. a subsequent data transfer to fail.
  1359.  
  1360. =item rmdir ( DIR )
  1361.  
  1362. Remove the directory with the name C<DIR>.
  1363.  
  1364. =item mkdir ( DIR [, RECURSE ])
  1365.  
  1366. Create a new directory with the name C<DIR>. If C<RECURSE> is I<true> then
  1367. C<mkdir> will attempt to create all the directories in the given path.
  1368.  
  1369. Returns the full pathname to the new directory.
  1370.  
  1371. =item ls ( [ DIR ] )
  1372.  
  1373. Get a directory listing of C<DIR>, or the current directory.
  1374.  
  1375. In an array context, returns a list of lines returned from the server. In
  1376. a scalar context, returns a reference to a list.
  1377.  
  1378. =item dir ( [ DIR ] )
  1379.  
  1380. Get a directory listing of C<DIR>, or the current directory in long format.
  1381.  
  1382. In an array context, returns a list of lines returned from the server. In
  1383. a scalar context, returns a reference to a list.
  1384.  
  1385. =item get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )
  1386.  
  1387. Get C<REMOTE_FILE> from the server and store locally. C<LOCAL_FILE> may be
  1388. a filename or a filehandle. If not specified, the file will be stored in
  1389. the current directory with the same leafname as the remote file.
  1390.  
  1391. If C<WHERE> is given then the first C<WHERE> bytes of the file will
  1392. not be transfered, and the remaining bytes will be appended to
  1393. the local file if it already exists.
  1394.  
  1395. Returns C<LOCAL_FILE>, or the generated local file name if C<LOCAL_FILE>
  1396. is not given. If an error was encountered undef is returned.
  1397.  
  1398. =item put ( LOCAL_FILE [, REMOTE_FILE ] )
  1399.  
  1400. Put a file on the remote server. C<LOCAL_FILE> may be a name or a filehandle.
  1401. If C<LOCAL_FILE> is a filehandle then C<REMOTE_FILE> must be specified. If
  1402. C<REMOTE_FILE> is not specified then the file will be stored in the current
  1403. directory with the same leafname as C<LOCAL_FILE>.
  1404.  
  1405. Returns C<REMOTE_FILE>, or the generated remote filename if C<REMOTE_FILE>
  1406. is not given.
  1407.  
  1408. B<NOTE>: If for some reason the transfer does not complete and an error is
  1409. returned then the contents that had been transfered will not be remove
  1410. automatically.
  1411.  
  1412. =item put_unique ( LOCAL_FILE [, REMOTE_FILE ] )
  1413.  
  1414. Same as put but uses the C<STOU> command.
  1415.  
  1416. Returns the name of the file on the server.
  1417.  
  1418. =item append ( LOCAL_FILE [, REMOTE_FILE ] )
  1419.  
  1420. Same as put but appends to the file on the remote server.
  1421.  
  1422. Returns C<REMOTE_FILE>, or the generated remote filename if C<REMOTE_FILE>
  1423. is not given.
  1424.  
  1425. =item unique_name ()
  1426.  
  1427. Returns the name of the last file stored on the server using the
  1428. C<STOU> command.
  1429.  
  1430. =item mdtm ( FILE )
  1431.  
  1432. Returns the I<modification time> of the given file
  1433.  
  1434. =item size ( FILE )
  1435.  
  1436. Returns the size in bytes for the given file as stored on the remote server.
  1437.  
  1438. B<NOTE>: The size reported is the size of the stored file on the remote server.
  1439. If the file is subsequently transfered from the server in ASCII mode
  1440. and the remote server and local machine have different ideas about
  1441. "End Of Line" then the size of file on the local machine after transfer
  1442. may be different.
  1443.  
  1444. =item supported ( CMD )
  1445.  
  1446. Returns TRUE if the remote server supports the given command.
  1447.  
  1448. =item hash ( [FILEHANDLE_GLOB_REF],[ BYTES_PER_HASH_MARK] )
  1449.  
  1450. Called without parameters, or with the first argument false, hash marks
  1451. are suppressed.  If the first argument is true but not a reference to a 
  1452. file handle glob, then \*STDERR is used.  The second argument is the number
  1453. of bytes per hash mark printed, and defaults to 1024.  In all cases the
  1454. return value is a reference to an array of two:  the filehandle glob reference
  1455. and the bytes per hash mark.
  1456.  
  1457. =back
  1458.  
  1459. The following methods can return different results depending on
  1460. how they are called. If the user explicitly calls either
  1461. of the C<pasv> or C<port> methods then these methods will
  1462. return a I<true> or I<false> value. If the user does not
  1463. call either of these methods then the result will be a
  1464. reference to a C<Net::FTP::dataconn> based object.
  1465.  
  1466. =over 4
  1467.  
  1468. =item nlst ( [ DIR ] )
  1469.  
  1470. Send an C<NLST> command to the server, with an optional parameter.
  1471.  
  1472. =item list ( [ DIR ] )
  1473.  
  1474. Same as C<nlst> but using the C<LIST> command
  1475.  
  1476. =item retr ( FILE )
  1477.  
  1478. Begin the retrieval of a file called C<FILE> from the remote server.
  1479.  
  1480. =item stor ( FILE )
  1481.  
  1482. Tell the server that you wish to store a file. C<FILE> is the
  1483. name of the new file that should be created.
  1484.  
  1485. =item stou ( FILE )
  1486.  
  1487. Same as C<stor> but using the C<STOU> command. The name of the unique
  1488. file which was created on the server will be available via the C<unique_name>
  1489. method after the data connection has been closed.
  1490.  
  1491. =item appe ( FILE )
  1492.  
  1493. Tell the server that we want to append some data to the end of a file
  1494. called C<FILE>. If this file does not exist then create it.
  1495.  
  1496. =back
  1497.  
  1498. If for some reason you want to have complete control over the data connection,
  1499. this includes generating it and calling the C<response> method when required,
  1500. then the user can use these methods to do so.
  1501.  
  1502. However calling these methods only affects the use of the methods above that
  1503. can return a data connection. They have no effect on methods C<get>, C<put>,
  1504. C<put_unique> and those that do not require data connections.
  1505.  
  1506. =over 4
  1507.  
  1508. =item port ( [ PORT ] )
  1509.  
  1510. Send a C<PORT> command to the server. If C<PORT> is specified then it is sent
  1511. to the server. If not, then a listen socket is created and the correct information
  1512. sent to the server.
  1513.  
  1514. =item pasv ()
  1515.  
  1516. Tell the server to go into passive mode. Returns the text that represents the
  1517. port on which the server is listening, this text is in a suitable form to
  1518. sent to another ftp server using the C<port> method.
  1519.  
  1520. =back
  1521.  
  1522. The following methods can be used to transfer files between two remote
  1523. servers, providing that these two servers can connect directly to each other.
  1524.  
  1525. =over 4
  1526.  
  1527. =item pasv_xfer ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )
  1528.  
  1529. This method will do a file transfer between two remote ftp servers. If
  1530. C<DEST_FILE> is omitted then the leaf name of C<SRC_FILE> will be used.
  1531.  
  1532. =item pasv_xfer_unique ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )
  1533.  
  1534. Like C<pasv_xfer> but the file is stored on the remote server using
  1535. the STOU command.
  1536.  
  1537. =item pasv_wait ( NON_PASV_SERVER )
  1538.  
  1539. This method can be used to wait for a transfer to complete between a passive
  1540. server and a non-passive server. The method should be called on the passive
  1541. server with the C<Net::FTP> object for the non-passive server passed as an
  1542. argument.
  1543.  
  1544. =item abort ()
  1545.  
  1546. Abort the current data transfer.
  1547.  
  1548. =item quit ()
  1549.  
  1550. Send the QUIT command to the remote FTP server and close the socket connection.
  1551.  
  1552. =back
  1553.  
  1554. =head2 Methods for the adventurous
  1555.  
  1556. C<Net::FTP> inherits from C<Net::Cmd> so methods defined in C<Net::Cmd> may
  1557. be used to send commands to the remote FTP server.
  1558.  
  1559. =over 4
  1560.  
  1561. =item quot (CMD [,ARGS])
  1562.  
  1563. Send a command, that Net::FTP does not directly support, to the remote
  1564. server and wait for a response.
  1565.  
  1566. Returns most significant digit of the response code.
  1567.  
  1568. B<WARNING> This call should only be used on commands that do not require
  1569. data connections. Misuse of this method can hang the connection.
  1570.  
  1571. =back
  1572.  
  1573. =head1 THE dataconn CLASS
  1574.  
  1575. Some of the methods defined in C<Net::FTP> return an object which will
  1576. be derived from this class.The dataconn class itself is derived from
  1577. the C<IO::Socket::INET> class, so any normal IO operations can be performed.
  1578. However the following methods are defined in the dataconn class and IO should
  1579. be performed using these.
  1580.  
  1581. =over 4
  1582.  
  1583. =item read ( BUFFER, SIZE [, TIMEOUT ] )
  1584.  
  1585. Read C<SIZE> bytes of data from the server and place it into C<BUFFER>, also
  1586. performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
  1587. given, the timeout value from the command connection will be used.
  1588.  
  1589. Returns the number of bytes read before any <CRLF> translation.
  1590.  
  1591. =item write ( BUFFER, SIZE [, TIMEOUT ] )
  1592.  
  1593. Write C<SIZE> bytes of data from C<BUFFER> to the server, also
  1594. performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
  1595. given, the timeout value from the command connection will be used.
  1596.  
  1597. Returns the number of bytes written before any <CRLF> translation.
  1598.  
  1599. =item bytes_read ()
  1600.  
  1601. Returns the number of bytes read so far.
  1602.  
  1603. =item abort ()
  1604.  
  1605. Abort the current data transfer.
  1606.  
  1607. =item close ()
  1608.  
  1609. Close the data connection and get a response from the FTP server. Returns
  1610. I<true> if the connection was closed successfully and the first digit of
  1611. the response from the server was a '2'.
  1612.  
  1613. =back
  1614.  
  1615. =head1 UNIMPLEMENTED
  1616.  
  1617. The following RFC959 commands have not been implemented:
  1618.  
  1619. =over 4
  1620.  
  1621. =item B<ALLO>
  1622.  
  1623. Allocates storage for the file to be transferred.
  1624.  
  1625. =item B<SMNT>
  1626.  
  1627. Mount a different file system structure without changing login or
  1628. accounting information.
  1629.  
  1630. =item B<HELP>
  1631.  
  1632. Ask the server for "helpful information" (that's what the RFC says) on
  1633. the commands it accepts.
  1634.  
  1635. =item B<MODE>
  1636.  
  1637. Specifies transfer mode (stream, block or compressed) for file to be
  1638. transferred.
  1639.  
  1640. =item B<SYST>
  1641.  
  1642. Request remote server system identification.
  1643.  
  1644. =item B<STAT>
  1645.  
  1646. Request remote server status.
  1647.  
  1648. =item B<STRU>
  1649.  
  1650. Specifies file structure for file to be transferred.
  1651.  
  1652. =item B<REIN>
  1653.  
  1654. Reinitialize the connection, flushing all I/O and account information.
  1655.  
  1656. =back
  1657.  
  1658. =head1 REPORTING BUGS
  1659.  
  1660. When reporting bugs/problems please include as much information as possible.
  1661. It may be difficult for me to reproduce the problem as almost every setup
  1662. is different.
  1663.  
  1664. A small script which yields the problem will probably be of help. It would
  1665. also be useful if this script was run with the extra options C<Debug => 1>
  1666. passed to the constructor, and the output sent with the bug report. If you
  1667. cannot include a small script then please include a Debug trace from a
  1668. run of your program which does yield the problem.
  1669.  
  1670. =head1 AUTHOR
  1671.  
  1672. Graham Barr <gbarr@pobox.com>
  1673.  
  1674. =head1 SEE ALSO
  1675.  
  1676. L<Net::Netrc>
  1677. L<Net::Cmd>
  1678.  
  1679. ftp(1), ftpd(8), RFC 959
  1680. http://www.cis.ohio-state.edu/htbin/rfc/rfc959.html
  1681.  
  1682. =head1 USE EXAMPLES
  1683.  
  1684. For an example of the use of Net::FTP see
  1685.  
  1686. =over 4
  1687.  
  1688. =item http://www.csh.rit.edu/~adam/Progs/autoftp-2.0.tar.gz
  1689.  
  1690. C<autoftp> is a program that can retrieve, send, or list files via
  1691. the FTP protocol in a non-interactive manner.
  1692.  
  1693. =back
  1694.  
  1695. =head1 CREDITS
  1696.  
  1697. Henry Gabryjelski <henryg@WPI.EDU> - for the suggestion of creating directories
  1698. recursively.
  1699.  
  1700. Nathan Torkington <gnat@frii.com> - for some input on the documentation.
  1701.  
  1702. Roderick Schertler <roderick@gate.net> - for various inputs
  1703.  
  1704. =head1 COPYRIGHT
  1705.  
  1706. Copyright (c) 1995-1998 Graham Barr. All rights reserved.
  1707. This program is free software; you can redistribute it and/or modify it
  1708. under the same terms as Perl itself.
  1709.  
  1710. =for html <hr>
  1711.  
  1712. I<$Id: //depot/libnet/Net/FTP.pm#68 $>
  1713.  
  1714. =cut
  1715.