]>
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.59 1999-11-21 03:51:05 assar Exp $ (LBL)";
27 #include <sys/types.h>
49 #ifdef TIME_WITH_SYS_TIME
54 #include "interface.h"
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.
62 fn_print(register const u_char
*s
, register const u_char
*ep
)
67 ret
= 1; /* assume truncated */
68 while (ep
== NULL
|| s
< ep
) {
80 c
^= 0x40; /* DEL to ?, others to alpha */
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.
94 fn_printn(register const u_char
*s
, register u_int n
,
95 register const u_char
*ep
)
100 ret
= 1; /* assume truncated */
101 while (ep
== NULL
|| s
< ep
) {
113 c
^= 0x40; /* DEL to ?, others to alpha */
122 * Print the timestamp
125 ts_print(register const struct timeval
*tvp
)
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) {
136 static unsigned b_sec
;
137 static unsigned b_usec
;
141 int d_usec
= tvp
->tv_usec
- b_usec
;
142 int d_sec
= tvp
->tv_sec
- b_sec
;
149 printf("%d. ", d_sec
);
150 printf("%06d ", d_usec
);
153 b_usec
= tvp
->tv_usec
;
155 /* Unix timeval style */
156 (void)printf("%u.%06u ",
157 (u_int32_t
)tvp
->tv_sec
, (u_int32_t
)tvp
->tv_usec
);
163 * Convert a token value to a string; use "fmt" if not found.
166 tok2str(register const struct tok
*lp
, register const char *fmt
,
169 static char buf
[128];
171 while (lp
->s
!= NULL
) {
178 (void)sprintf(buf
, fmt
, v
);
186 error(const char *fmt
, ...)
195 (void)fprintf(stderr
, "%s: ", program_name
);
201 (void)vfprintf(stderr
, fmt
, ap
);
206 (void)fputc('\n', stderr
);
215 warning(const char *fmt
, ...)
217 warning(fmt
, va_alist
)
224 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
230 (void)vfprintf(stderr
, fmt
, ap
);
235 (void)fputc('\n', stderr
);
240 * Copy arg vector into a new buffer, concatenating arguments with spaces.
243 copy_argv(register char **argv
)
246 register u_int len
= 0;
255 len
+= strlen(*p
++) + 1;
257 buf
= (char *)malloc(len
);
259 error("copy_argv: malloc");
263 while ((src
= *p
++) != NULL
) {
264 while ((*dst
++ = *src
++) != '\0')
274 read_infile(char *fname
)
280 fd
= open(fname
, O_RDONLY
);
282 error("can't open %s: %s", fname
, pcap_strerror(errno
));
284 if (fstat(fd
, &buf
) < 0)
285 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
287 cp
= malloc((u_int
)buf
.st_size
+ 1);
288 cc
= read(fd
, cp
, (int)buf
.st_size
);
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';