Math Error in timespecsub()
#define timespecsub(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) { \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (0)
#endif
The line "if ((vsp)->tv_nsec < 0) {" should be "if ((vsp)->tv_nsec < 0 && (vsp)->tv_sec != 0) {"
consider a test case below:
tsp->tv_sec = 0; tsp->tv_nsec = 1; usp->tv_sec = 0; usp->tv_nsec = 2;
I found this trying to calculate how long to nanosleep for.