#!/bin/perl -w
# $Id: clientname,v 2.10 1999/01/14 17:26:55 hasegawa Exp $
# copyright (c)1997-1999 Yoshinori Hasegawa <hasegawa@madoka.org>

if ($] < 5) {
  foreach $inc (@INC) {
    if (-r "$inc/sys/socket.ph") {
      eval 'require "sys/socket.ph"';
      $SOCKET = "$inc/sys/socket.ph" unless $@;
      last;
    }
    if (-r "$inc/socket.ph") {
      eval 'require "socket.ph"';
      $SOCKET = "$inc/socket.ph" unless $@;
      last;
    }
  }
} else {
  eval 'use Socket';
  $SOCKET = 'Socket.pm' unless $@;
}

$AF_INET = eval '&AF_INET' || 2;

&main(@ARGV);

sub main {
  local(@args) = @_;
  local($host, $addr, @addr, $name);
  if (@args < 1) {
    &usage;
    exit(1);
  }
  ($host) = @args;
  if ($host =~ /^\d+$/) {
    $addr = pack('N', $host);
  } elsif ($host =~ /^[\d\.]+$/) {
    @addr = split(/\./, $host);
    $addr = pack('C4', @addr, 0, 0, 0);
  } else {
    $addr = (gethostbyname($host))[4] || "\0\0\0\0";
  }
  if ($addr && $addr ne "\0\0\0\0") {
    $name = (gethostbyaddr($addr, $AF_INET))[0];
    if ($name) {
      print $name, "\n";
    } else {
      print "*** unknown ***\n";
    }
    print join('.', unpack('C4', $addr)), "\n";
    print unpack('N', $addr), "\n";
  } else {
    print "hostname lookup failed for $host\n";
  }
}

sub usage {
  print "usage: perl clientname {<hostname>|<ip-address>}\n";
}
