1 /* 2 * gethostbyname tester. compile with: 3 * 4 * cc -o gethostbyname gethostbyname.c (SunOS 4.x) 5 * 6 * cc -o gethostbyname gethostbyname.c -lnsl (SunOS 5.x) 7 * 8 * run as: gethostbyname hostname 9 * 10 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 11 */ 12#include <sys/types.h> 13#include <sys/socket.h> 14#include <netinet/in.h> 15#include <arpa/inet.h> 16#include <netdb.h> 17#include <stdio.h> 18 19main(argc, argv) 20int argc; 21char **argv; 22{ 23 struct hostent *hp; 24 25 if (argc != 2) { 26 fprintf(stderr, "usage: %s hostname\n", argv[0]); 27 exit(1); 28 } 29 if (hp = gethostbyname(argv[1])) { 30 printf("Hostname:\t%s\n", hp->h_name); 31 printf("Aliases:\t"); 32 while (hp->h_aliases[0]) 33 printf("%s ", *hp->h_aliases++); 34 printf("\n"); 35 printf("Addresses:\t"); 36 while (hp->h_addr_list[0]) 37 printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++)); 38 printf("\n"); 39 exit(0); 40 } else { 41 fprintf(stderr, "host %s not found\n", argv[1]); 42 exit(1); 43 } 44} 45