initial import
[ICEs.git] / 199025 / gethostbyname.c
1 #define _GNU_SOURCE
2 #include <netdb.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <arpa/inet.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9
10 int main(int argc, char *argv[])
11 {
12 const char *name = (argc > 1 ? argv[1] : "localhost");
13 printf("looking up '%s'\n", name);
14
15 struct hostent *hent = gethostbyname(name);
16 if (!hent) {
17 herror("gethostbyname() failed");
18 return 1;
19 }
20
21 size_t i;
22 printf("h_name = %s\n", hent->h_name);
23 printf("h_aliases = { ");
24 for (i = 0; hent->h_aliases[i]; ++i)
25 printf("%s'%s'", (i ? ", " : ""), hent->h_aliases[i]);
26 printf(" }\n");
27 printf("h_addr_list = { ");
28 for (i = 0; hent->h_addr_list[i]; ++i) {
29 struct in_addr addr;
30 memcpy(&addr, hent->h_addr_list[i], sizeof(addr));
31 printf("%s'%s'", (i ? ", " : ""), inet_ntoa(addr));
32 }
33 printf(" }\n");
34
35 return 0;
36 }