--- ./lib/xnanosleep.c.xnanosleep	2003-03-08 14:25:58.000000000 +0000
+++ ./lib/xnanosleep.c	2004-11-12 22:53:58.000000000 +0000
@@ -28,16 +28,6 @@
 #include <sys/types.h>
 #include <time.h>
 
-#if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
-# define USE_CLOCK_GETTIME 1
-#else
-# define USE_CLOCK_GETTIME 0
-#endif
-
-#if ! USE_CLOCK_GETTIME
-# include <sys/time.h>
-#endif
-
 #ifndef CHAR_BIT
 # define CHAR_BIT 8
 #endif
@@ -57,7 +47,6 @@
 #include "timespec.h"
 #include "xalloc.h"
 #include "xnanosleep.h"
-#include "xstrtod.h"
 
 /* Subtract the `struct timespec' values X and Y,
    storing the difference in DIFF.
@@ -68,50 +57,29 @@
 timespec_subtract (struct timespec *diff,
 		   const struct timespec *x, struct timespec *y)
 {
-  /* Perform the carry for the later subtraction by updating Y. */
-  if (x->tv_nsec < y->tv_nsec)
-    {
-      int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
-      y->tv_nsec -= 1000000000 * nsec;
-      y->tv_sec += nsec;
-    }
+  time_t sec = x->tv_sec - y->tv_sec;
+  long int nsec = x->tv_nsec - y->tv_nsec;
+
+  if (x->tv_sec < y->tv_sec)
+    return 0;
 
-  if (1000000000 < x->tv_nsec - y->tv_nsec)
+  if (sec < 0)
     {
-      int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000;
-      y->tv_nsec += 1000000000 * nsec;
-      y->tv_sec -= nsec;
+      sec = TIME_T_MAX;
+      nsec = 999999999;
     }
+  else if (sec == 0 && nsec <= 0)
+    return 0;
 
-  /* Compute the time remaining to wait.
-     `tv_nsec' is certainly positive. */
-  diff->tv_sec = x->tv_sec - y->tv_sec;
-  diff->tv_nsec = x->tv_nsec - y->tv_nsec;
-
-  /* Return 1 if result is positive. */
-  return y->tv_sec < x->tv_sec;
-}
-
-struct timespec *
-clock_get_realtime (struct timespec *ts)
-{
-  int fail;
-#if USE_CLOCK_GETTIME
-  fail = clock_gettime (CLOCK_REALTIME, ts);
-#else
-  struct timeval tv;
-  fail = gettimeofday (&tv, NULL);
-  if (!fail)
+  if (nsec < 0)
     {
-      ts->tv_sec = tv.tv_sec;
-      ts->tv_nsec = 1000 * tv.tv_usec;
+      sec--;
+      nsec += 1000000000;
     }
-#endif
-
-  if (fail)
-    return NULL;
 
-  return ts;
+  diff->tv_sec = sec;
+  diff->tv_nsec = nsec;
+  return 1;
 }
 
 /* Sleep until the time (call it WAKE_UP_TIME) specified as
@@ -132,7 +100,7 @@
 
   assert (0 <= seconds);
 
-  if (clock_get_realtime (&ts_start) == NULL)
+  if (gettime (&ts_start) != 0)
     return -1;
 
   /* Separate whole seconds from nanoseconds.
@@ -184,7 +152,7 @@
 
   while (nanosleep (&ts_sleep, NULL) != 0)
     {
-      if (errno != EINTR)
+      if (errno != EINTR || gettime (&ts_start) != 0)
 	return -1;
 
       /* POSIX.1-2001 requires that when a process is suspended, then
@@ -196,7 +164,7 @@
 	 relying on nanosleep's computation.  */
 
       if (! timespec_subtract (&ts_sleep, &ts_stop,
-			       clock_get_realtime (&ts_start)))
+			       &ts_start))
 	break;
     }
 
