]>
The Tcpdump Group git mirrors - tcpdump/blob - util.c
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.79 2002-08-06 04:42:07 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
45 #include "interface.h"
48 * Print out a filename (or other ascii string).
49 * If ep is NULL, assume no truncation check is needed.
50 * Return true if truncated.
53 fn_print(register const u_char
*s
, register const u_char
*ep
)
58 ret
= 1; /* assume truncated */
59 while (ep
== NULL
|| s
< ep
) {
71 c
^= 0x40; /* DEL to ?, others to alpha */
80 * Print out a counted filename (or other ascii string).
81 * If ep is NULL, assume no truncation check is needed.
82 * Return true if truncated.
85 fn_printn(register const u_char
*s
, register u_int n
,
86 register const u_char
*ep
)
91 ret
= 1; /* assume truncated */
92 while (ep
== NULL
|| s
< ep
) {
104 c
^= 0x40; /* DEL to ?, others to alpha */
113 * Print the timestamp
116 ts_print(register const struct timeval
*tvp
)
121 static unsigned b_sec
;
122 static unsigned b_usec
;
125 case 1: /* Default */
126 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
127 (void)printf("%02d:%02d:%02d.%06u ",
128 s
/ 3600, (s
% 3600) / 60, s
% 60,
129 (unsigned)tvp
->tv_usec
);
131 case -1: /* Unix timeval style */
132 (void)printf("%u.%06u ",
133 (unsigned)tvp
->tv_sec
,
134 (unsigned)tvp
->tv_usec
);
140 int d_usec
= tvp
->tv_usec
- b_usec
;
141 int d_sec
= tvp
->tv_sec
- b_sec
;
148 printf("%d. ", d_sec
);
149 printf("%06d ", d_usec
);
152 b_usec
= tvp
->tv_usec
;
154 case -3: /* Default + Date*/
155 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
156 Time
= (tvp
->tv_sec
+ thiszone
) - s
;
158 (void)printf("%02d/%02d/%04d %02d:%02d:%02d.%06u ",
159 tm
->tm_mon
+1, tm
->tm_mday
,
161 s
/ 3600, (s
% 3600) / 60,
162 s
% 60, (unsigned)tvp
->tv_usec
);
168 * Print a relative number of seconds (e.g. hold time, prune timer)
169 * in the form 5m1s. This does no truncation, so 32230861 seconds
170 * is represented as 1y1w1d1h1m1s.
173 relts_print(int secs
)
175 static const char *lengths
[] = {"y", "w", "d", "h", "m", "s"};
176 static const int seconds
[] = {31536000, 604800, 86400, 3600, 60, 1};
177 const char **l
= lengths
;
178 const int *s
= seconds
;
190 (void)printf("%d%s", secs
/ *s
, *l
);
191 secs
-= (secs
/ *s
) * *s
;
199 * this is a generic routine for printing unknown data;
200 * we pass on the linefeed plus indentation string to
201 * get a proper output - returns 0 on error
205 print_unknown_data(const u_char
*cp
,const char *lf
,int len
)
209 printf("%s0x0000: ",lf
);
211 if (!TTEST2(*(cp
+i
), 1)) {
212 printf("%spacket exceeded snapshot",lf
);
215 printf("%02x",*(cp
+i
));
218 if (i
/16!=(i
+1)/16) {
220 printf("%s0x%04x: ",lf
,i
);
223 return(1); /* everything is ok */
227 * Convert a token value to a string; use "fmt" if not found.
230 tok2str(register const struct tok
*lp
, register const char *fmt
,
233 static char buf
[128];
235 while (lp
->s
!= NULL
) {
242 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
247 * Convert a value to a string using an array; the macro
248 * tok2strary() in <interface.h> is the public interface to
249 * this function and ensures that the second argument is
250 * correct for bounds-checking.
253 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
256 static char buf
[128];
258 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
262 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
268 error(const char *fmt
, ...)
272 (void)fprintf(stderr
, "%s: ", program_name
);
274 (void)vfprintf(stderr
, fmt
, ap
);
279 (void)fputc('\n', stderr
);
287 warning(const char *fmt
, ...)
291 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
293 (void)vfprintf(stderr
, fmt
, ap
);
298 (void)fputc('\n', stderr
);
303 * Copy arg vector into a new buffer, concatenating arguments with spaces.
306 copy_argv(register char **argv
)
309 register u_int len
= 0;
318 len
+= strlen(*p
++) + 1;
320 buf
= (char *)malloc(len
);
322 error("copy_argv: malloc");
326 while ((src
= *p
++) != NULL
) {
327 while ((*dst
++ = *src
++) != '\0')
337 read_infile(char *fname
)
343 fd
= open(fname
, O_RDONLY
);
345 error("can't open %s: %s", fname
, pcap_strerror(errno
));
347 if (fstat(fd
, &buf
) < 0)
348 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
350 cp
= malloc((u_int
)buf
.st_size
+ 1);
352 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
353 fname
, pcap_strerror(errno
));
354 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
356 error("read %s: %s", fname
, pcap_strerror(errno
));
357 if (cc
!= buf
.st_size
)
359 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
361 /* Windows seems not to like the final \xa character */
364 pdest
=strchr( cp
, '\xa');
368 cp
[(int)buf
.st_size
] = '\0';
374 safeputs(const char *s
)
387 ch
= (unsigned char)(c
& 0xff);
388 if (ch
< 0x80 && isprint(ch
))
391 printf("\\%03o", ch
);