wg: warn about clock going backward

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2016-11-29 13:38:59 +01:00
parent 16a6972bb6
commit 9707e5d6af
1 changed files with 20 additions and 16 deletions

View File

@ -144,15 +144,15 @@ static size_t pretty_time(char *buf, const size_t len, unsigned long long left)
seconds = left % 60; seconds = left % 60;
if (years) if (years)
offset += snprintf(buf + offset, len - offset, "%s%llu " TERMINAL_FG_CYAN "year%s" TERMINAL_RESET, offset ? ", " : "", years, years == 1 ? "" : "s"); offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "year%s" TERMINAL_RESET, offset ? ", " : "", years, years == 1 ? "" : "s");
if (days) if (days)
offset += snprintf(buf + offset, len - offset, "%s%llu " TERMINAL_FG_CYAN "day%s" TERMINAL_RESET, offset ? ", " : "", days, days == 1 ? "" : "s"); offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "day%s" TERMINAL_RESET, offset ? ", " : "", days, days == 1 ? "" : "s");
if (hours) if (hours)
offset += snprintf(buf + offset, len - offset, "%s%llu " TERMINAL_FG_CYAN "hour%s" TERMINAL_RESET, offset ? ", " : "", hours, hours == 1 ? "" : "s"); offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "hour%s" TERMINAL_RESET, offset ? ", " : "", hours, hours == 1 ? "" : "s");
if (minutes) if (minutes)
offset += snprintf(buf + offset, len - offset, "%s%llu " TERMINAL_FG_CYAN "minute%s" TERMINAL_RESET, offset ? ", " : "", minutes, minutes == 1 ? "" : "s"); offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "minute%s" TERMINAL_RESET, offset ? ", " : "", minutes, minutes == 1 ? "" : "s");
if (seconds) if (seconds)
offset += snprintf(buf + offset, len - offset, "%s%llu " TERMINAL_FG_CYAN "second%s" TERMINAL_RESET, offset ? ", " : "", seconds, seconds == 1 ? "" : "s"); offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "second%s" TERMINAL_RESET, offset ? ", " : "", seconds, seconds == 1 ? "" : "s");
return offset; return offset;
} }
@ -161,12 +161,16 @@ static char *ago(const struct timeval *t)
{ {
static char buf[1024]; static char buf[1024];
size_t offset; size_t offset;
time_t now = time(NULL);
offset = pretty_time(buf, sizeof(buf), time(NULL) - t->tv_sec); if (now == t->tv_sec)
if (offset) strncpy(buf, "Now", sizeof(buf) - 1);
snprintf(buf + offset, sizeof(buf) - offset, " ago"); else if (now < t->tv_sec)
else strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward; connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 1);
snprintf(buf, sizeof(buf), "Now"); else {
offset = pretty_time(buf, sizeof(buf), now - t->tv_sec);
strncpy(buf + offset, " ago", sizeof(buf) - offset - 1);
}
return buf; return buf;
} }
@ -174,7 +178,7 @@ static char *ago(const struct timeval *t)
static char *every(uint16_t seconds) static char *every(uint16_t seconds)
{ {
static char buf[1024] = "every "; static char buf[1024] = "every ";
pretty_time(buf + strlen("every "), sizeof(buf) - strlen("every "), seconds); pretty_time(buf + strlen("every "), sizeof(buf) - strlen("every ") - 1, seconds);
return buf; return buf;
} }
@ -183,15 +187,15 @@ static char *bytes(uint64_t b)
static char buf[1024]; static char buf[1024];
if (b < 1024ULL) if (b < 1024ULL)
snprintf(buf, sizeof(buf), "%u " TERMINAL_FG_CYAN "B" TERMINAL_RESET, (unsigned)b); snprintf(buf, sizeof(buf) - 1, "%u " TERMINAL_FG_CYAN "B" TERMINAL_RESET, (unsigned)b);
else if (b < 1024ULL * 1024ULL) else if (b < 1024ULL * 1024ULL)
snprintf(buf, sizeof(buf), "%.2f " TERMINAL_FG_CYAN "KiB" TERMINAL_RESET, (double)b / 1024); snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "KiB" TERMINAL_RESET, (double)b / 1024);
else if (b < 1024ULL * 1024ULL * 1024ULL) else if (b < 1024ULL * 1024ULL * 1024ULL)
snprintf(buf, sizeof(buf), "%.2f " TERMINAL_FG_CYAN "MiB" TERMINAL_RESET, (double)b / (1024 * 1024)); snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "MiB" TERMINAL_RESET, (double)b / (1024 * 1024));
else if (b < 1024ULL * 1024ULL * 1024ULL * 1024ULL) else if (b < 1024ULL * 1024ULL * 1024ULL * 1024ULL)
snprintf(buf, sizeof(buf), "%.2f " TERMINAL_FG_CYAN "GiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024)); snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "GiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024));
else else
snprintf(buf, sizeof(buf), "%.2f " TERMINAL_FG_CYAN "TiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024) / 1024); snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "TiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024) / 1024);
return buf; return buf;
} }