]>
git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/util.c
6df0352ee737f0a2f7f0d1d7fd2f2891962b8779
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 static const char rcsid
[] =
47 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/util.c,v 1.1.1.1 1999/05/02 03:58:34 wsanchez Exp $ (LBL)";
50 #include <sys/types.h>
69 #ifdef TIME_WITH_SYS_TIME
74 #include "interface.h"
77 * Print out a filename (or other ascii string).
78 * If ep is NULL, assume no truncation check is needed.
79 * Return true if truncated.
82 fn_print(register const u_char
*s
, register const u_char
*ep
)
87 ret
= 1; /* assume truncated */
88 while (ep
== NULL
|| s
< ep
) {
100 c
^= 0x40; /* DEL to ?, others to alpha */
109 * Print out a counted filename (or other ascii string).
110 * If ep is NULL, assume no truncation check is needed.
111 * Return true if truncated.
114 fn_printn(register const u_char
*s
, register u_int n
,
115 register const u_char
*ep
)
120 ret
= 1; /* assume truncated */
121 while (ep
== NULL
|| s
< ep
) {
133 c
^= 0x40; /* DEL to ?, others to alpha */
142 * Print the timestamp
145 ts_print(register const struct timeval
*tvp
)
151 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
152 (void)printf("%02d:%02d:%02d.%06u ",
153 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)tvp
->tv_usec
);
154 } else if (tflag
< 0) {
155 /* Unix timeval style */
156 (void)printf("%u.%06u ",
157 (u_int32_t
)tvp
->tv_sec
, (u_int32_t
)tvp
->tv_usec
);
162 * Convert a token value to a string; use "fmt" if not found.
165 tok2str(register const struct tok
*lp
, register const char *fmt
,
168 static char buf
[128];
170 while (lp
->s
!= NULL
) {
177 (void)sprintf(buf
, fmt
, v
);
185 error(const char *fmt
, ...)
194 (void)fprintf(stderr
, "%s: ", program_name
);
200 (void)vfprintf(stderr
, fmt
, ap
);
205 (void)fputc('\n', stderr
);
214 warning(const char *fmt
, ...)
216 warning(fmt
, va_alist
)
223 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
229 (void)vfprintf(stderr
, fmt
, ap
);
234 (void)fputc('\n', stderr
);
239 * Copy arg vector into a new buffer, concatenating arguments with spaces.
242 copy_argv(register char **argv
)
245 register u_int len
= 0;
254 len
+= strlen(*p
++) + 1;
256 buf
= (char *)malloc(len
);
258 error("copy_argv: malloc");
262 while ((src
= *p
++) != NULL
) {
263 while ((*dst
++ = *src
++) != '\0')
272 /* A replacement for strdup() that cuts down on malloc() overhead */
274 savestr(register const char *str
)
278 static char *strptr
= NULL
;
279 static u_int strsize
= 0;
281 size
= strlen(str
) + 1;
282 if (size
> strsize
) {
286 strptr
= (char *)malloc(strsize
);
288 error("savestr: malloc");
290 (void)strcpy(strptr
, str
);
298 read_infile(char *fname
)
304 fd
= open(fname
, O_RDONLY
);
306 error("can't open %s: %s", fname
, pcap_strerror(errno
));
308 if (fstat(fd
, &buf
) < 0)
309 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
311 cp
= malloc((u_int
)buf
.st_size
+ 1);
312 cc
= read(fd
, cp
, (int)buf
.st_size
);
314 error("read %s: %s", fname
, pcap_strerror(errno
));
315 if (cc
!= buf
.st_size
)
316 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
317 cp
[(int)buf
.st_size
] = '\0';
323 * Returns the difference between gmt and local time in seconds.
324 * Use gmtime() and localtime() to keep things simple.
329 register int dt
, dir
;
330 register struct tm
*gmt
, *loc
;
338 dt
= (loc
->tm_hour
- gmt
->tm_hour
) * 60 * 60 +
339 (loc
->tm_min
- gmt
->tm_min
) * 60;
342 * If the year or julian day is different, we span 00:00 GMT
343 * and must add or subtract a day. Check the year first to
344 * avoid problems when the julian day wraps.
346 dir
= loc
->tm_year
- gmt
->tm_year
;
348 dir
= loc
->tm_yday
- gmt
->tm_yday
;
349 dt
+= dir
* 24 * 60 * 60;