summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2009-08-02 11:54:52 +0200
committerBruno Haible <bruno@clisp.org>2009-08-02 11:54:52 +0200
commit33bcc047c11586cff59d22566226e847e0c23954 (patch)
tree0319bf42a4f720254a05943586a4f60fdc43a808
parent83c5d5d57bbaa15ad770ff0166e9fc6319402d76 (diff)
downloadgnulib-33bcc047c11586cff59d22566226e847e0c23954.tar.gz
Implement gethostname correctly for native Windows.
-rw-r--r--ChangeLog8
-rw-r--r--lib/gethostname.c30
-rw-r--r--m4/gethostname.m435
-rw-r--r--modules/gethostname7
-rw-r--r--modules/gethostname-tests1
5 files changed, 74 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 005290eb59..68506e242c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-03-31 Simon Josefsson <simon@josefsson.org>
+
+ * lib/gethostname.c: Add Windows wrapper.
+ * m4/gethostname.m4: Look for gethostname in -lws2_32.
+ * modules/gethostname: Depend on sys_socket & errno, for also
+ added lib/w32sock.h. Add GETHOSTNAME_LIB link directive.
+ * modules/gethostname-tests: Link to @GETHOSTNAME_LIB@.
+
2009-07-31 Jim Meyering <meyering@redhat.com>
getloadavg: fix symbol name in comment
diff --git a/lib/gethostname.c b/lib/gethostname.c
index acff351d87..782c4028f0 100644
--- a/lib/gethostname.c
+++ b/lib/gethostname.c
@@ -1,6 +1,6 @@
/* gethostname emulation for SysV and POSIX.1.
- Copyright (C) 1992, 2003, 2006, 2008 Free Software Foundation, Inc.
+ Copyright (C) 1992, 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -15,10 +15,13 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-/* David MacKenzie <djm@gnu.ai.mit.edu> */
+/* David MacKenzie <djm@gnu.ai.mit.edu>
+ Windows port by Simon Josefsson <simon@josefsson.org> */
#include <config.h>
+#if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
+
/* Specification. */
#include <unistd.h>
@@ -54,3 +57,26 @@ gethostname (char *name, size_t len)
#endif
return 0;
}
+
+#else
+
+#define WIN32_LEAN_AND_MEAN
+/* Get winsock2.h. */
+#include <unistd.h>
+
+/* Get set_winsock_errno. */
+#include "w32sock.h"
+
+#undef gethostname
+
+int
+rpl_gethostname (char *name, size_t namelen)
+{
+ int r = gethostname (name, (int) namelen);
+ if (r < 0)
+ set_winsock_errno ();
+
+ return r;
+}
+
+#endif
diff --git a/m4/gethostname.m4 b/m4/gethostname.m4
index 6b6fca95c2..620e023c83 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,
@@ -8,8 +8,33 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME],
[
AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
gl_PREREQ_SYS_H_WINSOCK2
- AC_REPLACE_FUNCS([gethostname])
- if test $ac_cv_func_gethostname = no; then
+
+ dnl Where is gethostname() defined?
+ dnl - On native Windows, it is in ws2_32.dll.
+ dnl - Otherwise is is in libc.
+ 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
+ ])
+ AC_SUBST([GETHOSTNAME_LIB])
+
+ if test "$ac_cv_func_gethostname" = no; then
+ AC_LIBOBJ([gethostname])
HAVE_GETHOSTNAME=0
gl_PREREQ_GETHOSTNAME
fi
@@ -17,5 +42,7 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME],
# Prerequisites of lib/gethostname.c.
AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
- AC_CHECK_FUNCS([uname])
+ if test "$gl_cv_w32_gethostname" != "yes"; then
+ AC_CHECK_FUNCS([uname])
+ fi
])
diff --git a/modules/gethostname b/modules/gethostname
index 796dc6fea9..e21afe6a9c 100644
--- a/modules/gethostname
+++ b/modules/gethostname
@@ -4,10 +4,12 @@ gethostname() function: Return machine's hostname.
Files:
lib/gethostname.c
m4/gethostname.m4
-m4/sys_socket_h.m4
+lib/w32sock.h
Depends-on:
unistd
+sys_socket
+errno
configure.ac:
gl_FUNC_GETHOSTNAME
@@ -18,6 +20,9 @@ Makefile.am:
Include:
<unistd.h>
+Link:
+$(GETHOSTNAME_LIB)
+
License:
LGPLv2+
diff --git a/modules/gethostname-tests b/modules/gethostname-tests
index 236ca58519..ea17aba2d9 100644
--- a/modules/gethostname-tests
+++ b/modules/gethostname-tests
@@ -8,3 +8,4 @@ configure.ac:
Makefile.am:
TESTS += test-gethostname
check_PROGRAMS += test-gethostname
+test_gethostname_LDADD = $(LDADD) @GETHOSTNAME_LIB@