immutable: Add tests.
[gnulib.git] / m4 / poll.m4
blob80e991142f888d80db7669a8431e82090d88e906
1 # poll.m4 serial 20
2 dnl Copyright (c) 2003, 2005-2007, 2009-2021 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.
7 AC_DEFUN([gl_FUNC_POLL],
9   AC_REQUIRE([gl_POLL_H])
10   AC_REQUIRE([gl_SOCKETS])
11   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12   if test $ac_cv_header_poll_h = no; then
13     ac_cv_func_poll=no
14     gl_cv_func_poll=no
15   else
16     AC_CHECK_FUNC([poll],
17       [# Check whether poll() works on special files (like /dev/null) and
18        # and ttys (like /dev/tty). On Mac OS X 10.4.0 and AIX 5.3, it doesn't.
19        AC_RUN_IFELSE([AC_LANG_SOURCE([[
20 #include <fcntl.h>
21 #include <poll.h>
22 ]GL_MDA_DEFINES[
23          int main()
24          {
25            int result = 0;
26            struct pollfd ufd;
27            /* Try /dev/null for reading.  */
28            ufd.fd = open ("/dev/null", O_RDONLY);
29            /* If /dev/null does not exist, it's not Mac OS X nor AIX. */
30            if (ufd.fd >= 0)
31              {
32                ufd.events = POLLIN;
33                ufd.revents = 0;
34                if (!(poll (&ufd, 1, 0) == 1 && ufd.revents == POLLIN))
35                  result |= 1;
36              }
37            /* Try /dev/null for writing.  */
38            ufd.fd = open ("/dev/null", O_WRONLY);
39            /* If /dev/null does not exist, it's not Mac OS X nor AIX. */
40            if (ufd.fd >= 0)
41              {
42                ufd.events = POLLOUT;
43                ufd.revents = 0;
44                if (!(poll (&ufd, 1, 0) == 1 && ufd.revents == POLLOUT))
45                  result |= 2;
46              }
47            /* Trying /dev/tty may be too environment dependent.  */
48            return result;
49          }]])],
50          [gl_cv_func_poll=yes],
51          [gl_cv_func_poll=no],
52          [# When cross-compiling, assume that poll() works everywhere except on
53           # Mac OS X or AIX, regardless of its version.
54           AC_EGREP_CPP([MacOSX], [
55 #if (defined(__APPLE__) && defined(__MACH__)) || defined(_AIX)
56 This is MacOSX or AIX
57 #endif
58 ], [gl_cv_func_poll="guessing no"], [gl_cv_func_poll="guessing yes"])])])
59   fi
60   case "$gl_cv_func_poll" in
61     *yes) ;;
62     *)
63       AC_CHECK_FUNC([poll], [ac_cv_func_poll=yes], [ac_cv_func_poll=no])
64       if test $ac_cv_func_poll = no; then
65         HAVE_POLL=0
66       else
67         REPLACE_POLL=1
68       fi
69       ;;
70   esac
71   if test $HAVE_POLL = 0 || test $REPLACE_POLL = 1; then
72     :
73   else
74     AC_DEFINE([HAVE_POLL], [1],
75       [Define to 1 if you have the 'poll' function and it works.])
76   fi
78   dnl Determine the needed libraries.
79   LIB_POLL="$LIBSOCKET"
80   if test $HAVE_POLL = 0 || test $REPLACE_POLL = 1; then
81     case "$host_os" in
82       mingw*)
83         dnl On the MSVC platform, the function MsgWaitForMultipleObjects
84         dnl (used in lib/poll.c) requires linking with -luser32. On mingw,
85         dnl it is implicit.
86         AC_LINK_IFELSE(
87           [AC_LANG_SOURCE([[
88 #define WIN32_LEAN_AND_MEAN
89 #include <windows.h>
90 int
91 main ()
93   MsgWaitForMultipleObjects (0, NULL, 0, 0, 0);
94   return 0;
95 }]])],
96           [],
97           [LIB_POLL="$LIB_POLL -luser32"])
98         ;;
99     esac
100   fi
101   AC_SUBST([LIB_POLL])
104 # Prerequisites of lib/poll.c.
105 AC_DEFUN([gl_PREREQ_POLL],
107   AC_CHECK_HEADERS_ONCE([sys/ioctl.h sys/filio.h])