summaryrefslogtreecommitdiffstats
path: root/auxiliary/name-addr-test/gethostbyname.c
blob: d8079dd73b20c1117ad0d7a1ff07dc1e476eff47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 /*
  * gethostbyname tester. compile with:
  * 
  * cc -o gethostbyname gethostbyname.c (SunOS 4.x)
  * 
  * cc -o gethostbyname gethostbyname.c -lnsl (SunOS 5.x)
  * 
  * run as: gethostbyname hostname
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

main(argc, argv)
int     argc;
char  **argv;
{
    struct hostent *hp;

    if (argc != 2) {
	fprintf(stderr, "usage: %s hostname\n", argv[0]);
	exit(1);
    }
    if (hp = gethostbyname(argv[1])) {
	printf("Hostname:\t%s\n", hp->h_name);
	printf("Aliases:\t");
	while (hp->h_aliases[0])
	    printf("%s ", *hp->h_aliases++);
	printf("\n");
	printf("Addresses:\t");
	while (hp->h_addr_list[0])
	    printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
	printf("\n");
	exit(0);
    } else {
	fprintf(stderr, "host %s not found\n", argv[1]);
	exit(1);
    }
}