1 /* Test case for gethostbyname_r bug when buffer expansion required. */
13 const char *host
= "www.gnu.org";
15 /* This code approximates the example code in the library manual. */
17 struct hostent hostbuf
, *hp
;
23 hstbuflen
= 16; /* Make it way small to ensure ERANGE. */
24 /* Allocate buffer, remember to free it to avoid memory leakage. */
25 tmphstbuf
= malloc (hstbuflen
);
27 while ((res
= gethostbyname_r (host
, &hostbuf
, tmphstbuf
, hstbuflen
,
28 &hp
, &herr
)) == ERANGE
)
30 /* Enlarge the buffer. */
32 tmphstbuf
= realloc (tmphstbuf
, hstbuflen
);
35 if (res
!= 0 || hp
== NULL
)
37 printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res
));
41 printf ("Got: %s %s\n", hp
->h_name
,
42 inet_ntoa (*(struct in_addr
*) hp
->h_addr
));