+#include <unistd.h>
+
+#undef nanosleep
+
+enum { BILLION = 1000 * 1000 * 1000 };
+
+#if HAVE_BUG_BIG_NANOSLEEP
+
+void
+getnow (struct timespec *t)
+{
+# if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
+ if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
+ return;
+# endif
+ gettime (t);
+}
+
+int
+rpl_nanosleep (const struct timespec *requested_delay,
+ struct timespec *remaining_delay)
+{
+ /* nanosleep mishandles large sleeps due to internal overflow
+ problems, so check that the proper amount of time has actually
+ elapsed. */
+
+ struct timespec delay = *requested_delay;
+ struct timespec t0;
+ getnow (&t0);