gethostname.m4 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # gethostname.m4 serial 9
  2. dnl Copyright (C) 2002, 2008, 2009, 2010 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. # Ensure
  7. # - the gethostname() function,
  8. # - the HOST_NAME_MAX macro in <limits.h>.
  9. AC_DEFUN([gl_FUNC_GETHOSTNAME],
  10. [
  11. AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
  12. gl_PREREQ_SYS_H_WINSOCK2
  13. dnl Where is gethostname() defined?
  14. dnl - On native Windows, it is in ws2_32.dll.
  15. dnl - Otherwise is is in libc.
  16. GETHOSTNAME_LIB=
  17. AC_CHECK_FUNCS([gethostname], , [
  18. AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32],
  19. [gl_cv_w32_gethostname],
  20. [gl_cv_w32_gethostname=no
  21. gl_save_LIBS="$LIBS"
  22. LIBS="$LIBS -lws2_32"
  23. AC_LINK_IFELSE([AC_LANG_PROGRAM([[
  24. #ifdef HAVE_WINSOCK2_H
  25. #include <winsock2.h>
  26. #endif
  27. #include <stddef.h>
  28. ]], [[gethostname(NULL, 0);]])], [gl_cv_w32_gethostname=yes])
  29. LIBS="$gl_save_LIBS"
  30. ])
  31. if test "$gl_cv_w32_gethostname" = "yes"; then
  32. GETHOSTNAME_LIB="-lws2_32"
  33. fi
  34. ])
  35. AC_SUBST([GETHOSTNAME_LIB])
  36. if test "$ac_cv_func_gethostname" = no; then
  37. AC_LIBOBJ([gethostname])
  38. HAVE_GETHOSTNAME=0
  39. gl_PREREQ_GETHOSTNAME
  40. fi
  41. dnl Also provide HOST_NAME_MAX when <limits.h> lacks it.
  42. dnl - On most Unix systems, use MAXHOSTNAMELEN from <sys/param.h> instead.
  43. dnl - On Solaris, Cygwin, BeOS, use MAXHOSTNAMELEN from <netdb.h> instead.
  44. dnl - On mingw, use 256, because
  45. dnl <http://msdn.microsoft.com/en-us/library/ms738527.aspx> says:
  46. dnl "if a buffer of 256 bytes is passed in the name parameter and
  47. dnl the namelen parameter is set to 256, the buffer size will always
  48. dnl be adequate."
  49. dnl With this, there is no need to use sysconf (_SC_HOST_NAME_MAX), which
  50. dnl is not a compile-time constant.
  51. dnl We cannot override <limits.h> using the usual technique, because
  52. dnl gl_CHECK_NEXT_HEADERS does not work for <limits.h>. Therefore retrieve
  53. dnl the value of HOST_NAME_MAX at configure time.
  54. AC_CHECK_HEADERS_ONCE([sys/param.h])
  55. AC_CHECK_HEADERS_ONCE([sys/socket.h])
  56. AC_CHECK_HEADERS_ONCE([netdb.h])
  57. AC_CACHE_CHECK([for HOST_NAME_MAX], [gl_cv_decl_HOST_NAME_MAX], [
  58. gl_cv_decl_HOST_NAME_MAX=
  59. AC_EGREP_CPP([lucky], [
  60. #include <limits.h>
  61. #ifdef HOST_NAME_MAX
  62. lucky
  63. #endif
  64. ], [gl_cv_decl_HOST_NAME_MAX=yes])
  65. if test -z "$gl_cv_decl_HOST_NAME_MAX"; then
  66. dnl It's not defined in <limits.h>. Substitute it.
  67. if test "$gl_cv_w32_gethostname" = yes; then
  68. dnl mingw.
  69. gl_cv_decl_HOST_NAME_MAX=256
  70. else
  71. _AC_COMPUTE_INT([MAXHOSTNAMELEN], [gl_cv_decl_HOST_NAME_MAX], [
  72. #include <sys/types.h>
  73. #if HAVE_SYS_PARAM_H
  74. # include <sys/param.h>
  75. #endif
  76. #if HAVE_SYS_SOCKET_H
  77. # include <sys/socket.h>
  78. #endif
  79. #if HAVE_NETDB_H
  80. # include <netdb.h>
  81. #endif
  82. ])
  83. fi
  84. fi
  85. ])
  86. if test "$gl_cv_decl_HOST_NAME_MAX" != yes; then
  87. AC_DEFINE_UNQUOTED([HOST_NAME_MAX], [$gl_cv_decl_HOST_NAME_MAX],
  88. [Define HOST_NAME_MAX when <limits.h> does not define it.])
  89. fi
  90. ])
  91. # Prerequisites of lib/gethostname.c.
  92. AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
  93. if test "$gl_cv_w32_gethostname" != "yes"; then
  94. AC_CHECK_FUNCS([uname])
  95. fi
  96. ])