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

int main(int argc, char **argv)
{
	struct hostent *host;
	struct in_addr addr;
	int i;
	
	/* command line help */
	if(argc != 2)
	{
		fprintf(stderr, "usage: %s [name | address]\n", 
			argv[0]);
		exit(1);
	}
	
	/* guess whether argument is name or address */
	if(inet_aton(argv[1], &addr))
	{
		printf("lookup address: %s\n", argv[1]);
		if((host = gethostbyaddr(&addr, 4, AF_INET)) == NULL)
		{	
			herror(argv[0]);
			exit(2);
		}
	}
	else
	{
		printf("lookup name: %s\n", argv[1]);
		if((host = gethostbyname(argv[1])) == NULL)
		{
			herror(argv[0]);
			exit(2);
		}
	}

	/* print details */
	printf("host name:\t%s\n", host->h_name);
	for(; *host->h_aliases != NULL; host->h_aliases++)
		printf("host alias:\t%s\n", *host->h_aliases);

	for(i=0; *host->h_addr_list != NULL; host->h_addr_list++)
	{
		printf("host address:\t%s\n",
			inet_ntoa(*(struct in_addr *)*host->h_addr_list));
	}
	return 0;
}
			

