#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <ctype.h>
#include <netdb.h>

extern int h_errno;
int i;
struct hostent *host;
struct in_addr ina;

void getaddr(hostname)
char *hostname;
{
    host = gethostbyname(hostname);
}

void gethost(addr)
char *addr;
{
    bzero((caddr_t)&ina, sizeof ina);
    ina.s_addr = inet_addr(addr);
    host = gethostbyaddr(&ina,sizeof ina,AF_INET);
}

void out() {
    if (host == (struct hostent *)0) {
	switch (h_errno) {
	  case HOST_NOT_FOUND :
	    fprintf(stderr,"error : host not found\n");
	    break;
	  case TRY_AGAIN :
	    fprintf(stderr,"error : try again\n");
	    break;
	  case NO_RECOVERY :
	    fprintf(stderr,"error : no recovery\n");
	    break;
	  case NO_ADDRESS :
	    fprintf(stderr,"error : no address\n");
	    break;
	  default :
	    fprintf(stderr,"error : code %d\n",h_errno);
	}
	exit(1);
    }
    printf("%s (alias ", host->h_name);
    for (i = 0; host->h_aliases[i] != NULL; i++)
	printf("%s ",host->h_aliases[i]);
    printf(") at (");
    for (i = 0; host->h_addr_list[i] != NULL; i++) {
	bcopy(host->h_addr_list[i],&ina,sizeof ina);
	printf("%s ",inet_ntoa(ina));
    }
    printf(")\n");
}

int analyse(name)
char *name;
{
    int dots = 0;
    char c;

    for (c = *name++; c != '\0'; c = *name++) {
	if (isdigit(c))
	    continue;
	if (c == '.') {
	    dots++;
	    continue;
	} else
	    return 0;
    }
    if (dots > 3)
	return 0;
    return 1;
}

void main(argc, argv)
int argc;
char **argv;
{
    argc--, argv++;
    for (;argc != 0; argc--, argv++)
	if (analyse(*argv)) {
	    gethost(*argv);
	    out();
	} else {
	    getaddr(*argv);
	    out();
	}
    exit(0);
}
