Ensure HOST_NAME_MAX as part of the gethostname module.
[pspp] / m4 / gethostname.m4
1 # gethostname.m4 serial 7
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 # Ensure
8 # - the gethostname() function,
9 # - the HOST_NAME_MAX macro in <limits.h>.
10 AC_DEFUN([gl_FUNC_GETHOSTNAME],
11 [
12   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
13   gl_PREREQ_SYS_H_WINSOCK2
14
15   dnl Where is gethostname() defined?
16   dnl - On native Windows, it is in ws2_32.dll.
17   dnl - Otherwise is is in libc.
18   GETHOSTNAME_LIB=
19   AC_CHECK_FUNCS([gethostname], , [
20     AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32],
21       [gl_cv_w32_gethostname],
22       [gl_cv_w32_gethostname=no
23        gl_save_LIBS="$LIBS"
24        LIBS="$LIBS -lws2_32"
25        AC_TRY_LINK([
26 #ifdef HAVE_WINSOCK2_H
27 #include <winsock2.h>
28 #endif
29 #include <stddef.h>
30 ], [gethostname(NULL, 0);], [gl_cv_w32_gethostname=yes])
31        LIBS="$gl_save_LIBS"
32       ])
33     if test "$gl_cv_w32_gethostname" = "yes"; then
34       GETHOSTNAME_LIB="-lws2_32"
35     fi
36   ])
37   AC_SUBST([GETHOSTNAME_LIB])
38
39   if test "$ac_cv_func_gethostname" = no; then
40     AC_LIBOBJ([gethostname])
41     HAVE_GETHOSTNAME=0
42     gl_PREREQ_GETHOSTNAME
43   fi
44
45   dnl Also provide HOST_NAME_MAX when <limits.h> lacks it.
46   if test "$gl_cv_w32_gethostname" = "yes"; then
47     # <http://msdn.microsoft.com/en-us/library/ms738527.aspx> says:
48     # "if a buffer of 256 bytes is passed in the name parameter and
49     # the namelen parameter is set to 256, the buffer size will always
50     # be adequate."
51     AC_DEFINE([HOST_NAME_MAX], [256],
52       [Define HOST_NAME_MAX when <limits.h> does not define it.])
53   fi
54 ])
55
56 # Prerequisites of lib/gethostname.c.
57 AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
58   if test "$gl_cv_w32_gethostname" != "yes"; then
59     AC_CHECK_FUNCS([uname])
60   fi
61 ])