select.m4 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # select.m4 serial 8
  2. dnl Copyright (C) 2009-2017 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_HEADER_SYS_SELECT])
  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. ]],[[
  62. fd_set set;
  63. dup2(0, 16);
  64. FD_ZERO(&set);
  65. FD_SET(16, &set);
  66. close(16);
  67. struct timeval timeout;
  68. timeout.tv_sec = 0;
  69. timeout.tv_usec = 5;
  70. return select (17, &set, NULL, NULL, &timeout) != -1 || errno != EBADF;
  71. ]])], [gl_cv_func_select_detects_ebadf=yes],
  72. [gl_cv_func_select_detects_ebadf=no],
  73. [
  74. case "$host_os" in
  75. # Guess yes on glibc systems.
  76. *-gnu*) gl_cv_func_select_detects_ebadf="guessing yes" ;;
  77. # If we don't know, assume the worst.
  78. *) gl_cv_func_select_detects_ebadf="guessing no" ;;
  79. esac
  80. ])
  81. ])
  82. case $gl_cv_func_select_detects_ebadf in
  83. *yes) ;;
  84. *) REPLACE_SELECT=1 ;;
  85. esac
  86. fi
  87. dnl Determine the needed libraries.
  88. LIB_SELECT="$LIBSOCKET"
  89. if test $REPLACE_SELECT = 1; then
  90. case "$host_os" in
  91. mingw*)
  92. dnl On the MSVC platform, the function MsgWaitForMultipleObjects
  93. dnl (used in lib/select.c) requires linking with -luser32. On mingw,
  94. dnl it is implicit.
  95. AC_LINK_IFELSE(
  96. [AC_LANG_SOURCE([[
  97. #define WIN32_LEAN_AND_MEAN
  98. #include <windows.h>
  99. int
  100. main ()
  101. {
  102. MsgWaitForMultipleObjects (0, NULL, 0, 0, 0);
  103. return 0;
  104. }]])],
  105. [],
  106. [LIB_SELECT="$LIB_SELECT -luser32"])
  107. ;;
  108. esac
  109. fi
  110. AC_SUBST([LIB_SELECT])
  111. ])