gethostname.m4 3.4 KB

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