1 /* Test case for gethostbyname_r bug when buffer expansion required. */
14 const char *host = "www.gnu.org";
16 /* This code approximates the example code in the library manual. */
18 struct hostent hostbuf, *hp;
24 hstbuflen = 16; /* Make it way small to ensure ERANGE. */
25 /* Allocate buffer, remember to free it to avoid memory leakage. */
26 tmphstbuf = malloc (hstbuflen);
28 while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
29 &hp, &herr)) == ERANGE)
31 /* Enlarge the buffer. */
33 tmphstbuf = realloc (tmphstbuf, hstbuflen);
36 if (res != 0 || hp == NULL)
38 printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res));
40 if (access ("/etc/resolv.conf", R_OK))
42 puts ("DNS probably not set up");
49 printf ("Got: %s %s\n", hp->h_name,
50 inet_ntoa (*(struct in_addr *) hp->h_addr));