Hello everybody,

W32 has gethostname in ws2_32.dll. This is not detected by
gethostname.m4, and gethostname is inadvertently replaced by a function
that always returns an empty string.

The following patch fixes this for me. It works on Debian and with a
MinGW cross compiler. I tried to change gethostname.m4 to do similar
checks that e.g. hostent.m4 does, but I'm not sure if I got it right,
since M4 and autoconf macros tend to confuse me.

Regards,
Martin


diff --git a/m4/gethostname.m4 b/m4/gethostname.m4
index 6b6fca9..572a140 100644
--- a/m4/gethostname.m4
+++ b/m4/gethostname.m4
@@ -1,4 +1,4 @@
-# gethostname.m4 serial 5
+# gethostname.m4 serial 6
 dnl Copyright (C) 2002, 2008, 2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,11 +6,31 @@ dnl with or without modifications, as long as this notice is 
preserved.
 
 AC_DEFUN([gl_FUNC_GETHOSTNAME],
 [
+  dnl On native Windows, gethostname is in ws2_32.dll
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   gl_PREREQ_SYS_H_WINSOCK2
-  AC_REPLACE_FUNCS([gethostname])
-  if test $ac_cv_func_gethostname = no; then
+  GETHOSTNAME_LIB=
+  AC_CHECK_FUNCS([gethostname], , [
+    AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32],
+      [gl_cv_w32_gethostname],
+      [gl_cv_w32_gethostname=no
+       gl_save_LIBS="$LIBS"
+       LIBS="$LIBS -lws2_32"
+       AC_TRY_LINK([
+#ifdef HAVE_WINSOCK2_H
+#include <winsock2.h>
+#endif
+#include <stddef.h>
+], [gethostname(NULL, 0);], [gl_cv_w32_gethostname=yes])
+       LIBS="$gl_save_LIBS"
+      ])
+    if test "$gl_cv_w32_gethostname" = "yes"; then
+      GETHOSTNAME_LIB="-lws2_32"
+    fi
+  ])
+  if test -z $GETHOSTNAME_LIB -a $ac_cv_func_gethostname = no; then
     HAVE_GETHOSTNAME=0
+    AC_LIBOBJ([gethostname])
     gl_PREREQ_GETHOSTNAME
   fi
 ])
diff --git a/modules/gethostname b/modules/gethostname
index 796dc6f..a9a464c 100644
--- a/modules/gethostname
+++ b/modules/gethostname
@@ -18,6 +18,9 @@ Makefile.am:
 Include:
 <unistd.h>
 
+Link:
+$(GETHOSTNAME_LIB)
+
 License:
 LGPLv2+
 


Reply via email to