/* -*-text-*-
 *
 *   gethostbyname / gethostbyaddr
 *   DWF 10/13/93
 *
 *   cc -o gethostbyname gethostbyname.c
 *   ln -s gethostbyname gethostbyaddr
 *
 *  6/2/94
 *    You can now provide addresses to gethostbyaddr in hexadecimal format.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>

main (int argc, char **argv)
{
  struct hostent *hp;
  unsigned long a;
  if (argc != 2)
  {
    printf ("Usage:  %s host.at.this.net\n", argv[0]);
    exit (-1);
  }
  if (strstr (argv[0], "gethostbyaddr"))
  {
    if (strchr (argv[1], '.'))
      a = inet_addr (argv[1]);
    else
      sscanf (argv[1], "%lx", &a);
    hp = gethostbyaddr (&a, sizeof a, AF_INET);
  }
  else
    hp = gethostbyname (argv[1]);
  if (!hp)
  {
    puts ("No idea.");
    exit (-1);
  }
  else
  {
    char **a;
#ifndef sun
    struct in_addr u;
#endif
    printf ("Canonical:  '%s'\n", hp->h_name);
    printf ("Aliases:    ");
    a = hp->h_aliases;
    if (!(*a))
      puts ("None.");
    else
      while (*a)
      {
        printf ("'%s'\n            ", *a);
        a++;
      }
    a = hp->h_addr_list;
    printf ("\rAddresses: ");
    while (*a)
    {
#ifdef sun
      printf (" %s", inet_ntoa (*a));
#else
      u.s_addr = **(unsigned long **)a;
      printf (" %s", inet_ntoa (u));
#endif
      a++;
    }
  }
  printf ("\n");
  exit (0);
}
