This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug manual/2801] Bug in gethostbyname_r() example
- From: "andyparkins at gmail dot com" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 19 Jun 2006 15:02:10 -0000
- Subject: [Bug manual/2801] Bug in gethostbyname_r() example
- References: <20060619145117.2801.andyparkins@gmail.com>
- Reply-to: sourceware-bugzilla at sourceware dot org
------- Additional Comments From andyparkins at gmail dot com 2006-06-19 15:02 -------
In case it's of use, the following would work (but it's not very pretty):
/* Called like this:
struct hostent myhost;
char *auxbuf;
auxbuf = gethostname( myhost, "www.example.com" );
if( auxbuf ) {
/* ... Do something with myhost ... */
free( auxbuf );
}
*/
char *gethostname( struct hostent *hostbuf, const char *host)
{
struct hostent hostbuf, *hp;
size_t hstbuflen;
char *tmphstbuf;
int res;
int herr;
hstbuflen = 1024;
tmphstbuf = malloc (hstbuflen);
while ((res = gethostbyname_r( host, hostbuf, tmphstbuf, hstbuflen,
&hp, &herr)) == ERANGE)
{
/* Enlarge the buffer. */
hstbuflen *= 2;
tmphstbuf = realloc (tmphstbuf, hstbuflen);
}
/* Check for errors. */
if (res || hp == NULL)
return NULL;
return tmphstbuf;
}
--
http://sourceware.org/bugzilla/show_bug.cgi?id=2801
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.