/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/



#include	<errno.h>
#include	"memory_debug.h"
#include	"task.h"
#include	"netutils.h"

extern SEM netutils_lock;

struct hostent *
gethostent_rr()
{
struct hostent * hp;
struct hostent * h_hp;
char * h_buf;
int h_size;
int er;


	h_size = 100;
	h_hp = d_alloc(sizeof(*h_hp),11);
	h_buf = d_alloc(h_size,12);
	for ( ; ; ) {
		hp = gethostent_r(
			h_hp,
			h_buf,
			h_size,
			&er);
		if ( hp )
			break;
		if ( errno != ERANGE )
			break;
		h_size += 100;
		h_buf = d_re_alloc(h_buf,h_size);
	}


	if ( hp ) {
		set_buffer(h_hp);
		set_buffer(h_buf);
		return hp;
	}
	else {
		d_f_ree(h_hp);
		d_f_ree(h_buf);
		return 0;
	}
}
