mirror of
https://github.com/nmap/nmap.git
synced 2025-12-24 16:39:03 +00:00
Use time_t instead of long and double for storing uptime
Fixes #275. This results in fewer casts and less subtraction than the previous method, and should still be portable. Only division and subtraction and difftime are performed on the value, so it will not overflow. And the TCP timestamp itself is a 32-bit value, so it can't refer to a time farther in the past than the 32-bit epoch. One explicit cast (to long long) is used in order to ensure the format string can handle any conceivable value according to the compiler and avoid a warning message.
This commit is contained in:
10
output.cc
10
output.cc
@@ -1989,17 +1989,17 @@ void printosscanoutput(Target *currenths) {
|
||||
if (currenths->seq.lastboot) {
|
||||
char tmbuf[128];
|
||||
struct timeval tv;
|
||||
time_t lastboot;
|
||||
lastboot = (time_t) currenths->seq.lastboot;
|
||||
strncpy(tmbuf, ctime(&lastboot), sizeof(tmbuf));
|
||||
double uptime;
|
||||
strncpy(tmbuf, ctime(¤ths->seq.lastboot), sizeof(tmbuf));
|
||||
chomp(tmbuf);
|
||||
gettimeofday(&tv, NULL);
|
||||
uptime = difftime(tv.tv_sec, currenths->seq.lastboot);
|
||||
if (o.verbose)
|
||||
log_write(LOG_PLAIN, "Uptime guess: %.3f days (since %s)\n",
|
||||
(double) (tv.tv_sec - currenths->seq.lastboot) / 86400,
|
||||
uptime / 86400,
|
||||
tmbuf);
|
||||
xml_open_start_tag("uptime");
|
||||
xml_attribute("seconds", "%li", tv.tv_sec - currenths->seq.lastboot);
|
||||
xml_attribute("seconds", "%.0f", uptime);
|
||||
xml_attribute("lastboot", "%s", tmbuf);
|
||||
xml_close_empty_tag();
|
||||
xml_newline();
|
||||
|
||||
Reference in New Issue
Block a user