]> The Tcpdump Group git mirrors - tcpdump/blob - util.c
print pppOE. From <[email protected]>
[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.59 1999-11-21 03:51:05 assar Exp $ (LBL)";
25 #endif
26
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/file.h>
30 #include <sys/stat.h>
31
32 #include <ctype.h>
33 #include <errno.h>
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #ifdef HAVE_MALLOC_H
38 #include <malloc.h>
39 #endif
40 #include <pcap.h>
41 #include <stdio.h>
42 #if __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47 #include <stdlib.h>
48 #include <string.h>
49 #ifdef TIME_WITH_SYS_TIME
50 #include <time.h>
51 #endif
52 #include <unistd.h>
53
54 #include "interface.h"
55
56 /*
57 * Print out a filename (or other ascii string).
58 * If ep is NULL, assume no truncation check is needed.
59 * Return true if truncated.
60 */
61 int
62 fn_print(register const u_char *s, register const u_char *ep)
63 {
64 register int ret;
65 register u_char c;
66
67 ret = 1; /* assume truncated */
68 while (ep == NULL || s < ep) {
69 c = *s++;
70 if (c == '\0') {
71 ret = 0;
72 break;
73 }
74 if (!isascii(c)) {
75 c = toascii(c);
76 putchar('M');
77 putchar('-');
78 }
79 if (!isprint(c)) {
80 c ^= 0x40; /* DEL to ?, others to alpha */
81 putchar('^');
82 }
83 putchar(c);
84 }
85 return(ret);
86 }
87
88 /*
89 * Print out a counted filename (or other ascii string).
90 * If ep is NULL, assume no truncation check is needed.
91 * Return true if truncated.
92 */
93 int
94 fn_printn(register const u_char *s, register u_int n,
95 register const u_char *ep)
96 {
97 register int ret;
98 register u_char c;
99
100 ret = 1; /* assume truncated */
101 while (ep == NULL || s < ep) {
102 if (n-- <= 0) {
103 ret = 0;
104 break;
105 }
106 c = *s++;
107 if (!isascii(c)) {
108 c = toascii(c);
109 putchar('M');
110 putchar('-');
111 }
112 if (!isprint(c)) {
113 c ^= 0x40; /* DEL to ?, others to alpha */
114 putchar('^');
115 }
116 putchar(c);
117 }
118 return(ret);
119 }
120
121 /*
122 * Print the timestamp
123 */
124 void
125 ts_print(register const struct timeval *tvp)
126 {
127 register int s;
128
129 if (tflag > 0) {
130 /* Default */
131 s = (tvp->tv_sec + thiszone) % 86400;
132 (void)printf("%02d:%02d:%02d.%06u ",
133 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
134 } else if (tflag < 0) {
135 if (tflag < -1) {
136 static unsigned b_sec;
137 static unsigned b_usec;
138 if (b_sec == 0) {
139 printf("000000 ");
140 } else {
141 int d_usec = tvp->tv_usec - b_usec;
142 int d_sec = tvp->tv_sec - b_sec;
143
144 while (d_usec < 0) {
145 d_usec += 1000000;
146 d_sec--;
147 }
148 if (d_sec)
149 printf("%d. ", d_sec);
150 printf("%06d ", d_usec);
151 }
152 b_sec = tvp->tv_sec;
153 b_usec = tvp->tv_usec;
154 } else {
155 /* Unix timeval style */
156 (void)printf("%u.%06u ",
157 (u_int32_t)tvp->tv_sec, (u_int32_t)tvp->tv_usec);
158 }
159 }
160 }
161
162 /*
163 * Convert a token value to a string; use "fmt" if not found.
164 */
165 const char *
166 tok2str(register const struct tok *lp, register const char *fmt,
167 register int v)
168 {
169 static char buf[128];
170
171 while (lp->s != NULL) {
172 if (lp->v == v)
173 return (lp->s);
174 ++lp;
175 }
176 if (fmt == NULL)
177 fmt = "#%d";
178 (void)sprintf(buf, fmt, v);
179 return (buf);
180 }
181
182
183 /* VARARGS */
184 __dead void
185 #if __STDC__
186 error(const char *fmt, ...)
187 #else
188 error(fmt, va_alist)
189 const char *fmt;
190 va_dcl
191 #endif
192 {
193 va_list ap;
194
195 (void)fprintf(stderr, "%s: ", program_name);
196 #if __STDC__
197 va_start(ap, fmt);
198 #else
199 va_start(ap);
200 #endif
201 (void)vfprintf(stderr, fmt, ap);
202 va_end(ap);
203 if (*fmt) {
204 fmt += strlen(fmt);
205 if (fmt[-1] != '\n')
206 (void)fputc('\n', stderr);
207 }
208 exit(1);
209 /* NOTREACHED */
210 }
211
212 /* VARARGS */
213 void
214 #if __STDC__
215 warning(const char *fmt, ...)
216 #else
217 warning(fmt, va_alist)
218 const char *fmt;
219 va_dcl
220 #endif
221 {
222 va_list ap;
223
224 (void)fprintf(stderr, "%s: WARNING: ", program_name);
225 #if __STDC__
226 va_start(ap, fmt);
227 #else
228 va_start(ap);
229 #endif
230 (void)vfprintf(stderr, fmt, ap);
231 va_end(ap);
232 if (*fmt) {
233 fmt += strlen(fmt);
234 if (fmt[-1] != '\n')
235 (void)fputc('\n', stderr);
236 }
237 }
238
239 /*
240 * Copy arg vector into a new buffer, concatenating arguments with spaces.
241 */
242 char *
243 copy_argv(register char **argv)
244 {
245 register char **p;
246 register u_int len = 0;
247 char *buf;
248 char *src, *dst;
249
250 p = argv;
251 if (*p == 0)
252 return 0;
253
254 while (*p)
255 len += strlen(*p++) + 1;
256
257 buf = (char *)malloc(len);
258 if (buf == NULL)
259 error("copy_argv: malloc");
260
261 p = argv;
262 dst = buf;
263 while ((src = *p++) != NULL) {
264 while ((*dst++ = *src++) != '\0')
265 ;
266 dst[-1] = ' ';
267 }
268 dst[-1] = '\0';
269
270 return buf;
271 }
272
273 char *
274 read_infile(char *fname)
275 {
276 register int fd, cc;
277 register char *cp;
278 struct stat buf;
279
280 fd = open(fname, O_RDONLY);
281 if (fd < 0)
282 error("can't open %s: %s", fname, pcap_strerror(errno));
283
284 if (fstat(fd, &buf) < 0)
285 error("can't stat %s: %s", fname, pcap_strerror(errno));
286
287 cp = malloc((u_int)buf.st_size + 1);
288 cc = read(fd, cp, (int)buf.st_size);
289 if (cc < 0)
290 error("read %s: %s", fname, pcap_strerror(errno));
291 if (cc != buf.st_size)
292 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
293 cp[(int)buf.st_size] = '\0';
294
295 return (cp);
296 }