From bc329417f840d8073d4e5af7e2c8b23bb32e149c Mon Sep 17 00:00:00 2001
From: Rob Landley <rob@landley.net>
Date: Thu, 15 Mar 2012 20:49:11 -0500
Subject: Simplify nanosleep call.

---
 toys/sleep.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/toys/sleep.c b/toys/sleep.c
index ed8523f3..311a2683 100644
--- a/toys/sleep.c
+++ b/toys/sleep.c
@@ -35,7 +35,7 @@ void sleep_main(void)
 	else {
 		char *arg;
 		double d = strtod(*toys.optargs, &arg);
-		unsigned long l;
+		struct timespec tv;
 
 		// Parse suffix
 		if (*arg) {
@@ -45,17 +45,7 @@ void sleep_main(void)
 			d *= imhd[c-mhd];
 		}
 
-		// wait through the delay
-		l = (unsigned long)d;
-		d -= l;
-		if (l) toys.exitval = sleep(l);
-		if (!toys.exitval) {
-			unsigned long usec = d * 1000000;
-			struct timespec tv = {
-				.tv_sec = usec / 1000000,
-				.tv_nsec = (usec % 1000000) * 1000
-			};
-			toys.exitval = nanosleep(&tv, NULL);
-		}
+		tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
+		toys.exitval = !!nanosleep(&tv, NULL);
 	}
 }
-- 
cgit v1.2.3