nanosleep: recalculate the amount of time to sleep

In cases where the total number of nanoseconds equal or
exceed the MAX_VALUE of the nanosleep function, recalculate
and reassign the new values to tv_sec and tv_nsec.

Signed-off-by: Lisa Nguyen <lisa.nguyen@linaro.org>
diff --git a/utils/nanosleep.c b/utils/nanosleep.c
index 27f197e..0088eb4 100644
--- a/utils/nanosleep.c
+++ b/utils/nanosleep.c
@@ -27,6 +27,8 @@
 #include <errno.h>
 #include <time.h>
 
+#define SECOND 1000000000 /* in nanoseconds */
+
 int main(int argc, char *argv[])
 {
 	struct timespec req = { 0 }, rem = { 0 };
@@ -36,7 +38,13 @@
 		return 1;
 	}
 
-	req.tv_nsec = atoi(argv[1]);
+	int total_nsec = atoi(argv[1]);
+
+	/* if total of nanoseconds equals or exceeds one second */
+	if (total_nsec >= SECOND) {
+		req.tv_sec = total_nsec / SECOND;
+		req.tv_nsec = total_nsec % SECOND;
+	}
 
 	for (;;) {
 		if (!nanosleep(&req, &rem))