timestamp formatting: always use 64-bit for timesstamp formatting.

Using C99 `unsigned long long` to format `struct timeval`'s `tv_sec`
and `tv_usec`, fix incorrect print under some 32bit platform which
 using time64.
pull/473/head
TyK 2023-11-27 10:59:21 +08:00
parent 97c8ccdbbf
commit ceda93bd5c
11 changed files with 58 additions and 42 deletions

View File

@ -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) { 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) if (dev > 0)
fprintf(file, "can%d ", dev-1); 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 dir[3]; /* 'Rx' or 'Tx' plus terminating zero */
char *extra_info; char *extra_info;
int i, items; int i, items;
unsigned long long sec, usec;
/* check for ErrorFrames */ /* check for ErrorFrames */
if (sscanf(buf, "%lu.%lu %d %s", if (sscanf(buf, "%llu.%llu %d %s",
&read_tv.tv_sec, &read_tv.tv_usec, &sec, &usec,
&interface, tmp1) == 4) { &interface, tmp1) == 4) {
read_tv.tv_sec = sec;
read_tv.tv_usec = usec;
if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) { 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 */ /* check for CAN frames with (hexa)decimal values */
if (base == 'h') if (base == 'h')
items = sscanf(buf, "%lu.%lu %d %s %2s %c %x %x %x %x %x %x %x %x %x", items = sscanf(buf, "%llu.%llu %d %s %2s %c %x %x %x %x %x %x %x %x %x",
&read_tv.tv_sec, &read_tv.tv_usec, &interface, &sec, &usec, &interface,
tmp1, dir, &rtr, &dlc, tmp1, dir, &rtr, &dlc,
&data[0], &data[1], &data[2], &data[3], &data[0], &data[1], &data[2], &data[3],
&data[4], &data[5], &data[6], &data[7]); &data[4], &data[5], &data[6], &data[7]);
else else
items = sscanf(buf, "%lu.%lu %d %s %2s %c %x %d %d %d %d %d %d %d %d", items = sscanf(buf, "%llu.%llu %d %s %2s %c %x %d %d %d %d %d %d %d %d",
&read_tv.tv_sec, &read_tv.tv_usec, &interface, &sec, &usec, &interface,
tmp1, dir, &rtr, &dlc, tmp1, dir, &rtr, &dlc,
&data[0], &data[1], &data[2], &data[3], &data[0], &data[1], &data[2], &data[3],
&data[4], &data[5], &data[6], &data[7]); &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 */ if (items < 7 ) /* make sure we've read the dlc */
return; return;
@ -246,6 +251,7 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, int dplace
char *extra_info; char *extra_info;
char *ptr; char *ptr;
int i; int i;
unsigned long long sec, usec;
/* The CANFD format is mainly in hex representation but <DataLength> /* The CANFD format is mainly in hex representation but <DataLength>
and probably some content we skip anyway. Don't trust the docs! */ 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 */ 100000 214 223040 80000000 46500250 460a0250 20011736 20010205 */
/* check for valid line without symbolic name */ /* check for valid line without symbolic name */
if (sscanf(buf, "%lu.%lu %*s %d %2s %s %hhx %hhx %x %d ", if (sscanf(buf, "%llu.%llu %*s %d %2s %s %hhx %hhx %x %d ",
&read_tv.tv_sec, &read_tv.tv_usec, &interface, &sec, &usec, &interface,
dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) { dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) {
/* check for valid line with a symbolic name */ /* check for valid line with a symbolic name */
if (sscanf(buf, "%lu.%lu %*s %d %2s %s %*s %hhx %hhx %x %d ", if (sscanf(buf, "%llu.%llu %*s %d %2s %s %*s %hhx %hhx %x %d ",
&read_tv.tv_sec, &read_tv.tv_usec, &interface, &sec, &usec, &interface,
dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) { dir, tmp1, &brs, &esi, &dlc, &dlen) != 9) {
/* no valid CANFD format pattern */ /* no valid CANFD format pattern */
return; return;
} }
} }
read_tv.tv_sec = sec;
read_tv.tv_usec = usec;
/* check for allowed (unsigned) value ranges */ /* check for allowed (unsigned) value ranges */
if ((dlen > CANFD_MAX_DLEN) || (dlc > CANFD_MAX_DLC) || if ((dlen > CANFD_MAX_DLEN) || (dlc > CANFD_MAX_DLC) ||
@ -427,12 +435,12 @@ int main(int argc, char **argv)
FILE *infile = stdin; FILE *infile = stdin;
FILE *outfile = stdout; FILE *outfile = stdout;
static int verbose; 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 struct timeval date_tv; /* date of the ASC file */
static int dplace; /* decimal place 4, 5 or 6 or uninitialized */ static int dplace; /* decimal place 4, 5 or 6 or uninitialized */
static char base; /* 'd'ec or 'h'ex */ static char base; /* 'd'ec or 'h'ex */
static char timestamps; /* 'a'bsolute or 'r'elative */ static char timestamps; /* 'a'bsolute or 'r'elative */
int opt; int opt;
unsigned long long sec, usec;
while ((opt = getopt(argc, argv, "I:O:v?")) != -1) { while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
switch (opt) { switch (opt) {
@ -505,12 +513,12 @@ int main(int argc, char **argv)
gettimeofday(&date_tv, NULL); gettimeofday(&date_tv, NULL);
} }
if (verbose) 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; continue;
} }
/* check for decimal places length in valid CAN frames */ /* 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) tmp1) != 3)
continue; /* dplace remains zero until first found CAN frame */ 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 */ /* so try to get CAN frames and ErrorFrames and convert them */
/* check classic CAN format or the CANFD tag which can take both types */ /* 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)) if (!strncmp(tmp1, "CANFD", 5))
eval_canfd(buf, &date_tv, timestamps, dplace, outfile); eval_canfd(buf, &date_tv, timestamps, dplace, outfile);
else else

View File

@ -226,7 +226,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
{ {
switch (timestamp) { switch (timestamp) {
case 'a': /* absolute with 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; break;
case 'A': /* absolute with date */ case 'A': /* absolute with date */
@ -236,7 +236,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
tm = *localtime(&tv->tv_sec); tm = *localtime(&tv->tv_sec);
strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm); 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; break;
@ -253,7 +253,7 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
diff.tv_sec--, diff.tv_usec += 1000000; diff.tv_sec--, diff.tv_usec += 1000000;
if (diff.tv_sec < 0) if (diff.tv_sec < 0)
diff.tv_sec = diff.tv_usec = 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') if (timestamp == 'd')
*last_tv = *tv; /* update for delta calculation */ *last_tv = *tv; /* update for delta calculation */

View File

@ -415,8 +415,8 @@ int main(int argc, char **argv)
idx = idx2dindex(addr.can_ifindex, s[i]); idx = idx2dindex(addr.can_ifindex, s[i]);
sprintf(temp, "(%lu.%06lu) %*s ", sprintf(temp, "(%llu.%06llu) %*s ",
tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]); (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); sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen);
strcat(temp, "\n"); strcat(temp, "\n");

View File

@ -259,6 +259,7 @@ int main(int argc, char **argv)
int txidx; /* sendto() interface index */ int txidx; /* sendto() interface index */
int eof, txmtu, i, j; int eof, txmtu, i, j;
char *fret; char *fret;
unsigned long long sec, usec;
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xv?")) != -1) { while ((opt = getopt(argc, argv, "I:l:tin:g:s:xv?")) != -1) {
switch (opt) { switch (opt) {
@ -416,10 +417,12 @@ int main(int argc, char **argv)
eof = 0; 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"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }
log_tv.tv_sec = sec;
log_tv.tv_usec = usec;
/* /*
* ensure the fractions of seconds are 6 decimal places long to catch * ensure the fractions of seconds are 6 decimal places long to catch
@ -507,10 +510,12 @@ int main(int argc, char **argv)
break; 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"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }
log_tv.tv_sec = sec;
log_tv.tv_usec = usec;
/* /*
* ensure the fractions of seconds are 6 decimal places long to catch * ensure the fractions of seconds are 6 decimal places long to catch

View File

@ -361,7 +361,7 @@ int main(int argc, char **argv)
switch (timestamp) { switch (timestamp) {
case 'a': /* absolute with 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; break;
case 'A': /* absolute with date */ case 'A': /* absolute with date */
@ -372,7 +372,7 @@ int main(int argc, char **argv)
tm = *localtime(&tv.tv_sec); tm = *localtime(&tv.tv_sec);
strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", strftime(timestring, 24, "%Y-%m-%d %H:%M:%S",
&tm); &tm);
printf("(%s.%06lu) ", timestring, tv.tv_usec); printf("(%s.%06llu) ", timestring, (unsigned long long)tv.tv_usec);
} break; } break;
case 'd': /* delta */ case 'd': /* delta */
@ -388,8 +388,8 @@ int main(int argc, char **argv)
diff.tv_sec--, diff.tv_usec += 1000000; diff.tv_sec--, diff.tv_usec += 1000000;
if (diff.tv_sec < 0) if (diff.tv_sec < 0)
diff.tv_sec = diff.tv_usec = 0; diff.tv_sec = diff.tv_usec = 0;
printf("(%lu.%06lu) ", diff.tv_sec, printf("(%llu.%06llu) ", (unsigned long long)diff.tv_sec,
diff.tv_usec); (unsigned long long)diff.tv_usec);
if (timestamp == 'd') if (timestamp == 'd')
last_tv = last_tv =

View File

@ -403,9 +403,9 @@ int main(int argc, char **argv)
/* check devisor to be not zero */ /* check devisor to be not zero */
if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){ 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) / 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 } else
printf("(no time available) "); printf("(no time available) ");

View File

@ -101,7 +101,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
switch (timestamp) { switch (timestamp) {
case 'a': /* absolute with 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; break;
case 'A': /* absolute with date */ 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); tm = *localtime(&tv->tv_sec);
strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm); 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; break;
@ -128,7 +128,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
diff.tv_sec--, diff.tv_usec += 1000000; diff.tv_sec--, diff.tv_usec += 1000000;
if (diff.tv_sec < 0) if (diff.tv_sec < 0)
diff.tv_sec = diff.tv_usec = 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') if (timestamp == 'd')
*last_tv = *tv; /* update for delta calculation */ *last_tv = *tv; /* update for delta calculation */

View File

@ -148,8 +148,8 @@ static void j1939cat_print_timestamp(struct j1939cat_priv *priv, const char *nam
if (!(cur->tv_sec | cur->tv_nsec)) if (!(cur->tv_sec | cur->tv_nsec))
return; return;
fprintf(stderr, " %s: %lu s %lu us (seq=%03u, send=%07u)", fprintf(stderr, " %s: %llu s %llu us (seq=%03u, send=%07u)",
name, cur->tv_sec, cur->tv_nsec / 1000, name, (unsigned long long)cur->tv_sec, (unsigned long long)cur->tv_nsec / 1000,
stats->tskey, stats->send); stats->tskey, stats->send);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View File

@ -259,14 +259,14 @@ int main(int argc, char **argv)
goto abs_time; goto abs_time;
} else if ('a' == s.time) { } else if ('a' == s.time) {
abs_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) { } else if ('A' == s.time) {
struct tm tm; struct tm tm;
tm = *localtime(&tdut.tv_sec); 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_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, 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)); printf(" %s ", libj1939_addr2str(&src));

View File

@ -191,6 +191,7 @@ int main(int argc, char **argv)
FILE *outfile = stdout; FILE *outfile = stdout;
static int maxdev, devno, i, crlf, fdfmt, nortrdlc, d4, opt, mtu; static int maxdev, devno, i, crlf, fdfmt, nortrdlc, d4, opt, mtu;
int print_banner = 1; int print_banner = 1;
unsigned long long sec, usec;
while ((opt = getopt(argc, argv, "I:O:4nfr?")) != -1) { while ((opt = getopt(argc, argv, "I:O:4nfr?")) != -1) {
switch (opt) { switch (opt) {
@ -260,18 +261,20 @@ int main(int argc, char **argv)
if (buf[0] != '(') if (buf[0] != '(')
continue; 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) { device, ascframe, extra_info) != 5) {
/* do not evaluate the extra info */ /* do not evaluate the extra info */
extra_info[0] = 0; 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) { device, ascframe) != 4) {
fprintf(stderr, "incorrect line format in logfile\n"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }
} }
tv.tv_sec = sec;
tv.tv_usec = usec;
if (print_banner) { /* print banner */ if (print_banner) { /* print banner */
print_banner = 0; print_banner = 0;
@ -307,9 +310,9 @@ int main(int argc, char **argv)
tv.tv_sec = tv.tv_usec = 0; tv.tv_sec = tv.tv_usec = 0;
if (d4) 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 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)) if ((mtu == CAN_MTU) && (fdfmt == 0))
can_asc(&cf, devno, nortrdlc, extra_info, outfile); can_asc(&cf, devno, nortrdlc, extra_info, outfile);

View File

@ -363,8 +363,8 @@ int can2pty(int pty, int socket, int *tstamp)
if (ioctl(socket, SIOCGSTAMP, &tv) < 0) if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
perror("SIOCGSTAMP"); perror("SIOCGSTAMP");
sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX", sprintf(&buf[ptr + 2*frame.can_dlc], "%04llX",
(tv.tv_sec%60)*1000 + tv.tv_usec/1000); (unsigned long long)(tv.tv_sec%60)*1000 + tv.tv_usec/1000);
} }
strcat(buf, "\r"); /* add terminating character */ strcat(buf, "\r"); /* add terminating character */