summaryrefslogtreecommitdiff
path: root/src/libstddjb/sagethostname.c
blob: b20db392fb649c508df7a8ae058cb00f2ebe3d35 (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
/* ISC license. */

#include <unistd.h>
#include <string.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>

int sagethostname (stralloc *sa)
{
  size_t n = 128 ;
  int wasnull = !sa->s ;

  for (;;)
  {
    if (!stralloc_readyplus(sa, n)) goto err ;
    sa->s[sa->len + n - 2] = 0 ;
    if (gethostname(sa->s + sa->len, n) == -1) goto err ;
    if (!sa->s[sa->len + n - 2]) break ;
    n += 128 ;
  }
  sa->len += strlen(sa->s + sa->len) ;
  return 0 ;

err:
  if (wasnull) stralloc_free(sa) ;
  return -1 ;
}