]>
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.74 2002-07-16 03:58:16 guy Exp $ (LBL)";
31 #include <sys/types.h>
46 #ifdef TIME_WITH_SYS_TIME
51 #include "interface.h"
54 * Print out a filename (or other ascii string).
55 * If ep is NULL, assume no truncation check is needed.
56 * Return true if truncated.
59 fn_print(register const u_char
*s
, register const u_char
*ep
)
64 ret
= 1; /* assume truncated */
65 while (ep
== NULL
|| s
< ep
) {
77 c
^= 0x40; /* DEL to ?, others to alpha */
86 * Print out a counted filename (or other ascii string).
87 * If ep is NULL, assume no truncation check is needed.
88 * Return true if truncated.
91 fn_printn(register const u_char
*s
, register u_int n
,
92 register const u_char
*ep
)
97 ret
= 1; /* assume truncated */
98 while (ep
== NULL
|| s
< ep
) {
110 c
^= 0x40; /* DEL to ?, others to alpha */
119 * Print the timestamp
122 ts_print(register const struct timeval
*tvp
)
127 static unsigned b_sec
;
128 static unsigned b_usec
;
131 case 1: /* Default */
132 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
133 (void)printf("%02d:%02d:%02d.%06u ",
134 s
/ 3600, (s
% 3600) / 60, s
% 60,
135 (unsigned)tvp
->tv_usec
);
137 case -1: /* Unix timeval style */
138 (void)printf("%u.%06u ",
139 (unsigned)tvp
->tv_sec
,
140 (unsigned)tvp
->tv_usec
);
146 int d_usec
= tvp
->tv_usec
- b_usec
;
147 int d_sec
= tvp
->tv_sec
- b_sec
;
154 printf("%d. ", d_sec
);
155 printf("%06d ", d_usec
);
158 b_usec
= tvp
->tv_usec
;
160 case -3: /* Default + Date*/
161 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
162 Time
= (tvp
->tv_sec
+ thiszone
) - s
;
164 (void)printf("%02d/%02d/%04d %02d:%02d:%02d.%06u ",
165 tm
->tm_mon
+1, tm
->tm_mday
,
167 s
/ 3600, (s
% 3600) / 60,
168 s
% 60, (unsigned)tvp
->tv_usec
);
174 * Print a relative number of seconds (e.g. hold time, prune timer)
175 * in the form 5m1s. This does no truncation, so 32230861 seconds
176 * is represented as 1y1w1d1h1m1s.
179 relts_print(int secs
)
181 static const char *lengths
[] = {"y", "w", "d", "h", "m", "s"};
182 static const int seconds
[] = {31536000, 604800, 86400, 3600, 60, 1};
183 const char **l
= lengths
;
184 const int *s
= seconds
;
196 (void)printf("%d%s", secs
/ *s
, *l
);
197 secs
-= (secs
/ *s
) * *s
;
205 * Convert a token value to a string; use "fmt" if not found.
208 tok2str(register const struct tok
*lp
, register const char *fmt
,
211 static char buf
[128];
213 while (lp
->s
!= NULL
) {
220 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
225 * Convert a value to a string using an array; the macro
226 * tok2strary() in <interface.h> is the public interface to
227 * this function and ensures that the second argument is
228 * correct for bounds-checking.
231 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
234 static char buf
[128];
236 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
240 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
246 error(const char *fmt
, ...)
250 (void)fprintf(stderr
, "%s: ", program_name
);
252 (void)vfprintf(stderr
, fmt
, ap
);
257 (void)fputc('\n', stderr
);
265 warning(const char *fmt
, ...)
269 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
271 (void)vfprintf(stderr
, fmt
, ap
);
276 (void)fputc('\n', stderr
);
281 * Copy arg vector into a new buffer, concatenating arguments with spaces.
284 copy_argv(register char **argv
)
287 register u_int len
= 0;
296 len
+= strlen(*p
++) + 1;
298 buf
= (char *)malloc(len
);
300 error("copy_argv: malloc");
304 while ((src
= *p
++) != NULL
) {
305 while ((*dst
++ = *src
++) != '\0')
315 read_infile(char *fname
)
321 fd
= open(fname
, O_RDONLY
);
323 error("can't open %s: %s", fname
, pcap_strerror(errno
));
325 if (fstat(fd
, &buf
) < 0)
326 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
328 cp
= malloc((u_int
)buf
.st_size
+ 1);
330 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
331 fname
, pcap_strerror(errno
));
332 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
334 error("read %s: %s", fname
, pcap_strerror(errno
));
335 if (cc
!= buf
.st_size
)
336 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
337 cp
[(int)buf
.st_size
] = '\0';
343 safeputs(const char *s
)
356 ch
= (unsigned char)(c
& 0xff);
357 if (ch
< 0x80 && isprint(ch
))
358 printf("%c", ch
& 0xff);
360 printf("\\%03o", ch
& 0xff);