]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util.c
s/sprintf/snprintf/.
[tcpdump] / util.c
diff --git a/util.c b/util.c
index 1321f9c9e09fb480a36343ddbe19fe12ef6e8d41..9e611ae52ffe08970e4bbe1203c3fa75545b7df8 100644 (file)
--- a/util.c
+++ b/util.c
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.59 1999-11-21 03:51:05 assar Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.63 2000-01-17 06:24:27 itojun Exp $ (LBL)";
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
 #endif
 
 #include <sys/types.h>
@@ -159,6 +163,32 @@ ts_print(register const struct timeval *tvp)
        }
 }
 
+/*
+ * Print a relative number of seconds (e.g. hold time, prune timer)
+ * in the form 5m1s.  This does no truncation, so 32230861 seconds
+ * is represented as 1y1w1d1h1m1s.
+ */
+void
+relts_print(int secs)
+{
+    static char *lengths[]={"y","w","d","h","m","s"};
+    static int seconds[]={31536000,604800,86400,3600,60,1};
+    char **l = lengths;
+    int *s = seconds;
+
+    if (secs == 0) {
+       (void)printf("0s");
+       return;
+    }
+    while (secs) {
+       if (secs >= *s) {
+           (void)printf("%d%s", secs / *s, *l);
+           secs -= (secs / *s) * *s;
+       }
+       s++; l++;
+    }
+}
+
 /*
  * Convert a token value to a string; use "fmt" if not found.
  */
@@ -175,7 +205,7 @@ tok2str(register const struct tok *lp, register const char *fmt,
        }
        if (fmt == NULL)
                fmt = "#%d";
-       (void)sprintf(buf, fmt, v);
+       (void)snprintf(buf, sizeof(buf), fmt, v);
        return (buf);
 }