Tizen 2.0 Release
[external/tizen-coreutils.git] / m4 / nanosleep.m4
1 #serial 22
2
3 dnl From Jim Meyering.
4 dnl Check for the nanosleep function.
5 dnl If not found, use the supplied replacement.
6 dnl
7
8 # Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007 Free
9 # Software Foundation, Inc.
10
11 # This file is free software; the Free Software Foundation
12 # gives unlimited permission to copy and/or distribute it,
13 # with or without modifications, as long as this notice is preserved.
14
15 AC_DEFUN([gl_FUNC_NANOSLEEP],
16 [
17  dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
18  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
19
20  AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
21  AC_REQUIRE([gl_CLOCK_TIME])
22  AC_CHECK_HEADERS_ONCE(sys/time.h)
23
24  nanosleep_save_libs=$LIBS
25
26  # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
27  # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
28  AC_SEARCH_LIBS([nanosleep], [rt posix4],
29                 [test "$ac_cv_search_nanosleep" = "none required" ||
30                  LIB_NANOSLEEP=$ac_cv_search_nanosleep])
31
32  AC_CACHE_CHECK([for working nanosleep],
33   [gl_cv_func_nanosleep],
34   [
35    AC_RUN_IFELSE(
36      [AC_LANG_SOURCE([[
37         #include <errno.h>
38         #include <limits.h>
39         #include <signal.h>
40         #if HAVE_SYS_TIME_H
41          #include <sys/time.h>
42         #endif
43         #include <time.h>
44         #include <unistd.h>
45         #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
46         #define TYPE_MAXIMUM(t) \
47           ((t) (! TYPE_SIGNED (t) \
48                 ? (t) -1 \
49                 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
50
51         static void
52         check_for_SIGALRM (int sig)
53         {
54           if (sig != SIGALRM)
55             _exit (1);
56         }
57
58         int
59         main ()
60         {
61           static struct timespec ts_sleep;
62           static struct timespec ts_remaining;
63           static struct sigaction act;
64           if (! nanosleep)
65             return 1;
66           act.sa_handler = check_for_SIGALRM;
67           sigemptyset (&act.sa_mask);
68           sigaction (SIGALRM, &act, NULL);
69           ts_sleep.tv_sec = 0;
70           ts_sleep.tv_nsec = 1;
71           alarm (1);
72           if (nanosleep (&ts_sleep, NULL) != 0)
73             return 1;
74           ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
75           ts_sleep.tv_nsec = 999999999;
76           alarm (1);
77           if (nanosleep (&ts_sleep, &ts_remaining) == -1 && errno == EINTR
78               && TYPE_MAXIMUM (time_t) - 10 < ts_remaining.tv_sec)
79             return 0;
80           return 119;
81         }]])],
82      [gl_cv_func_nanosleep=yes],
83      [case $? in dnl (
84       119) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
85       *)   gl_cv_func_nanosleep=no;;
86       esac],
87      [gl_cv_func_nanosleep=cross-compiling])
88   ])
89   if test "$gl_cv_func_nanosleep" = yes; then
90     REPLACE_NANOSLEEP=0
91   else
92     REPLACE_NANOSLEEP=1
93     if test "$gl_cv_func_nanosleep" = 'no (mishandles large arguments)'; then
94       AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], 1,
95         [Define to 1 if nanosleep mishandle large arguments.])
96       for ac_lib in $LIB_CLOCK_GETTIME; do
97         case " $LIB_NANOSLEEP " in
98         *" $ac_lib "*) ;;
99         *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
100         esac
101       done
102     fi
103     AC_LIBOBJ(nanosleep)
104     gl_PREREQ_NANOSLEEP
105   fi
106
107  AC_SUBST([LIB_NANOSLEEP])
108  LIBS=$nanosleep_save_libs
109 ])
110
111 # Prerequisites of lib/nanosleep.c.
112 AC_DEFUN([gl_PREREQ_NANOSLEEP],
113 [
114   AC_CHECK_FUNCS_ONCE(siginterrupt)
115   AC_CHECK_HEADERS_ONCE(sys/select.h)
116 ])