sagethostname.c (635B)
1 /* ISC license. */ 2 3 #include <unistd.h> 4 #include <string.h> 5 #include <errno.h> 6 #include <skalibs/stralloc.h> 7 #include <skalibs/djbunix.h> 8 9 int sagethostname (stralloc *sa) 10 { 11 size_t n = 32 ; 12 int e = errno ; 13 int wasnull = !sa->s ; 14 15 for (;;) 16 { 17 if (!stralloc_readyplus(sa, n)) goto err ; 18 sa->s[sa->len + n - 2] = 0 ; 19 errno = 0 ; 20 if (gethostname(sa->s + sa->len, n) < 0) 21 { 22 if (errno != ENAMETOOLONG) goto err ; 23 } 24 else if (!sa->s[sa->len + n - 2]) break ; 25 n += 32 ; 26 } 27 sa->len += strlen(sa->s + sa->len) ; 28 errno = e ; 29 return 0 ; 30 31 err: 32 if (wasnull) stralloc_free(sa) ; 33 return -1 ; 34 }