Blame m4/select.m4

Packit 8f70b4
# select.m4 serial 10
Packit 8f70b4
dnl Copyright (C) 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
dnl This file is free software; the Free Software Foundation
Packit 8f70b4
dnl gives unlimited permission to copy and/or distribute it,
Packit 8f70b4
dnl with or without modifications, as long as this notice is preserved.
Packit 8f70b4
Packit 8f70b4
AC_DEFUN([gl_FUNC_SELECT],
Packit 8f70b4
[
Packit 8f70b4
  AC_REQUIRE([gl_HEADER_SYS_SELECT])
Packit 8f70b4
  AC_REQUIRE([AC_C_RESTRICT])
Packit 8f70b4
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 8f70b4
  AC_REQUIRE([gl_SOCKETS])
Packit 8f70b4
  if test "$ac_cv_header_winsock2_h" = yes; then
Packit 8f70b4
    REPLACE_SELECT=1
Packit 8f70b4
  else
Packit 8f70b4
    dnl On Interix 3.5, select(0, NULL, NULL, NULL, timeout) fails with error
Packit 8f70b4
    dnl EFAULT.
Packit 8f70b4
    AC_CHECK_HEADERS_ONCE([sys/select.h])
Packit 8f70b4
    AC_CACHE_CHECK([whether select supports a 0 argument],
Packit 8f70b4
      [gl_cv_func_select_supports0],
Packit 8f70b4
      [
Packit 8f70b4
        AC_RUN_IFELSE([AC_LANG_SOURCE([[
Packit 8f70b4
#include <sys/types.h>
Packit 8f70b4
#include <sys/time.h>
Packit 8f70b4
#if HAVE_SYS_SELECT_H
Packit 8f70b4
#include <sys/select.h>
Packit 8f70b4
#endif
Packit 8f70b4
int main ()
Packit 8f70b4
{
Packit 8f70b4
  struct timeval timeout;
Packit 8f70b4
  timeout.tv_sec = 0;
Packit 8f70b4
  timeout.tv_usec = 5;
Packit 8f70b4
  return select (0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout) < 0;
Packit 8f70b4
}]])], [gl_cv_func_select_supports0=yes], [gl_cv_func_select_supports0=no],
Packit 8f70b4
          [
Packit 8f70b4
changequote(,)dnl
Packit 8f70b4
           case "$host_os" in
Packit 8f70b4
                       # Guess no on Interix.
Packit 8f70b4
             interix*) gl_cv_func_select_supports0="guessing no";;
Packit 8f70b4
                       # Guess yes otherwise.
Packit 8f70b4
             *)        gl_cv_func_select_supports0="guessing yes";;
Packit 8f70b4
           esac
Packit 8f70b4
changequote([,])dnl
Packit 8f70b4
          ])
Packit 8f70b4
      ])
Packit 8f70b4
    case "$gl_cv_func_select_supports0" in
Packit 8f70b4
      *yes) ;;
Packit 8f70b4
      *) REPLACE_SELECT=1 ;;
Packit 8f70b4
    esac
Packit 8f70b4
Packit 8f70b4
    dnl On FreeBSD 8.2, select() doesn't always reject bad fds.
Packit 8f70b4
    AC_CACHE_CHECK([whether select detects invalid fds],
Packit 8f70b4
      [gl_cv_func_select_detects_ebadf],
Packit 8f70b4
      [
Packit 8f70b4
        AC_RUN_IFELSE([AC_LANG_PROGRAM([[
Packit 8f70b4
#include <sys/types.h>
Packit 8f70b4
#include <sys/time.h>
Packit 8f70b4
#if HAVE_SYS_SELECT_H
Packit 8f70b4
# include <sys/select.h>
Packit 8f70b4
#endif
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
]],[[
Packit 8f70b4
  fd_set set;
Packit 8f70b4
  dup2(0, 16);
Packit 8f70b4
  FD_ZERO(&set);
Packit 8f70b4
  FD_SET(16, &set);
Packit 8f70b4
  close(16);
Packit 8f70b4
  struct timeval timeout;
Packit 8f70b4
  timeout.tv_sec = 0;
Packit 8f70b4
  timeout.tv_usec = 5;
Packit 8f70b4
  return select (17, &set, NULL, NULL, &timeout) != -1 || errno != EBADF;
Packit 8f70b4
]])], [gl_cv_func_select_detects_ebadf=yes],
Packit 8f70b4
      [gl_cv_func_select_detects_ebadf=no],
Packit 8f70b4
          [
Packit 8f70b4
           case "$host_os" in
Packit 8f70b4
                             # Guess yes on Linux systems.
Packit 8f70b4
            linux-* | linux) gl_cv_func_select_detects_ebadf="guessing yes" ;;
Packit 8f70b4
                             # Guess yes on glibc systems.
Packit 8f70b4
            *-gnu* | gnu*)   gl_cv_func_select_detects_ebadf="guessing yes" ;;
Packit 8f70b4
                             # If we don't know, assume the worst.
Packit 8f70b4
            *)               gl_cv_func_select_detects_ebadf="guessing no" ;;
Packit 8f70b4
           esac
Packit 8f70b4
          ])
Packit 8f70b4
      ])
Packit 8f70b4
    case $gl_cv_func_select_detects_ebadf in
Packit 8f70b4
      *yes) ;;
Packit 8f70b4
      *) REPLACE_SELECT=1 ;;
Packit 8f70b4
    esac
Packit 8f70b4
  fi
Packit 8f70b4
Packit 8f70b4
  dnl Determine the needed libraries.
Packit 8f70b4
  LIB_SELECT="$LIBSOCKET"
Packit 8f70b4
  if test $REPLACE_SELECT = 1; then
Packit 8f70b4
    case "$host_os" in
Packit 8f70b4
      mingw*)
Packit 8f70b4
        dnl On the MSVC platform, the function MsgWaitForMultipleObjects
Packit 8f70b4
        dnl (used in lib/select.c) requires linking with -luser32. On mingw,
Packit 8f70b4
        dnl it is implicit.
Packit 8f70b4
        AC_LINK_IFELSE(
Packit 8f70b4
          [AC_LANG_SOURCE([[
Packit 8f70b4
#define WIN32_LEAN_AND_MEAN
Packit 8f70b4
#include <windows.h>
Packit 8f70b4
int
Packit 8f70b4
main ()
Packit 8f70b4
{
Packit 8f70b4
  MsgWaitForMultipleObjects (0, NULL, 0, 0, 0);
Packit 8f70b4
  return 0;
Packit 8f70b4
}]])],
Packit 8f70b4
          [],
Packit 8f70b4
          [LIB_SELECT="$LIB_SELECT -luser32"])
Packit 8f70b4
        ;;
Packit 8f70b4
    esac
Packit 8f70b4
  fi
Packit 8f70b4
  AC_SUBST([LIB_SELECT])
Packit 8f70b4
])