]> The Tcpdump Group git mirrors - tcpdump/blob - util.c
fd8843c376c105101f5d37969be87dbc9e46950c
[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.64 2000-06-01 01:13:53 assar 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 struct tm *tm;
133 time_t Time;
134 static unsigned b_sec;
135 static unsigned b_usec;
136
137 switch(tflag)
138 {
139 case 1: /* Default */
140 s = (tvp->tv_sec + thiszone) % 86400;
141 (void)printf("%02d:%02d:%02d.%06u ",
142 s / 3600, (s % 3600) / 60, s % 60,
143 (unsigned)tvp->tv_usec);
144 break;
145 case -1: /* Unix timeval style */
146 (void)printf("%u.%06u ",
147 (unsigned)tvp->tv_sec,
148 (unsigned)tvp->tv_usec);
149 break;
150 case -2:
151 if (b_sec == 0) {
152 printf("000000 ");
153 } else {
154 int d_usec = tvp->tv_usec - b_usec;
155 int d_sec = tvp->tv_sec - b_sec;
156
157 while (d_usec < 0) {
158 d_usec += 1000000;
159 d_sec--;
160 }
161 if (d_sec)
162 printf("%d. ", d_sec);
163 printf("%06d ", d_usec);
164 }
165 b_sec = tvp->tv_sec;
166 b_usec = tvp->tv_usec;
167 break;
168 case -3: /* Default + Date*/
169 s = (tvp->tv_sec + thiszone) % 86400;
170 time(&Time);
171 tm = localtime(&Time);
172 (void)printf("%02d/%02d/%04d %02d:%02d:%02d.%06u ",
173 tm->tm_mon+1, tm->tm_mday,
174 tm->tm_year+1900,
175 s / 3600, (s % 3600) / 60,
176 s % 60, (unsigned)tvp->tv_usec);
177 break;
178 }
179 }
180
181 /*
182 * Print a relative number of seconds (e.g. hold time, prune timer)
183 * in the form 5m1s. This does no truncation, so 32230861 seconds
184 * is represented as 1y1w1d1h1m1s.
185 */
186 void
187 relts_print(int secs)
188 {
189 static char *lengths[]={"y","w","d","h","m","s"};
190 static int seconds[]={31536000,604800,86400,3600,60,1};
191 char **l = lengths;
192 int *s = seconds;
193
194 if (secs == 0) {
195 (void)printf("0s");
196 return;
197 }
198 while (secs) {
199 if (secs >= *s) {
200 (void)printf("%d%s", secs / *s, *l);
201 secs -= (secs / *s) * *s;
202 }
203 s++; l++;
204 }
205 }
206
207 /*
208 * Convert a token value to a string; use "fmt" if not found.
209 */
210 const char *
211 tok2str(register const struct tok *lp, register const char *fmt,
212 register int v)
213 {
214 static char buf[128];
215
216 while (lp->s != NULL) {
217 if (lp->v == v)
218 return (lp->s);
219 ++lp;
220 }
221 if (fmt == NULL)
222 fmt = "#%d";
223 (void)snprintf(buf, sizeof(buf), fmt, v);
224 return (buf);
225 }
226
227
228 /* VARARGS */
229 __dead void
230 #if __STDC__
231 error(const char *fmt, ...)
232 #else
233 error(fmt, va_alist)
234 const char *fmt;
235 va_dcl
236 #endif
237 {
238 va_list ap;
239
240 (void)fprintf(stderr, "%s: ", program_name);
241 #if __STDC__
242 va_start(ap, fmt);
243 #else
244 va_start(ap);
245 #endif
246 (void)vfprintf(stderr, fmt, ap);
247 va_end(ap);
248 if (*fmt) {
249 fmt += strlen(fmt);
250 if (fmt[-1] != '\n')
251 (void)fputc('\n', stderr);
252 }
253 exit(1);
254 /* NOTREACHED */
255 }
256
257 /* VARARGS */
258 void
259 #if __STDC__
260 warning(const char *fmt, ...)
261 #else
262 warning(fmt, va_alist)
263 const char *fmt;
264 va_dcl
265 #endif
266 {
267 va_list ap;
268
269 (void)fprintf(stderr, "%s: WARNING: ", program_name);
270 #if __STDC__
271 va_start(ap, fmt);
272 #else
273 va_start(ap);
274 #endif
275 (void)vfprintf(stderr, fmt, ap);
276 va_end(ap);
277 if (*fmt) {
278 fmt += strlen(fmt);
279 if (fmt[-1] != '\n')
280 (void)fputc('\n', stderr);
281 }
282 }
283
284 /*
285 * Copy arg vector into a new buffer, concatenating arguments with spaces.
286 */
287 char *
288 copy_argv(register char **argv)
289 {
290 register char **p;
291 register u_int len = 0;
292 char *buf;
293 char *src, *dst;
294
295 p = argv;
296 if (*p == 0)
297 return 0;
298
299 while (*p)
300 len += strlen(*p++) + 1;
301
302 buf = (char *)malloc(len);
303 if (buf == NULL)
304 error("copy_argv: malloc");
305
306 p = argv;
307 dst = buf;
308 while ((src = *p++) != NULL) {
309 while ((*dst++ = *src++) != '\0')
310 ;
311 dst[-1] = ' ';
312 }
313 dst[-1] = '\0';
314
315 return buf;
316 }
317
318 char *
319 read_infile(char *fname)
320 {
321 register int fd, cc;
322 register char *cp;
323 struct stat buf;
324
325 fd = open(fname, O_RDONLY);
326 if (fd < 0)
327 error("can't open %s: %s", fname, pcap_strerror(errno));
328
329 if (fstat(fd, &buf) < 0)
330 error("can't stat %s: %s", fname, pcap_strerror(errno));
331
332 cp = malloc((u_int)buf.st_size + 1);
333 cc = read(fd, cp, (int)buf.st_size);
334 if (cc < 0)
335 error("read %s: %s", fname, pcap_strerror(errno));
336 if (cc != buf.st_size)
337 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
338 cp[(int)buf.st_size] = '\0';
339
340 return (cp);
341 }