summaryrefslogtreecommitdiff
path: root/src/sleep.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2000-01-06 08:28:39 +0000
committerJim Meyering <jim@meyering.net>2000-01-06 08:28:39 +0000
commit757861331e7e28a5cf1e4e439070a952ccba1177 (patch)
tree74df6b74839c01de9e388a3e6231149a8cdcb801 /src/sleep.c
parente1cc2ace3cd1c88baafc891df6b3edc1e9ad6bc0 (diff)
downloadcoreutils-757861331e7e28a5cf1e4e439070a952ccba1177.tar.xz
Minor code cleanup.
(clock_get_realtime): Return argument, for convenience. (main): Use this to simplify main sleep loop.
Diffstat (limited to 'src/sleep.c')
-rw-r--r--src/sleep.c26
1 files changed, 11 insertions, 15 deletions
diff --git a/src/sleep.c b/src/sleep.c
index 305066d2d..21593c4e1 100644
--- a/src/sleep.c
+++ b/src/sleep.c
@@ -40,6 +40,8 @@
#if HAVE_FENV_H
# include <fenv.h>
#endif
+
+/* Tell the compiler that non-default rounding modes are used. */
#if 199901 <= __STDC_VERSION__
#pragma STDC FENV_ACCESS ON
#endif
@@ -149,7 +151,7 @@ timespec_subtract (struct timespec *diff,
return y->tv_sec < x->tv_sec;
}
-static void
+static struct timespec *
clock_get_realtime (struct timespec *ts)
{
int fail;
@@ -167,6 +169,8 @@ clock_get_realtime (struct timespec *ts)
if (fail)
error (1, errno, _("cannot read realtime clock"));
+
+ return ts;
}
int
@@ -255,7 +259,7 @@ main (int argc, char **argv)
{
time_t t = ts_sleep.tv_sec + 1;
- /* Detect floating point overflow (NaN) in interval length. */
+ /* Detect integer overflow. */
forever |= (t < ts_sleep.tv_sec);
ts_sleep.tv_sec = t;
@@ -277,23 +281,15 @@ main (int argc, char **argv)
if (forever)
{
+ /* Fix ts_sleep and ts_stop, which may be garbage due to overflow. */
ts_sleep.tv_sec = ts_stop.tv_sec = TIME_T_MAX;
ts_sleep.tv_nsec = ts_stop.tv_nsec = 999999999;
}
- while (1)
- {
- struct timespec remaining;
- struct timespec ts_now;
- int any_remaining;
- int suspended = nanosleep (&ts_sleep, &remaining);
- if (!suspended)
- break;
- clock_get_realtime (&ts_now);
- any_remaining = timespec_subtract (&ts_sleep, &ts_stop, &ts_now);
- if (! any_remaining)
- break;
- }
+ while (nanosleep (&ts_sleep, NULL) != 0
+ && timespec_subtract (&ts_sleep, &ts_stop,
+ clock_get_realtime (&ts_start)))
+ continue;
exit (0);
}