]>
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.63 2000-01-17 06:24:27 itojun Exp $ (LBL)";
31 #include <sys/types.h>
53 #ifdef TIME_WITH_SYS_TIME
58 #include "interface.h"
61 * Print out a filename (or other ascii string).
62 * If ep is NULL, assume no truncation check is needed.
63 * Return true if truncated.
66 fn_print(register const u_char
*s
, register const u_char
*ep
)
71 ret
= 1; /* assume truncated */
72 while (ep
== NULL
|| s
< ep
) {
84 c
^= 0x40; /* DEL to ?, others to alpha */
93 * Print out a counted filename (or other ascii string).
94 * If ep is NULL, assume no truncation check is needed.
95 * Return true if truncated.
98 fn_printn(register const u_char
*s
, register u_int n
,
99 register const u_char
*ep
)
104 ret
= 1; /* assume truncated */
105 while (ep
== NULL
|| s
< ep
) {
117 c
^= 0x40; /* DEL to ?, others to alpha */
126 * Print the timestamp
129 ts_print(register const struct timeval
*tvp
)
135 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
136 (void)printf("%02d:%02d:%02d.%06u ",
137 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)tvp
->tv_usec
);
138 } else if (tflag
< 0) {
140 static unsigned b_sec
;
141 static unsigned b_usec
;
145 int d_usec
= tvp
->tv_usec
- b_usec
;
146 int d_sec
= tvp
->tv_sec
- b_sec
;
153 printf("%d. ", d_sec
);
154 printf("%06d ", d_usec
);
157 b_usec
= tvp
->tv_usec
;
159 /* Unix timeval style */
160 (void)printf("%u.%06u ",
161 (u_int32_t
)tvp
->tv_sec
, (u_int32_t
)tvp
->tv_usec
);
167 * Print a relative number of seconds (e.g. hold time, prune timer)
168 * in the form 5m1s. This does no truncation, so 32230861 seconds
169 * is represented as 1y1w1d1h1m1s.
172 relts_print(int secs
)
174 static char *lengths
[]={"y","w","d","h","m","s"};
175 static int seconds
[]={31536000,604800,86400,3600,60,1};
185 (void)printf("%d%s", secs
/ *s
, *l
);
186 secs
-= (secs
/ *s
) * *s
;
193 * Convert a token value to a string; use "fmt" if not found.
196 tok2str(register const struct tok
*lp
, register const char *fmt
,
199 static char buf
[128];
201 while (lp
->s
!= NULL
) {
208 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
216 error(const char *fmt
, ...)
225 (void)fprintf(stderr
, "%s: ", program_name
);
231 (void)vfprintf(stderr
, fmt
, ap
);
236 (void)fputc('\n', stderr
);
245 warning(const char *fmt
, ...)
247 warning(fmt
, va_alist
)
254 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
260 (void)vfprintf(stderr
, fmt
, ap
);
265 (void)fputc('\n', stderr
);
270 * Copy arg vector into a new buffer, concatenating arguments with spaces.
273 copy_argv(register char **argv
)
276 register u_int len
= 0;
285 len
+= strlen(*p
++) + 1;
287 buf
= (char *)malloc(len
);
289 error("copy_argv: malloc");
293 while ((src
= *p
++) != NULL
) {
294 while ((*dst
++ = *src
++) != '\0')
304 read_infile(char *fname
)
310 fd
= open(fname
, O_RDONLY
);
312 error("can't open %s: %s", fname
, pcap_strerror(errno
));
314 if (fstat(fd
, &buf
) < 0)
315 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
317 cp
= malloc((u_int
)buf
.st_size
+ 1);
318 cc
= read(fd
, cp
, (int)buf
.st_size
);
320 error("read %s: %s", fname
, pcap_strerror(errno
));
321 if (cc
!= buf
.st_size
)
322 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
323 cp
[(int)buf
.st_size
] = '\0';