Introduce tok2strary(), similar to tok2str() except it takes an

array of strings instead of a list of value/string pairs.
This commit is contained in:
fenner 2001-10-08 16:12:12 +00:00
parent 5f2ed3537a
commit 405e7c0140
2 changed files with 24 additions and 2 deletions

View File

@ -18,7 +18,7 @@
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.173 2001-10-03 07:35:42 guy Exp $ (LBL)
* @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.174 2001-10-08 16:12:12 fenner Exp $ (LBL)
*/
#ifndef tcpdump_interface_h
@ -151,6 +151,9 @@ extern void relts_print(int);
extern int fn_print(const u_char *, const u_char *);
extern int fn_printn(const u_char *, u_int, const u_char *);
extern const char *tok2str(const struct tok *, const char *, int);
extern const char *tok2strary_internal(const char **, int, const char *, int);
#define tok2strary(a,f,i) tok2strary_internal(a, sizeof(a)/sizeof(a[0]),f,i)
extern const char *dnaddr_string(u_short);
extern void info(int);

21
util.c
View File

@ -21,7 +21,7 @@
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.71 2001-09-17 21:05:43 fenner Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.72 2001-10-08 16:12:13 fenner Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
@ -221,6 +221,25 @@ tok2str(register const struct tok *lp, register const char *fmt,
return (buf);
}
/*
* Convert a value to a string using an array; the macro
* tok2strary() in <interface.h> is the public interface to
* this function and ensures that the second argument is
* correct for bounds-checking.
*/
const char *
tok2strary_internal(register const char **lp, int n, register const char *fmt,
register int v)
{
static char buf[128];
if (v >= 0 && v < n && lp[v] != NULL)
return lp[v];
if (fmt == NULL)
fmt = "#%d";
(void)snprintf(buf, sizeof(buf), fmt, v);
return (buf);
}
/* VARARGS */
void