home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / misc / shell-utils / gophercd < prev    next >
Text File  |  1991-08-29  |  2KB  |  84 lines

  1. #!/usr/local/bin/perl
  2.  
  3. sub dokill {
  4.     kill 9,$child if $child;
  5. }
  6.  
  7. sub Opengopher {
  8.  
  9.     local($them,$port) = @_;    
  10.     $them = 'localhost' unless $them;
  11.  
  12.     $AF_INET = 2;
  13.     $SOCK_STREAM = 1;
  14.  
  15.     $SIG{'INT'} = 'dokill';
  16.  
  17.     $sockaddr = 'S n a4 x8';
  18.  
  19.     chop($hostname = `hostname`);
  20.  
  21.     ($name,$aliases,$proto) = getprotobyname('tcp');
  22.     ($name,$aliases,$port) = getservbyname($port,'tcp')
  23.     unless $port =~ /^\d+$/;;
  24.     ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
  25.     ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
  26.  
  27.     $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
  28.     $that = pack($sockaddr, $AF_INET, $port, $thataddr);
  29.  
  30.     # Make the socket filehandle.
  31.     socket(S, $AF_INET, $SOCK_STREAM, $proto) || die $!;
  32.  
  33.     # Give the socket an address.
  34.     bind(S, $this) || die $!;
  35.  
  36.     # Call up the server.
  37.     connect(S,$that) || die $!;
  38.  
  39.     # Set socket to be command buffered.
  40.     select(S); $| = 1; select(STDOUT);
  41.  
  42. }
  43.  
  44. $DirName = shift(@ARGV) || die "Usage: $0 directory name";
  45.  
  46. print "Trying $DirName ....\n";
  47. open(GopherFile, "/tmp/.gopher") || die "You haven't run gsetup yet!";
  48. $Firstline = <GopherFile>;
  49. ($CurrentHost, $Port, $Path) = split('\t',$Firstline);
  50. close GopherFile;
  51.  
  52. &Opengopher( $CurrentHost, $Port);
  53.  
  54. print S "$Path\n";
  55.  
  56. $NewHost = "moo";
  57.  
  58. while (<S>) {
  59.     chop;
  60.     chop;
  61.     ($ObjName, $Path, $Host, $Port) = split('\t', $_, 6);
  62.     $Name = substr($ObjName, 1);
  63.     $Obj  = substr($ObjName, 0, 1);
  64.     
  65.     if (($Obj eq "1") && ($Name eq $DirName)) {
  66.     $NewPath = $Path;
  67.     $NewHost = $Host;
  68.     $NewPort = $Port;
  69.     }
  70.  
  71. }
  72.  
  73. if ($NewHost eq "moo") {
  74.     print "No such directory\n";
  75.     exit;
  76. }
  77.  
  78. open(GopherFile, ">/tmp/.gopher");
  79. $Out = sprintf("%s\t%s\t%s", $NewHost, $NewPort, $NewPath);
  80. #$Out = join('\t', $NewHost, $NewPort, $NewPath);
  81. print GopherFile $Out;
  82.  
  83. close GopherFile;
  84.