select.m4 3.2 KB

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