select.m4 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # select.m4 serial 14
  2. dnl Copyright (C) 2009-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. AC_DEFUN([gl_FUNC_SELECT],
  7. [
  8. AC_REQUIRE([gl_SYS_SELECT_H])
  9. AC_REQUIRE([AC_C_RESTRICT])
  10. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  11. AC_REQUIRE([gl_SOCKETS])
  12. if test "$ac_cv_header_winsock2_h" = yes; then
  13. REPLACE_SELECT=1
  14. else
  15. dnl On Interix 3.5, select(0, NULL, NULL, NULL, timeout) fails with error
  16. dnl EFAULT.
  17. AC_CHECK_HEADERS_ONCE([sys/select.h])
  18. AC_CACHE_CHECK([whether select supports a 0 argument],
  19. [gl_cv_func_select_supports0],
  20. [
  21. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  22. #include <sys/types.h>
  23. #include <sys/time.h>
  24. #if HAVE_SYS_SELECT_H
  25. #include <sys/select.h>
  26. #endif
  27. int main ()
  28. {
  29. struct timeval timeout;
  30. timeout.tv_sec = 0;
  31. timeout.tv_usec = 5;
  32. return select (0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout) < 0;
  33. }]])], [gl_cv_func_select_supports0=yes], [gl_cv_func_select_supports0=no],
  34. [
  35. changequote(,)dnl
  36. case "$host_os" in
  37. # Guess no on Interix.
  38. interix*) gl_cv_func_select_supports0="guessing no";;
  39. # Guess yes otherwise.
  40. *) gl_cv_func_select_supports0="guessing yes";;
  41. esac
  42. changequote([,])dnl
  43. ])
  44. ])
  45. case "$gl_cv_func_select_supports0" in
  46. *yes) ;;
  47. *) REPLACE_SELECT=1 ;;
  48. esac
  49. dnl On FreeBSD 8.2, select() doesn't always reject bad fds.
  50. AC_CACHE_CHECK([whether select detects invalid fds],
  51. [gl_cv_func_select_detects_ebadf],
  52. [
  53. AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  54. #include <sys/types.h>
  55. #include <sys/time.h>
  56. #if HAVE_SYS_SELECT_H
  57. # include <sys/select.h>
  58. #endif
  59. #include <unistd.h>
  60. #include <errno.h>
  61. ]GL_MDA_DEFINES],
  62. [[
  63. fd_set set;
  64. dup2(0, 16);
  65. FD_ZERO(&set);
  66. FD_SET(16, &set);
  67. close(16);
  68. struct timeval timeout;
  69. timeout.tv_sec = 0;
  70. timeout.tv_usec = 5;
  71. return select (17, &set, NULL, NULL, &timeout) != -1 || errno != EBADF;
  72. ]])], [gl_cv_func_select_detects_ebadf=yes],
  73. [gl_cv_func_select_detects_ebadf=no],
  74. [
  75. case "$host_os" in
  76. # Guess yes on Linux systems.
  77. linux-* | linux) gl_cv_func_select_detects_ebadf="guessing yes" ;;
  78. # Guess yes on glibc systems.
  79. *-gnu* | gnu*) gl_cv_func_select_detects_ebadf="guessing yes" ;;
  80. # If we don't know, obey --enable-cross-guesses.
  81. *) gl_cv_func_select_detects_ebadf="$gl_cross_guess_normal" ;;
  82. esac
  83. ])
  84. ])
  85. case $gl_cv_func_select_detects_ebadf in
  86. *yes) ;;
  87. *) REPLACE_SELECT=1 ;;
  88. esac
  89. fi
  90. dnl Determine the needed libraries.
  91. SELECT_LIB="$LIBSOCKET"
  92. if test $REPLACE_SELECT = 1; then
  93. case "$host_os" in
  94. mingw*)
  95. dnl On the MSVC platform, the function MsgWaitForMultipleObjects
  96. dnl (used in lib/select.c) requires linking with -luser32. On mingw,
  97. dnl it is implicit.
  98. AC_LINK_IFELSE(
  99. [AC_LANG_SOURCE([[
  100. #define WIN32_LEAN_AND_MEAN
  101. #include <windows.h>
  102. int
  103. main ()
  104. {
  105. MsgWaitForMultipleObjects (0, NULL, 0, 0, 0);
  106. return 0;
  107. }]])],
  108. [],
  109. [SELECT_LIB="$SELECT_LIB -luser32"])
  110. ;;
  111. esac
  112. fi
  113. AC_SUBST([SELECT_LIB])
  114. dnl For backward compatibility.
  115. LIB_SELECT="$LIB_SELECT"
  116. AC_SUBST([LIB_SELECT])
  117. ])