From 05eb82fc959328f851d4b939d394529ac377de19 Mon Sep 17 00:00:00 2001
From: TyK <tisyang@gmail.com>
Date: Mon, 27 Nov 2023 10:59:21 +0800
Subject: [PATCH] timestamp formatting: always use 64-bit for timestamp
 formatting.

Using C99 `unsigned long long` to format `struct timeval`'s `tv_sec`
and `tv_usec`, fix incorrect print on some 32bit platform which
are using time64.

Upstream-Status: Backport [https://github.com/linux-can/can-utils/commit/ceda93bd5c56927c72d48dcaa30e17d6ecea86b8]
---
 asc2log.c      | 38 +++++++++++++++++++++++---------------
 candump.c      |  6 +++---
 canlogserver.c |  4 ++--
 canplayer.c    | 11 +++++++----
 isotpdump.c    |  8 ++++----
 isotpperf.c    |  4 ++--
 isotpsniffer.c |  6 +++---
 j1939cat.c     |  4 ++--
 j1939spy.c     |  6 +++---
 log2asc.c      | 11 +++++++----
 slcanpty.c     |  4 ++--
 11 files changed, 58 insertions(+), 44 deletions(-)

diff --git a/asc2log.c b/asc2log.c
index ea6b486..4eb3609 100644
--- a/asc2log.c
+++ b/asc2log.c
@@ -73,7 +73,7 @@ void print_usage(char *prg)
 
 void prframe(FILE *file, struct timeval *tv, int dev, struct canfd_frame *cf, unsigned int max_dlen, char *extra_info) {
 
-	fprintf(file, "(%lu.%06lu) ", tv->tv_sec, tv->tv_usec);
+	fprintf(file, "(%llu.%06llu) ", (unsigned long long)tv->tv_sec, (unsigned long long)tv->tv_usec);
 
 	if (dev > 0)
 		fprintf(file, "can%d ", dev-1);
@@ -141,11 +141,14 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i
 	char dir[3]; /* 'Rx' or 'Tx' plus terminating zero */
 	char *extra_info;
 	int i, items;
+	unsigned long long sec, usec;
 
 	/* check for ErrorFrames */
-	if (sscanf(buf, "%lu.%lu %d %s",
-		   &read_tv.tv_sec, &read_tv.tv_usec,
+	if (sscanf(buf, "%llu.%llu %d %s",
+		   &sec, &usec,
 		   &interface, tmp1) == 4) {
+		read_tv.tv_sec = sec;
+		read_tv.tv_usec = usec;
 
 		if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
 
@@ -165,18 +168,20 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i
 
 	/* check for CAN frames with (hexa)decimal values */
 	if (base == 'h')
-		items = sscanf(buf, "%lu.%lu %d %s %2s %c %x %x %x %x %x %x %x %x %x",
-			       &read_tv.tv_sec, &read_tv.tv_usec, &interface,
+		items = sscanf(buf, "%llu.%llu %d %s %2s %c %x %x %x %x %x %x %x %x %x",
+			       &sec, &usec, &interface,
 			       tmp1, dir, &rtr, &dlc,
 			       &data[0], &data[1], &data[2], &data[3],
 			       &data[4], &data[5], &data[6], &data[7]);
 	else
-		items = sscanf(buf, "%lu.%lu %d %s %2s %c %x %d %d %d %d %d %d %d %d",
-			       &read_tv.tv_sec, &read_tv.tv_usec, &interface,
+		items = sscanf(buf, "%llu.%llu %d %s %2s %c %x %d %d %d %d %d %d %d %d",
+			       &sec, &usec, &interface,
 			       tmp1, dir, &rtr, &dlc,
 			       &data[0], &data[1], &data[2], &data[3],
 			       &data[4], &data[5], &data[6], &data[7]);
 
+	read_tv.tv_sec = sec;
+	read_tv.tv_usec = usec;
 	if (items < 7 ) /* make sure we've read the dlc */
 		return;
 
@@ -246,6 +251,7 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, int dplace
 	char *extra_info;
 	char *ptr;
 	int i;
+	unsigned long long sec, usec;
 
 	/* The CANFD format is mainly in hex representation but <DataLength>
 	   and probably some content we skip anyway. Don't trust the docs! */
@@ -255,19 +261,21 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, int dplace
 	   100000  214   223040 80000000 46500250 460a0250 20011736 20010205 */
 
 	/* check for valid line without symbolic name */
-	if (sscanf(buf, "%lu.%lu %*s %d %2s %s %hhx %hhx %x %d ",
-		   &read_tv.tv_sec, &read_tv.tv_usec, &interface,
+	if (sscanf(buf, "%llu.%llu %*s %d %2s %s %hhx %hhx %x %d ",
+		   &sec, &usec, &interface,
 		   dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) {
 
 		/* check for valid line with a symbolic name */
-		if (sscanf(buf, "%lu.%lu %*s %d %2s %s %*s %hhx %hhx %x %d ",
-			   &read_tv.tv_sec, &read_tv.tv_usec, &interface,
+		if (sscanf(buf, "%llu.%llu %*s %d %2s %s %*s %hhx %hhx %x %d ",
+			   &sec, &usec, &interface,
 			   dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) {
 
 			/* no valid CANFD format pattern */
 			return;
 		}
 	}
+	read_tv.tv_sec = sec;
+	read_tv.tv_usec = usec;
 
 	/* check for allowed (unsigned) value ranges */
 	if ((dlen > CANFD_MAX_DLEN) || (dlc > CANFD_MAX_DLC) ||
@@ -427,12 +435,12 @@ int main(int argc, char **argv)
 	FILE *infile = stdin;
 	FILE *outfile = stdout;
 	static int verbose;
-	static struct timeval tmp_tv; /* tmp frame timestamp from ASC file */
 	static struct timeval date_tv; /* date of the ASC file */
 	static int dplace; /* decimal place 4, 5 or 6 or uninitialized */
 	static char base; /* 'd'ec or 'h'ex */
 	static char timestamps; /* 'a'bsolute or 'r'elative */
 	int opt;
+	unsigned long long sec, usec;
 
 	while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
 		switch (opt) {
@@ -505,12 +513,12 @@ int main(int argc, char **argv)
 					gettimeofday(&date_tv, NULL);
 				}
 				if (verbose)
-					printf("date %lu => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
+					printf("date %llu => %s", (unsigned long long)date_tv.tv_sec, ctime(&date_tv.tv_sec));
 				continue;
 			}
 
 			/* check for decimal places length in valid CAN frames */
-			if (sscanf(buf, "%lu.%s %s ", &tmp_tv.tv_sec, tmp2,
+			if (sscanf(buf, "%llu.%s %s ", &sec, tmp2,
 				   tmp1) != 3)
 				continue; /* dplace remains zero until first found CAN frame */
 
@@ -529,7 +537,7 @@ int main(int argc, char **argv)
 		/* so try to get CAN frames and ErrorFrames and convert them */
 
 		/* check classic CAN format or the CANFD tag which can take both types */
-		if (sscanf(buf, "%lu.%lu %s ", &tmp_tv.tv_sec,  &tmp_tv.tv_usec, tmp1) == 3){
+		if (sscanf(buf, "%llu.%llu %s ", &sec,  &usec, tmp1) == 3){
 			if (!strncmp(tmp1, "CANFD", 5))
 				eval_canfd(buf, &date_tv, timestamps, dplace, outfile);
 			else
diff --git a/candump.c b/candump.c
index 82f75b1..4ae8864 100644
--- a/candump.c
+++ b/candump.c
@@ -224,7 +224,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
 {
 	switch (timestamp) {
 	case 'a': /* absolute with timestamp */
-		sprintf(ts_buffer, "(%010lu.%06lu) ", tv->tv_sec, tv->tv_usec);
+		sprintf(ts_buffer, "(%010llu.%06llu) ", (unsigned long long)tv->tv_sec, (unsigned long long)tv->tv_usec);
 		break;
 
 	case 'A': /* absolute with date */
@@ -234,7 +234,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
 
 		tm = *localtime(&tv->tv_sec);
 		strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
-		sprintf(ts_buffer, "(%s.%06lu) ", timestring, tv->tv_usec);
+		sprintf(ts_buffer, "(%s.%06llu) ", timestring, (unsigned long long)tv->tv_usec);
 	}
 	break;
 
@@ -251,7 +251,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
 			diff.tv_sec--, diff.tv_usec += 1000000;
 		if (diff.tv_sec < 0)
 			diff.tv_sec = diff.tv_usec = 0;
-		sprintf(ts_buffer, "(%03lu.%06lu) ", diff.tv_sec, diff.tv_usec);
+		sprintf(ts_buffer, "(%03llu.%06llu) ", (unsigned long long)diff.tv_sec, (unsigned long long)diff.tv_usec);
 
 		if (timestamp == 'd')
 			*last_tv = *tv; /* update for delta calculation */
diff --git a/canlogserver.c b/canlogserver.c
index 51d548f..349d64e 100644
--- a/canlogserver.c
+++ b/canlogserver.c
@@ -408,8 +408,8 @@ int main(int argc, char **argv)
 
 				idx = idx2dindex(addr.can_ifindex, s[i]);
 
-				sprintf(temp, "(%lu.%06lu) %*s ",
-					tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]);
+				sprintf(temp, "(%llu.%06llu) %*s ",
+					(unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec, max_devname_len, devname[idx]);
 				sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen); 
 				strcat(temp, "\n");
 
diff --git a/canplayer.c b/canplayer.c
index 51adc77..96346ce 100644
--- a/canplayer.c
+++ b/canplayer.c
@@ -259,6 +259,7 @@ int main(int argc, char **argv)
 	int txidx;       /* sendto() interface index */
 	int eof, txmtu, i, j;
 	char *fret;
+	unsigned long long sec, usec;
 
 	while ((opt = getopt(argc, argv, "I:l:tin:g:s:xv?")) != -1) {
 		switch (opt) {
@@ -419,11 +420,12 @@ int main(int argc, char **argv)
 
 		eof = 0;
 
-		if (sscanf(buf, "(%lu.%lu) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
-			   device, ascframe) != 4) {
+		if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, ascframe) != 4) {
 			fprintf(stderr, "incorrect line format in logfile\n");
 			return 1;
 		}
+		log_tv.tv_sec = sec;
+		log_tv.tv_usec = usec;
 
 		if (use_timestamps) { /* throttle sending due to logfile timestamps */
 
@@ -505,11 +507,12 @@ int main(int argc, char **argv)
 					break;
 				}
 
-				if (sscanf(buf, "(%lu.%lu) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
-					   device, ascframe) != 4) {
+				if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, ascframe) != 4) {
 					fprintf(stderr, "incorrect line format in logfile\n");
 					return 1;
 				}
+				log_tv.tv_sec = sec;
+				log_tv.tv_usec = usec;
 
 				/*
 				 * ensure the fractions of seconds are 6 decimal places long to catch
diff --git a/isotpdump.c b/isotpdump.c
index d22725e..e9e96ce 100644
--- a/isotpdump.c
+++ b/isotpdump.c
@@ -361,7 +361,7 @@ int main(int argc, char **argv)
 
 			switch (timestamp) {
 			case 'a': /* absolute with timestamp */
-				printf("(%lu.%06lu) ", tv.tv_sec, tv.tv_usec);
+				printf("(%llu.%06llu) ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec);
 				break;
 
 			case 'A': /* absolute with date */
@@ -372,7 +372,7 @@ int main(int argc, char **argv)
 				tm = *localtime(&tv.tv_sec);
 				strftime(timestring, 24, "%Y-%m-%d %H:%M:%S",
 					 &tm);
-				printf("(%s.%06lu) ", timestring, tv.tv_usec);
+				printf("(%s.%06llu) ", timestring, (unsigned long long)tv.tv_usec);
 			} break;
 
 			case 'd': /* delta */
@@ -388,8 +388,8 @@ int main(int argc, char **argv)
 					diff.tv_sec--, diff.tv_usec += 1000000;
 				if (diff.tv_sec < 0)
 					diff.tv_sec = diff.tv_usec = 0;
-				printf("(%lu.%06lu) ", diff.tv_sec,
-				       diff.tv_usec);
+				printf("(%llu.%06llu) ", (unsigned long long)diff.tv_sec,
+				       (unsigned long long)diff.tv_usec);
 
 				if (timestamp == 'd')
 					last_tv =
diff --git a/isotpperf.c b/isotpperf.c
index 154d5cd..ad0dc2a 100644
--- a/isotpperf.c
+++ b/isotpperf.c
@@ -403,9 +403,9 @@ int main(int argc, char **argv)
 
 				/* check devisor to be not zero */
 				if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){
-					printf("%lu.%06lus ", diff_tv.tv_sec, diff_tv.tv_usec);
+					printf("%llu.%06llus ", (unsigned long long)diff_tv.tv_sec, (unsigned long long)diff_tv.tv_usec);
 					printf("=> %lu byte/s", (fflen * 1000) /
-					       (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
+					       (unsigned long)(diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
 				} else
 					printf("(no time available)     ");
 
diff --git a/isotpsniffer.c b/isotpsniffer.c
index 2b6de40..f976149 100644
--- a/isotpsniffer.c
+++ b/isotpsniffer.c
@@ -101,7 +101,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
 		switch (timestamp) {
 
 		case 'a': /* absolute with timestamp */
-			printf("(%lu.%06lu) ", tv->tv_sec, tv->tv_usec);
+			printf("(%llu.%06llu) ", (unsigned long long)tv->tv_sec, (unsigned long long)tv->tv_usec);
 			break;
 
 		case 'A': /* absolute with date */
@@ -111,7 +111,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
 
 			tm = *localtime(&tv->tv_sec);
 			strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
-			printf("(%s.%06lu) ", timestring, tv->tv_usec);
+			printf("(%s.%06llu) ", timestring, (unsigned long long)tv->tv_usec);
 		}
 		break;
 
@@ -128,7 +128,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
 				diff.tv_sec--, diff.tv_usec += 1000000;
 			if (diff.tv_sec < 0)
 				diff.tv_sec = diff.tv_usec = 0;
-			printf("(%lu.%06lu) ", diff.tv_sec, diff.tv_usec);
+			printf("(%llu.%06llu) ", (unsigned long long)diff.tv_sec, (unsigned long long)diff.tv_usec);
 
 			if (timestamp == 'd')
 				*last_tv = *tv; /* update for delta calculation */
diff --git a/j1939cat.c b/j1939cat.c
index 4234aad..238c4ff 100644
--- a/j1939cat.c
+++ b/j1939cat.c
@@ -148,8 +148,8 @@ static void j1939cat_print_timestamp(struct j1939cat_priv *priv, const char *nam
 	if (!(cur->tv_sec | cur->tv_nsec))
 		return;
 
-	fprintf(stderr, "  %s: %lu s %lu us (seq=%03u, send=%07u)",
-			name, cur->tv_sec, cur->tv_nsec / 1000,
+	fprintf(stderr, "  %s: %llu s %llu us (seq=%03u, send=%07u)",
+			name, (unsigned long long)cur->tv_sec, (unsigned long long)cur->tv_nsec / 1000,
 			stats->tskey, stats->send);
 
 	fprintf(stderr, "\n");
diff --git a/j1939spy.c b/j1939spy.c
index e49ed14..56950ea 100644
--- a/j1939spy.c
+++ b/j1939spy.c
@@ -268,14 +268,14 @@ int main(int argc, char **argv)
 				goto abs_time;
 			} else if ('a' == s.time) {
 				abs_time:
-				printf("(%lu.%04lu)", tdut.tv_sec, tdut.tv_usec / 100);
+				printf("(%llu.%04llu)", (unsigned long long)tdut.tv_sec, (unsigned long long)tdut.tv_usec / 100);
 			} else if ('A' == s.time) {
 				struct tm tm;
 				tm = *localtime(&tdut.tv_sec);
-				printf("(%04u%02u%02uT%02u%02u%02u.%04lu)",
+				printf("(%04u%02u%02uT%02u%02u%02u.%04llu)",
 					tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
 					tm.tm_hour, tm.tm_min, tm.tm_sec,
-					tdut.tv_usec/100);
+					(unsigned long long)tdut.tv_usec/100);
 			}
 		}
 		printf(" %s ", libj1939_addr2str(&src));
diff --git a/log2asc.c b/log2asc.c
index a1cf364..85634f8 100644
--- a/log2asc.c
+++ b/log2asc.c
@@ -190,6 +190,7 @@ int main(int argc, char **argv)
 	FILE *infile = stdin;
 	FILE *outfile = stdout;
 	static int maxdev, devno, i, crlf, fdfmt, nortrdlc, d4, opt, mtu;
+	unsigned long long sec, usec;
 
 	while ((opt = getopt(argc, argv, "I:O:4nfr?")) != -1) {
 		switch (opt) {
@@ -259,18 +260,20 @@ int main(int argc, char **argv)
 		if (buf[0] != '(')
 			continue;
 
-		if (sscanf(buf, "(%lu.%lu) %s %s %s", &tv.tv_sec, &tv.tv_usec,
+		if (sscanf(buf, "(%llu.%llu) %s %s %s", &sec, &usec,
 			   device, ascframe, extra_info) != 5) {
 
 			/* do not evaluate the extra info */
 			extra_info[0] = 0;
 
-			if (sscanf(buf, "(%lu.%lu) %s %s", &tv.tv_sec, &tv.tv_usec,
+			if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec,
 				   device, ascframe) != 4) {
 				fprintf(stderr, "incorrect line format in logfile\n");
 				return 1;
 			}
 		}
+		tv.tv_sec = sec;
+		tv.tv_usec = usec;
 
 		if (!start_tv.tv_sec) { /* print banner */
 			start_tv = tv;
@@ -305,9 +308,9 @@ int main(int argc, char **argv)
 				tv.tv_sec = tv.tv_usec = 0;
 
 			if (d4)
-				fprintf(outfile, "%4lu.%04lu ", tv.tv_sec, tv.tv_usec/100);
+				fprintf(outfile, "%4llu.%04llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec/100);
 			else
-				fprintf(outfile, "%4lu.%06lu ", tv.tv_sec, tv.tv_usec);
+				fprintf(outfile, "%4llu.%06llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec);
 
 			if ((mtu == CAN_MTU) && (fdfmt == 0))
 				can_asc(&cf, devno, nortrdlc, extra_info, outfile);
diff --git a/slcanpty.c b/slcanpty.c
index fa97cd6..fc86d4f 100644
--- a/slcanpty.c
+++ b/slcanpty.c
@@ -363,8 +363,8 @@ int can2pty(int pty, int socket, int *tstamp)
 		if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
 			perror("SIOCGSTAMP");
 
-		sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
-			(tv.tv_sec%60)*1000 + tv.tv_usec/1000);
+		sprintf(&buf[ptr + 2*frame.can_dlc], "%04llX",
+			(unsigned long long)(tv.tv_sec%60)*1000 + tv.tv_usec/1000);
 	}
 
 	strcat(buf, "\r"); /* add terminating character */
-- 
2.43.0

