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