From: hannes Date: Tue, 13 Dec 2005 08:37:22 +0000 (+0000) Subject: - add the -ttttt timestamp option which prints the time difference X-Git-Tag: tcpdump-4.0.0~281 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/a6df5512cfdf7d7221d63d40d7329eaae6b93481 - add the -ttttt timestamp option which prints the time difference (in micro-second resolution) between the first and current packet. - cleanup the the ts_print code a bit -> add a ts_format helper --- diff --git a/tcpdump.1 b/tcpdump.1 index cff629d3..d3281c3e 100644 --- a/tcpdump.1 +++ b/tcpdump.1 @@ -1,4 +1,4 @@ -.\" @(#) $Header: /tcpdump/master/tcpdump/Attic/tcpdump.1,v 1.179 2005-12-05 20:10:58 guy Exp $ (LBL) +.\" @(#) $Header: /tcpdump/master/tcpdump/Attic/tcpdump.1,v 1.180 2005-12-13 08:37:23 hannes Exp $ (LBL) .\" .\" $NetBSD: tcpdump.8,v 1.9 2003/03/31 00:18:17 perry Exp $ .\" @@ -496,12 +496,16 @@ and Print an unformatted timestamp on each dump line. .TP .B \-ttt -Print a delta (in micro-seconds) between current and previous line +Print a delta (micro-second resolution) between current and previous line on each dump line. .TP .B \-tttt Print a timestamp in default format proceeded by date on each dump line. .TP +.B \-ttttt +Print a delta (micro-second resolution) between current and first line +on each dump line. +.TP .B \-u Print undecoded NFS handles. .TP diff --git a/tcpdump.c b/tcpdump.c index 8a9ac9d9..116f3daa 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -30,7 +30,7 @@ static const char copyright[] _U_ = "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\ The Regents of the University of California. All rights reserved.\n"; static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.264 2005-12-05 20:24:48 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.265 2005-12-13 08:37:22 hannes Exp $ (LBL)"; #endif /* @@ -834,10 +834,11 @@ main(int argc, char **argv) case 1: /* No time stamp */ case 2: /* Unix timeval style */ case 3: /* Microseconds since previous packet */ + case 5: /* Microseconds since first packet */ break; default: /* Not supported */ - error("only -t, -tt, -ttt, and -tttt are supported"); + error("only -t, -tt, -ttt, -tttt and -ttttt are supported"); break; } diff --git a/util.c b/util.c index 28c0f33d..13d29974 100644 --- a/util.c +++ b/util.c @@ -21,7 +21,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.102 2005-12-05 20:24:48 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.103 2005-12-13 08:37:23 hannes Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -44,6 +44,8 @@ static const char rcsid[] _U_ = #include "interface.h" +char * ts_format(register int, register int); + /* * Print out a null-terminated filename (or other ascii string). * If ep is NULL, assume no truncation check is needed. @@ -138,6 +140,19 @@ fn_printzp(register const u_char *s, register u_int n, return (n == 0) ? 0 : ret; } +/* + * Format the timestamp + */ +char * +ts_format(register int sec, register int usec) +{ + static char buf[sizeof("00:00:00.000000")]; + (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%06u", + sec / 3600, (sec % 3600) / 60, sec % 60, usec); + + return buf; +} + /* * Print the timestamp */ @@ -154,9 +169,7 @@ ts_print(register const struct timeval *tvp) case 0: /* Default */ s = (tvp->tv_sec + thiszone) % 86400; - (void)printf("%02d:%02d:%02d.%06u ", - s / 3600, (s % 3600) / 60, s % 60, - (unsigned)tvp->tv_usec); + (void)printf("%s ", ts_format(s, tvp->tv_usec)); break; case 1: /* No time stamp */ @@ -169,22 +182,27 @@ ts_print(register const struct timeval *tvp) break; case 3: /* Microseconds since previous packet */ - if (b_sec == 0) { - printf("000000 "); - } else { - int d_usec = tvp->tv_usec - b_usec; - int d_sec = tvp->tv_sec - b_sec; - - while (d_usec < 0) { - d_usec += 1000000; - d_sec--; - } - if (d_sec) - printf("%d. ", d_sec); - printf("%06d ", d_usec); - } - b_sec = tvp->tv_sec; - b_usec = tvp->tv_usec; + case 5: /* Microseconds since first packet */ + if (b_sec == 0 && tflag == 5) { + /* init timestamp for first packet */ + b_usec = tvp->tv_usec; + b_sec = tvp->tv_sec; + } + + int d_usec = tvp->tv_usec - b_usec; + int d_sec = tvp->tv_sec - b_sec; + + while (d_usec < 0) { + d_usec += 1000000; + d_sec--; + } + + (void)printf("%s ", ts_format(d_sec, d_usec)); + + if (tflag == 3) { /* set timestamp for last packet */ + b_sec = tvp->tv_sec; + b_usec = tvp->tv_usec; + } break; case 4: /* Default + Date*/ @@ -194,10 +212,9 @@ ts_print(register const struct timeval *tvp) if (!tm) printf("Date fail "); else - printf("%04d-%02d-%02d ", - tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); - printf("%02d:%02d:%02d.%06u ", - s / 3600, (s % 3600) / 60, s % 60, (unsigned)tvp->tv_usec); + printf("%04d-%02d-%02d %s ", + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, + ts_format(s, tvp->tv_usec)); break; } }