]> The Tcpdump Group git mirrors - tcpdump/blob - util.c
Enhanced PIMv1 support.
[tcpdump] / util.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.61 1999-11-22 07:25:27 fenner Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/file.h>
34 #include <sys/stat.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
41 #ifdef HAVE_MALLOC_H
42 #include <malloc.h>
43 #endif
44 #include <pcap.h>
45 #include <stdio.h>
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51 #include <stdlib.h>
52 #include <string.h>
53 #ifdef TIME_WITH_SYS_TIME
54 #include <time.h>
55 #endif
56 #include <unistd.h>
57
58 #include "interface.h"
59
60 /*
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.
64 */
65 int
66 fn_print(register const u_char *s, register const u_char *ep)
67 {
68 register int ret;
69 register u_char c;
70
71 ret = 1; /* assume truncated */
72 while (ep == NULL || s < ep) {
73 c = *s++;
74 if (c == '\0') {
75 ret = 0;
76 break;
77 }
78 if (!isascii(c)) {
79 c = toascii(c);
80 putchar('M');
81 putchar('-');
82 }
83 if (!isprint(c)) {
84 c ^= 0x40; /* DEL to ?, others to alpha */
85 putchar('^');
86 }
87 putchar(c);
88 }
89 return(ret);
90 }
91
92 /*
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.
96 */
97 int
98 fn_printn(register const u_char *s, register u_int n,
99 register const u_char *ep)
100 {
101 register int ret;
102 register u_char c;
103
104 ret = 1; /* assume truncated */
105 while (ep == NULL || s < ep) {
106 if (n-- <= 0) {
107 ret = 0;
108 break;
109 }
110 c = *s++;
111 if (!isascii(c)) {
112 c = toascii(c);
113 putchar('M');
114 putchar('-');
115 }
116 if (!isprint(c)) {
117 c ^= 0x40; /* DEL to ?, others to alpha */
118 putchar('^');
119 }
120 putchar(c);
121 }
122 return(ret);
123 }
124
125 /*
126 * Print the timestamp
127 */
128 void
129 ts_print(register const struct timeval *tvp)
130 {
131 register int s;
132
133 if (tflag > 0) {
134 /* Default */
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) {
139 if (tflag < -1) {
140 static unsigned b_sec;
141 static unsigned b_usec;
142 if (b_sec == 0) {
143 printf("000000 ");
144 } else {
145 int d_usec = tvp->tv_usec - b_usec;
146 int d_sec = tvp->tv_sec - b_sec;
147
148 while (d_usec < 0) {
149 d_usec += 1000000;
150 d_sec--;
151 }
152 if (d_sec)
153 printf("%d. ", d_sec);
154 printf("%06d ", d_usec);
155 }
156 b_sec = tvp->tv_sec;
157 b_usec = tvp->tv_usec;
158 } else {
159 /* Unix timeval style */
160 (void)printf("%u.%06u ",
161 (u_int32_t)tvp->tv_sec, (u_int32_t)tvp->tv_usec);
162 }
163 }
164 }
165
166 /*
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.
170 */
171 void
172 relts_print(int secs)
173 {
174 static char *lengths[]={"y","w","d","h","m","s"};
175 static int seconds[]={31536000,604800,86400,3600,60,1};
176 char **l = lengths;
177 int *s = seconds;
178 int len;
179 int i;
180
181 if (secs == 0) {
182 (void)printf("0s");
183 return;
184 }
185 while (secs) {
186 if (secs >= *s) {
187 (void)printf("%d%s", secs / *s, *l);
188 secs -= (secs / *s) * *s;
189 }
190 s++; l++;
191 }
192 }
193
194 /*
195 * Convert a token value to a string; use "fmt" if not found.
196 */
197 const char *
198 tok2str(register const struct tok *lp, register const char *fmt,
199 register int v)
200 {
201 static char buf[128];
202
203 while (lp->s != NULL) {
204 if (lp->v == v)
205 return (lp->s);
206 ++lp;
207 }
208 if (fmt == NULL)
209 fmt = "#%d";
210 (void)sprintf(buf, fmt, v);
211 return (buf);
212 }
213
214
215 /* VARARGS */
216 __dead void
217 #if __STDC__
218 error(const char *fmt, ...)
219 #else
220 error(fmt, va_alist)
221 const char *fmt;
222 va_dcl
223 #endif
224 {
225 va_list ap;
226
227 (void)fprintf(stderr, "%s: ", program_name);
228 #if __STDC__
229 va_start(ap, fmt);
230 #else
231 va_start(ap);
232 #endif
233 (void)vfprintf(stderr, fmt, ap);
234 va_end(ap);
235 if (*fmt) {
236 fmt += strlen(fmt);
237 if (fmt[-1] != '\n')
238 (void)fputc('\n', stderr);
239 }
240 exit(1);
241 /* NOTREACHED */
242 }
243
244 /* VARARGS */
245 void
246 #if __STDC__
247 warning(const char *fmt, ...)
248 #else
249 warning(fmt, va_alist)
250 const char *fmt;
251 va_dcl
252 #endif
253 {
254 va_list ap;
255
256 (void)fprintf(stderr, "%s: WARNING: ", program_name);
257 #if __STDC__
258 va_start(ap, fmt);
259 #else
260 va_start(ap);
261 #endif
262 (void)vfprintf(stderr, fmt, ap);
263 va_end(ap);
264 if (*fmt) {
265 fmt += strlen(fmt);
266 if (fmt[-1] != '\n')
267 (void)fputc('\n', stderr);
268 }
269 }
270
271 /*
272 * Copy arg vector into a new buffer, concatenating arguments with spaces.
273 */
274 char *
275 copy_argv(register char **argv)
276 {
277 register char **p;
278 register u_int len = 0;
279 char *buf;
280 char *src, *dst;
281
282 p = argv;
283 if (*p == 0)
284 return 0;
285
286 while (*p)
287 len += strlen(*p++) + 1;
288
289 buf = (char *)malloc(len);
290 if (buf == NULL)
291 error("copy_argv: malloc");
292
293 p = argv;
294 dst = buf;
295 while ((src = *p++) != NULL) {
296 while ((*dst++ = *src++) != '\0')
297 ;
298 dst[-1] = ' ';
299 }
300 dst[-1] = '\0';
301
302 return buf;
303 }
304
305 char *
306 read_infile(char *fname)
307 {
308 register int fd, cc;
309 register char *cp;
310 struct stat buf;
311
312 fd = open(fname, O_RDONLY);
313 if (fd < 0)
314 error("can't open %s: %s", fname, pcap_strerror(errno));
315
316 if (fstat(fd, &buf) < 0)
317 error("can't stat %s: %s", fname, pcap_strerror(errno));
318
319 cp = malloc((u_int)buf.st_size + 1);
320 cc = read(fd, cp, (int)buf.st_size);
321 if (cc < 0)
322 error("read %s: %s", fname, pcap_strerror(errno));
323 if (cc != buf.st_size)
324 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
325 cp[(int)buf.st_size] = '\0';
326
327 return (cp);
328 }