Split the HOST_NAME_MAX detection into a separate m4 macro
[gnulib.git] / m4 / gethostname.m4
1 # gethostname.m4 serial 13
2 dnl Copyright (C) 2002, 2008-2011 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 it 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_LINK_IFELSE([AC_LANG_PROGRAM([[
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     HAVE_GETHOSTNAME=0
41   fi
42
43   gl_PREREQ_HOST_NAME_MAX
44 ])
45
46 dnl Also provide HOST_NAME_MAX when <limits.h> lacks it.
47 AC_DEFUN([gl_PREREQ_HOST_NAME_MAX], [
48
49   dnl - On most Unix systems, use MAXHOSTNAMELEN from <sys/param.h> instead.
50   dnl - On Solaris, Cygwin, BeOS, use MAXHOSTNAMELEN from <netdb.h> instead.
51   dnl - On mingw, use 256, because
52   dnl   <http://msdn.microsoft.com/en-us/library/ms738527.aspx> says:
53   dnl   "if a buffer of 256 bytes is passed in the name parameter and
54   dnl    the namelen parameter is set to 256, the buffer size will always
55   dnl    be adequate."
56   dnl With this, there is no need to use sysconf (_SC_HOST_NAME_MAX), which
57   dnl is not a compile-time constant.
58   dnl We cannot override <limits.h> using the usual technique, because
59   dnl gl_CHECK_NEXT_HEADERS does not work for <limits.h>. Therefore retrieve
60   dnl the value of HOST_NAME_MAX at configure time.
61   AC_CHECK_HEADERS_ONCE([sys/param.h])
62   AC_CHECK_HEADERS_ONCE([sys/socket.h])
63   AC_CHECK_HEADERS_ONCE([netdb.h])
64   AC_CACHE_CHECK([for HOST_NAME_MAX], [gl_cv_decl_HOST_NAME_MAX], [
65     gl_cv_decl_HOST_NAME_MAX=
66     AC_EGREP_CPP([lucky], [
67 #include <limits.h>
68 #ifdef HOST_NAME_MAX
69 lucky
70 #endif
71       ], [gl_cv_decl_HOST_NAME_MAX=yes])
72     if test -z "$gl_cv_decl_HOST_NAME_MAX"; then
73       dnl It's not defined in <limits.h>. Substitute it.
74       if test "$gl_cv_w32_gethostname" = yes; then
75         dnl mingw.
76         gl_cv_decl_HOST_NAME_MAX=256
77       else
78         _AC_COMPUTE_INT([MAXHOSTNAMELEN], [gl_cv_decl_HOST_NAME_MAX], [
79 #include <sys/types.h>
80 #if HAVE_SYS_PARAM_H
81 # include <sys/param.h>
82 #endif
83 #if HAVE_SYS_SOCKET_H
84 # include <sys/socket.h>
85 #endif
86 #if HAVE_NETDB_H
87 # include <netdb.h>
88 #endif
89 ],
90           [dnl The system does not define MAXHOSTNAMELEN in any of the common
91            dnl headers. Use a safe fallback.
92            gl_cv_decl_HOST_NAME_MAX=256
93           ])
94       fi
95     fi
96   ])
97   if test "$gl_cv_decl_HOST_NAME_MAX" != yes; then
98     AC_DEFINE_UNQUOTED([HOST_NAME_MAX], [$gl_cv_decl_HOST_NAME_MAX],
99       [Define HOST_NAME_MAX when <limits.h> does not define it.])
100   fi
101 ])
102
103 # Prerequisites of lib/gethostname.c.
104 AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
105   if test "$gl_cv_w32_gethostname" != "yes"; then
106     AC_CHECK_FUNCS([uname])
107   fi
108 ])