#!/afs/athena/contrib/perl/p

print("<ul>\n");
while(<>){
    if(m#(http://[a-zA-Z/\.]+)#){
       $url = $1;
       $doc = &wwwfetch($url);
       $doc =~ /<title>([^<]+)<\/title>/i;
       $title = $1;
       $title = "No Title" unless $title;
       print("<li><a href=\"$url\">$title</a>\n");
   }
    $prev = $_;
}
print("</ul>\n");

sub wwwfetch {
    local($URL) = @_;

    $URL =~ m#http://([^/]+)/(.+)#;
    $hostport = $1; $path = $2;

    ($them, $port) = split(/:/, $hostport);
    $port = 80 unless $port;
 
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
    }
    else {
	return(0);
    }
    
# Give the socket an address.
 
    if (bind(S, $this)) {
    }
    else {
	return(0);
    }
    
    if (connect(S,$that)) {
	print(S $request);
	while(<S>){
	    $resp .= $_;
	}
	close(S);
	$resp;
    }
    else{
	return(0);
    }
}
