diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2017-11-27 23:39:04 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2017-11-27 23:39:04 +0000 |
commit | b35e69d2a193c9edd9a3786ff5d45f4bd1947f8d (patch) | |
tree | fdcd287b8885e104ae0e5953be748a77a54839b8 /usr.bin/less | |
parent | 0198c84929b4b0abce53a4a09ec6a6440f59941b (diff) |
Use CLOCK_MONOTONIC for the delay before printing "Calculating line numbers"
from Scott Cheloha who's pushing this upstream. ok tb@
Diffstat (limited to 'usr.bin/less')
-rw-r--r-- | usr.bin/less/linenum.c | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/usr.bin/less/linenum.c b/usr.bin/less/linenum.c index 48672e9f2ac..14a569cef1f 100644 --- a/usr.bin/less/linenum.c +++ b/usr.bin/less/linenum.c @@ -33,6 +33,10 @@ * called to make sure we cache line numbers often enough. */ +#include <sys/time.h> + +#include <time.h> + #include "less.h" /* @@ -197,14 +201,30 @@ add_lnum(off_t linenum, off_t pos) } static int loopcount; -static time_t startime; +static struct timespec timeout; + +static void +timeout_set(int seconds) +{ + clock_gettime(CLOCK_MONOTONIC, &timeout); + timeout.tv_sec += seconds; +} + +static int +timeout_elapsed(void) +{ + struct timespec now; + + clock_gettime(CLOCK_MONOTONIC, &now); + return timespeccmp(&now, &timeout, >=); +} static void longish(void) { if (loopcount >= 0 && ++loopcount > 100) { loopcount = 0; - if (time(NULL) >= startime + LONGTIME) { + if (timeout_elapsed()) { ierror("Calculating line numbers", NULL); loopcount = -1; } @@ -274,7 +294,7 @@ find_linenum(off_t pos) * The decision is based on which way involves * traversing fewer bytes in the file. */ - startime = time(NULL); + timeout_set(LONGTIME); if (p == &anchor || pos - p->prev->pos < p->pos - pos) { /* * Go forward. |