This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug manual/2801] Bug in gethostbyname_r() example


------- 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]