1 /* ufuncs - sleep and alarm functions that understand fractional values */
3 /* Copyright (C) 2008,2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
25 #if defined (TIME_WITH_SYS_TIME)
26 # include <sys/time.h>
29 # if defined (HAVE_SYS_TIME_H)
30 # include <sys/time.h>
36 #if defined (HAVE_UNISTD_H)
40 /* A version of `alarm' using setitimer if it's available. */
42 #if defined (HAVE_SETITIMER)
45 unsigned int secs, usecs;
47 struct itimerval it, oit;
49 it.it_interval.tv_sec = 0;
50 it.it_interval.tv_usec = 0;
52 it.it_value.tv_sec = secs;
53 it.it_value.tv_usec = usecs;
55 if (setitimer(ITIMER_REAL, &it, &oit) < 0)
56 return (-1); /* XXX will be converted to unsigned */
58 /* Backwards compatibility with alarm(3) */
59 if (oit.it_value.tv_usec)
60 oit.it_value.tv_sec++;
61 return (oit.it_value.tv_sec);
66 unsigned int secs, usecs;
68 if (secs == 0 && usecs == 0)
71 if (secs == 0 || usecs >= 500000)
76 return (alarm (secs));
78 #endif /* !HAVE_SETITIMER */
80 /* A version of sleep using fractional seconds and select. I'd like to use
81 `usleep', but it's already taken */
83 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
86 unsigned int sec, usec;
93 return select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
95 #else /* !HAVE_TIMEVAL || !HAVE_SELECT */
100 if (usec >= 500000) /* round */
104 #endif /* !HAVE_TIMEVAL || !HAVE_SELECT */