/* 
  xgethostname - implimentation of limitless gethostname 
  By Nick Rusnov <nick@fargus.net> 
  This is in the public domain.  
  Do what thou wilt and it shall be the whole of the law.
*/
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include "xgethostname.h"


char 
*xgethostname(void)
{
  char *myhostname = 0, *tmphostname = 0;
  int mylen = 256; /* arbitrary starting value */

  while (1)
    {
      tmphostname = realloc (myhostname,mylen);
      if (tmphostname == NULL)
	{
	  if (myhostname)
	    free (myhostname);

	  return 0;
	}
      myhostname = tmphostname;
      if (!gethostname (myhostname,mylen) && (errno == ERANGE))
	{
	  mylen *= 2;
	}
      else
	{
	  tmphostname = realloc (myhostname,strlen(myhostname)+1);
	  if (tmphostname == NULL)
	    {
	      if (myhostname)
		free (myhostname);
	      return 0;
	    }
	  return tmphostname;
	}
    }
}


