string-buffer tests: Avoid test failure on native Windows.
[gnulib.git] / m4 / sleep.m4
blobec8764bd2a0530ea95d0f45b572bb37267cc515b
1 # sleep.m4
2 # serial 14
3 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_SLEEP],
10   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
11   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12   dnl We expect to see the declaration of sleep() in a header file.
13   dnl Older versions of mingw have a sleep() function that is an alias to
14   dnl _sleep() in MSVCRT. It has a different signature than POSIX sleep():
15   dnl it takes the number of milliseconds as argument and returns void.
16   dnl mingw does not declare this function.
17   AC_CHECK_DECLS([sleep], , , [[#include <unistd.h>]])
18   AC_CHECK_FUNCS_ONCE([sleep])
19   if test $ac_cv_have_decl_sleep != yes; then
20     HAVE_SLEEP=0
21   else
22     dnl Cygwin 1.5.x has a bug where sleep can't exceed 49.7 days.
23     AC_CACHE_CHECK([for working sleep], [gl_cv_func_sleep_works],
24       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
25 #include <errno.h>
26 #include <unistd.h>
27 #include <signal.h>
28 static void
29 handle_alarm (int sig)
31   if (sig != SIGALRM)
32     _exit (2);
34 ]], [[
35     /* Failure to compile this test due to missing alarm is okay,
36        since all such platforms (mingw, MSVC) also lack sleep.  */
37     unsigned int pentecost = 50 * 24 * 60 * 60; /* 50 days.  */
38     unsigned int remaining;
39     signal (SIGALRM, handle_alarm);
40     alarm (1);
41     remaining = sleep (pentecost);
42     if (remaining > pentecost)
43       return 3;
44     if (remaining <= pentecost - 10)
45       return 4;
46     return 0;
47     ]])],
48       [gl_cv_func_sleep_works=yes], [gl_cv_func_sleep_works=no],
49       [case "$host_os" in
50                             # Guess yes on glibc systems.
51          *-gnu* | gnu*)     gl_cv_func_sleep_works="guessing yes" ;;
52                             # Guess yes on musl systems.
53          *-musl*)           gl_cv_func_sleep_works="guessing yes" ;;
54                             # Guess yes on systems that emulate the Linux system calls.
55          midipix*)          gl_cv_func_sleep_works="guessing yes" ;;
56                             # Guess no on native Windows.
57          mingw* | windows*) gl_cv_func_sleep_works="guessing no" ;;
58                             # If we don't know, obey --enable-cross-guesses.
59          *)                 gl_cv_func_sleep_works="$gl_cross_guess_normal" ;;
60        esac
61       ])])
62     case "$gl_cv_func_sleep_works" in
63       *yes) ;;
64       *)
65         REPLACE_SLEEP=1
66         ;;
67     esac
68   fi