summaryrefslogtreecommitdiffstats
path: root/usr.sbin/tcpdump/util.c
diff options
context:
space:
mode:
authorjakob <jakob@openbsd.org>2001-03-05 22:34:00 +0000
committerjakob <jakob@openbsd.org>2001-03-05 22:34:00 +0000
commit3d1358a91fc0c79b06e51068abd0235aafb9ff6b (patch)
tree2a959f2ca2f607b53b4da9197bedd1d9c89a3aff /usr.sbin/tcpdump/util.c
parentBacks out changes to wsdisplay.c so alpha (and presumably others) (diff)
downloadwireguard-openbsd-3d1358a91fc0c79b06e51068abd0235aafb9ff6b.tar.xz
wireguard-openbsd-3d1358a91fc0c79b06e51068abd0235aafb9ff6b.zip
add relts_print, safeputs and safeputchar
Diffstat (limited to 'usr.sbin/tcpdump/util.c')
-rw-r--r--usr.sbin/tcpdump/util.c52
1 files changed, 50 insertions, 2 deletions
diff --git a/usr.sbin/tcpdump/util.c b/usr.sbin/tcpdump/util.c
index 843897acf38..376c5bb59a4 100644
--- a/usr.sbin/tcpdump/util.c
+++ b/usr.sbin/tcpdump/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.10 2000/10/31 16:06:49 deraadt Exp $ */
+/* $OpenBSD: util.c,v 1.11 2001/03/05 22:34:01 jakob Exp $ */
/*
* Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
@@ -23,7 +23,7 @@
#ifndef lint
static const char rcsid[] =
- "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/util.c,v 1.10 2000/10/31 16:06:49 deraadt Exp $ (LBL)";
+ "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/util.c,v 1.11 2001/03/05 22:34:01 jakob Exp $ (LBL)";
#endif
#include <sys/types.h>
@@ -138,6 +138,33 @@ 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 > 0) {
+ 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.
*/
const char *
@@ -272,3 +299,24 @@ read_infile(char *fname)
return (cp);
}
+
+void
+safeputs(const char *s)
+{
+ while (*s) {
+ safeputchar(*s);
+ s++;
+ }
+}
+
+void
+safeputchar(int c)
+{
+ unsigned char ch;
+
+ ch = (unsigned char)(c & 0xff);
+ if (c < 0x80 && isprint(c))
+ printf("%c", c & 0xff);
+ else
+ printf("\\%03o", c & 0xff);
+}