summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-01-30 23:01:10 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-01-30 23:01:10 +0000
commit1c2fb80f178650da370cbf86e994a1d74be82a12 (patch)
treec0e995b8443dbfc03d84483969f22c898c776ace
parentd200635a4c624c687b3387f937425d12239eb5c3 (diff)
downloadskalibs-1c2fb80f178650da370cbf86e994a1d74be82a12.tar.gz
skalibs-1c2fb80f178650da370cbf86e994a1d74be82a12.tar.xz
skalibs-1c2fb80f178650da370cbf86e994a1d74be82a12.zip
Make sagethostname() work with all glibcs
-rw-r--r--src/libstddjb/sagethostname.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/libstddjb/sagethostname.c b/src/libstddjb/sagethostname.c
index b20db39..90108f8 100644
--- a/src/libstddjb/sagethostname.c
+++ b/src/libstddjb/sagethostname.c
@@ -2,23 +2,30 @@
 
 #include <unistd.h>
 #include <string.h>
+#include <errno.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/djbunix.h>
 
 int sagethostname (stralloc *sa)
 {
-  size_t n = 128 ;
+  size_t n = 32 ;
+  int e = errno ;
   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 ;
+    errno = 0 ;
+    if (gethostname(sa->s + sa->len, n) < 0)
+    {
+      if (errno != ENAMETOOLONG) goto err ;
+    }
+    else if (!sa->s[sa->len + n - 2]) break ;
+    n += 32 ;
   }
   sa->len += strlen(sa->s + sa->len) ;
+  errno = e ;
   return 0 ;
 
 err: