aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLisa Nguyen <lisa.nguyen@linaro.org>2016-06-21 11:13:12 -0700
committerLisa Nguyen <lisa.nguyen@linaro.org>2016-09-12 16:40:13 -0700
commitaec8851f5f6583666396fa54dd598d91ef5b5ee3 (patch)
tree3dcaa56f407937f3197f9718700704c106b30aaa
parent84479a38849f2b0abee9a2c2841afa8da15b08eb (diff)
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>
-rw-r--r--utils/nanosleep.c10
1 files changed, 9 insertions, 1 deletions
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 @@ int main(int argc, char *argv[])
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))