summaryrefslogtreecommitdiff
path: root/lib/xgethostname.c
blob: f00d7bd5f53e35682b3abeef675ab8e94e00c4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <sys/types.h>

int gethostname ();
char *xmalloc ();
char *xrealloc ();

#define INITIAL_HOSTNAME_LENGTH 33

char *
xgethostname ()
{
  char *hostname;
  size_t size;
  int err;

  size = INITIAL_HOSTNAME_LENGTH;
  while (1)
    {
      hostname = xmalloc (size);
      hostname[size - 1] = '\0';
      err = gethostname (hostname, size);
      if (err || hostname[size - 1] == '\0')
	break;
      size *= 2;
      hostname = xrealloc (hostname, size);
    }

  return hostname;
}