X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/ab6d9582afb5dccf1bc49576041aa136618be63b..1c05c2ce1d889dcee4bc4cf5b729018df88c8ad8:/util.c diff --git a/util.c b/util.c index c99bcb55..47d7a136 100644 --- a/util.c +++ b/util.c @@ -20,8 +20,8 @@ */ #ifndef lint -static const char rcsid[] = - "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.84 2003-03-04 08:53:26 guy Exp $ (LBL)"; +static const char rcsid[] _U_ = + "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.95.2.1 2005-04-25 16:15:07 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -85,15 +85,10 @@ int fn_printn(register const u_char *s, register u_int n, register const u_char *ep) { - register int ret; register u_char c; - ret = 1; /* assume truncated */ - while (ep == NULL || s < ep) { - if (n-- <= 0) { - ret = 0; - break; - } + while (n > 0 && (ep == NULL || s < ep)) { + n--; c = *s++; if (!isascii(c)) { c = toascii(c); @@ -106,7 +101,7 @@ fn_printn(register const u_char *s, register u_int n, } putchar(c); } - return(ret); + return (n == 0) ? 0 : 1; } /* @@ -121,19 +116,25 @@ ts_print(register const struct timeval *tvp) static unsigned b_sec; static unsigned b_usec; - switch(tflag) { - case 1: /* Default */ + switch (tflag) { + + 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); break; - case -1: /* Unix timeval style */ + + case 1: /* No time stamp */ + break; + + case 2: /* Unix timeval style */ (void)printf("%u.%06u ", (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec); break; - case -2: + + case 3: /* Microseconds since previous packet */ if (b_sec == 0) { printf("000000 "); } else { @@ -151,7 +152,8 @@ ts_print(register const struct timeval *tvp) b_sec = tvp->tv_sec; b_usec = tvp->tv_usec; break; - case -3: /* Default + Date*/ + + case 4: /* Default + Date*/ s = (tvp->tv_sec + thiszone) % 86400; Time = (tvp->tv_sec + thiszone) - s; tm = gmtime (&Time); @@ -204,28 +206,31 @@ relts_print(int secs) */ int -print_unknown_data(const u_char *cp,const char *lf,int len) +print_unknown_data(const u_char *cp,const char *ident,int len) { - int i; - - if (len ==0) - return(0); + hex_print(ident,cp,len); + return(1); /* everything is ok */ +} - printf("%s0x0000: ",lf); - for(i=0;is != NULL) { + if (lp->v == v) + return (lp->s); + ++lp; + } } - return(1); /* everything is ok */ + if (fmt == NULL) + fmt = "#%d"; + + (void)snprintf(buf, bufsize, fmt, v); + return (const char *)buf; } /* @@ -235,17 +240,13 @@ const char * tok2str(register const struct tok *lp, register const char *fmt, register int v) { - static char buf[128]; + static char buf[4][128]; + static int idx = 0; + char *ret; - while (lp->s != NULL) { - if (lp->v == v) - return (lp->s); - ++lp; - } - if (fmt == NULL) - fmt = "#%d"; - (void)snprintf(buf, sizeof(buf), fmt, v); - return (buf); + ret = buf[idx]; + idx = (idx+1) & 3; + return tok2strbuf(lp, fmt, v, ret, sizeof(buf[0])); } /* @@ -254,14 +255,14 @@ tok2str(register const struct tok *lp, register const char *fmt, */ char * bittok2str(register const struct tok *lp, register const char *fmt, - register int v) + register int v) { static char buf[256]; /* our stringbuffer */ int buflen=0; register int rotbit; /* this is the bit we rotate through all bitpositions */ register int tokval; - while (lp->s != NULL) { + while (lp->s != NULL && lp != NULL) { tokval=lp->v; /* load our first value */ rotbit=1; while (rotbit != 0) { @@ -333,15 +334,15 @@ mask2plen (u_int32_t mask) 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff }; - int prefix_len = 33; + int prefix_len = 32; - /* lets see if we can transform the mask into a prefixlen */ - while (prefix_len >= 0) { - if (bitmasks[prefix_len] == mask) - break; - prefix_len--; - } - return (prefix_len); + /* let's see if we can transform the mask into a prefixlen */ + while (prefix_len >= 0) { + if (bitmasks[prefix_len] == mask) + break; + prefix_len--; + } + return (prefix_len); } /* VARARGS */