Implement gethostname correctly for native Windows.
[gnulib.git] / m4 / gethostname.m4
1 # gethostname.m4 serial 6
2 dnl Copyright (C) 2002, 2008, 2009 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_GETHOSTNAME],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   gl_PREREQ_SYS_H_WINSOCK2
11
12   dnl Where is gethostname() defined?
13   dnl - On native Windows, it is in ws2_32.dll.
14   dnl - Otherwise is is in libc.
15   GETHOSTNAME_LIB=
16   AC_CHECK_FUNCS([gethostname], , [
17     AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32],
18       [gl_cv_w32_gethostname],
19       [gl_cv_w32_gethostname=no
20        gl_save_LIBS="$LIBS"
21        LIBS="$LIBS -lws2_32"
22        AC_TRY_LINK([
23 #ifdef HAVE_WINSOCK2_H
24 #include <winsock2.h>
25 #endif
26 #include <stddef.h>
27 ], [gethostname(NULL, 0);], [gl_cv_w32_gethostname=yes])
28        LIBS="$gl_save_LIBS"
29       ])
30     if test "$gl_cv_w32_gethostname" = "yes"; then
31       GETHOSTNAME_LIB="-lws2_32"
32     fi
33   ])
34   AC_SUBST([GETHOSTNAME_LIB])
35
36   if test "$ac_cv_func_gethostname" = no; then
37     AC_LIBOBJ([gethostname])
38     HAVE_GETHOSTNAME=0
39     gl_PREREQ_GETHOSTNAME
40   fi
41 ])
42
43 # Prerequisites of lib/gethostname.c.
44 AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
45   if test "$gl_cv_w32_gethostname" != "yes"; then
46     AC_CHECK_FUNCS([uname])
47   fi
48 ])