1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*8b26181fSAndroid Build Coastguard Worker * The Regents of the University of California. All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker *
5*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker * written permission.
17*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker */
21*8b26181fSAndroid Build Coastguard Worker
22*8b26181fSAndroid Build Coastguard Worker #include "varattrs.h"
23*8b26181fSAndroid Build Coastguard Worker
24*8b26181fSAndroid Build Coastguard Worker #ifndef lint
25*8b26181fSAndroid Build Coastguard Worker static const char copyright[] _U_ =
26*8b26181fSAndroid Build Coastguard Worker "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27*8b26181fSAndroid Build Coastguard Worker The Regents of the University of California. All rights reserved.\n";
28*8b26181fSAndroid Build Coastguard Worker #endif
29*8b26181fSAndroid Build Coastguard Worker
30*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
31*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
32*8b26181fSAndroid Build Coastguard Worker #include <string.h>
33*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
34*8b26181fSAndroid Build Coastguard Worker #include <limits.h>
35*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
36*8b26181fSAndroid Build Coastguard Worker #include "getopt.h"
37*8b26181fSAndroid Build Coastguard Worker #else
38*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
39*8b26181fSAndroid Build Coastguard Worker #endif
40*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
41*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
42*8b26181fSAndroid Build Coastguard Worker #include <signal.h>
43*8b26181fSAndroid Build Coastguard Worker #endif
44*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
45*8b26181fSAndroid Build Coastguard Worker
46*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>
47*8b26181fSAndroid Build Coastguard Worker
48*8b26181fSAndroid Build Coastguard Worker #include "pcap/funcattrs.h"
49*8b26181fSAndroid Build Coastguard Worker
50*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
51*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
52*8b26181fSAndroid Build Coastguard Worker #endif
53*8b26181fSAndroid Build Coastguard Worker
54*8b26181fSAndroid Build Coastguard Worker static char *program_name;
55*8b26181fSAndroid Build Coastguard Worker
56*8b26181fSAndroid Build Coastguard Worker /* Forwards */
57*8b26181fSAndroid Build Coastguard Worker static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
58*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN usage(void);
59*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60*8b26181fSAndroid Build Coastguard Worker static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
61*8b26181fSAndroid Build Coastguard Worker static char *copy_argv(char **);
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker static pcap_t *pd;
64*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
65*8b26181fSAndroid Build Coastguard Worker static int breaksigint = 0;
66*8b26181fSAndroid Build Coastguard Worker #endif
67*8b26181fSAndroid Build Coastguard Worker
68*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
69*8b26181fSAndroid Build Coastguard Worker static void
sigint_handler(int signum _U_)70*8b26181fSAndroid Build Coastguard Worker sigint_handler(int signum _U_)
71*8b26181fSAndroid Build Coastguard Worker {
72*8b26181fSAndroid Build Coastguard Worker if (breaksigint)
73*8b26181fSAndroid Build Coastguard Worker pcap_breakloop(pd);
74*8b26181fSAndroid Build Coastguard Worker }
75*8b26181fSAndroid Build Coastguard Worker #endif
76*8b26181fSAndroid Build Coastguard Worker
77*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
78*8b26181fSAndroid Build Coastguard Worker /*
79*8b26181fSAndroid Build Coastguard Worker * We don't have UN*X-style signals, so we don't have anything to test.
80*8b26181fSAndroid Build Coastguard Worker */
81*8b26181fSAndroid Build Coastguard Worker #define B_OPTION ""
82*8b26181fSAndroid Build Coastguard Worker #define R_OPTION ""
83*8b26181fSAndroid Build Coastguard Worker #define S_OPTION ""
84*8b26181fSAndroid Build Coastguard Worker #else
85*8b26181fSAndroid Build Coastguard Worker /*
86*8b26181fSAndroid Build Coastguard Worker * We do have UN*X-style signals (we assume that "not Windows" means "UN*X").
87*8b26181fSAndroid Build Coastguard Worker */
88*8b26181fSAndroid Build Coastguard Worker #define B_OPTION "b"
89*8b26181fSAndroid Build Coastguard Worker #define R_OPTION "r"
90*8b26181fSAndroid Build Coastguard Worker #define S_OPTION "s"
91*8b26181fSAndroid Build Coastguard Worker #endif
92*8b26181fSAndroid Build Coastguard Worker
93*8b26181fSAndroid Build Coastguard Worker #define COMMAND_OPTIONS B_OPTION "i:mn" R_OPTION S_OPTION "t:"
94*8b26181fSAndroid Build Coastguard Worker #define USAGE_OPTIONS "-" B_OPTION "mn" R_OPTION S_OPTION
95*8b26181fSAndroid Build Coastguard Worker
96*8b26181fSAndroid Build Coastguard Worker int
main(int argc,char ** argv)97*8b26181fSAndroid Build Coastguard Worker main(int argc, char **argv)
98*8b26181fSAndroid Build Coastguard Worker {
99*8b26181fSAndroid Build Coastguard Worker register int op;
100*8b26181fSAndroid Build Coastguard Worker register char *cp, *cmdbuf, *device;
101*8b26181fSAndroid Build Coastguard Worker long longarg;
102*8b26181fSAndroid Build Coastguard Worker char *p;
103*8b26181fSAndroid Build Coastguard Worker int timeout = 1000;
104*8b26181fSAndroid Build Coastguard Worker int immediate = 0;
105*8b26181fSAndroid Build Coastguard Worker int nonblock = 0;
106*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
107*8b26181fSAndroid Build Coastguard Worker int sigrestart = 0;
108*8b26181fSAndroid Build Coastguard Worker int catchsigint = 0;
109*8b26181fSAndroid Build Coastguard Worker #endif
110*8b26181fSAndroid Build Coastguard Worker pcap_if_t *devlist;
111*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 localnet, netmask;
112*8b26181fSAndroid Build Coastguard Worker struct bpf_program fcode;
113*8b26181fSAndroid Build Coastguard Worker char ebuf[PCAP_ERRBUF_SIZE];
114*8b26181fSAndroid Build Coastguard Worker int status;
115*8b26181fSAndroid Build Coastguard Worker int packet_count;
116*8b26181fSAndroid Build Coastguard Worker
117*8b26181fSAndroid Build Coastguard Worker device = NULL;
118*8b26181fSAndroid Build Coastguard Worker if ((cp = strrchr(argv[0], '/')) != NULL)
119*8b26181fSAndroid Build Coastguard Worker program_name = cp + 1;
120*8b26181fSAndroid Build Coastguard Worker else
121*8b26181fSAndroid Build Coastguard Worker program_name = argv[0];
122*8b26181fSAndroid Build Coastguard Worker
123*8b26181fSAndroid Build Coastguard Worker opterr = 0;
124*8b26181fSAndroid Build Coastguard Worker while ((op = getopt(argc, argv, COMMAND_OPTIONS)) != -1) {
125*8b26181fSAndroid Build Coastguard Worker switch (op) {
126*8b26181fSAndroid Build Coastguard Worker
127*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
128*8b26181fSAndroid Build Coastguard Worker case 'b':
129*8b26181fSAndroid Build Coastguard Worker breaksigint = 1;
130*8b26181fSAndroid Build Coastguard Worker break;
131*8b26181fSAndroid Build Coastguard Worker #endif
132*8b26181fSAndroid Build Coastguard Worker
133*8b26181fSAndroid Build Coastguard Worker case 'i':
134*8b26181fSAndroid Build Coastguard Worker device = optarg;
135*8b26181fSAndroid Build Coastguard Worker break;
136*8b26181fSAndroid Build Coastguard Worker
137*8b26181fSAndroid Build Coastguard Worker case 'm':
138*8b26181fSAndroid Build Coastguard Worker immediate = 1;
139*8b26181fSAndroid Build Coastguard Worker break;
140*8b26181fSAndroid Build Coastguard Worker
141*8b26181fSAndroid Build Coastguard Worker case 'n':
142*8b26181fSAndroid Build Coastguard Worker nonblock = 1;
143*8b26181fSAndroid Build Coastguard Worker break;
144*8b26181fSAndroid Build Coastguard Worker
145*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
146*8b26181fSAndroid Build Coastguard Worker case 'r':
147*8b26181fSAndroid Build Coastguard Worker sigrestart = 1;
148*8b26181fSAndroid Build Coastguard Worker break;
149*8b26181fSAndroid Build Coastguard Worker
150*8b26181fSAndroid Build Coastguard Worker case 's':
151*8b26181fSAndroid Build Coastguard Worker catchsigint = 1;
152*8b26181fSAndroid Build Coastguard Worker break;
153*8b26181fSAndroid Build Coastguard Worker #endif
154*8b26181fSAndroid Build Coastguard Worker
155*8b26181fSAndroid Build Coastguard Worker case 't':
156*8b26181fSAndroid Build Coastguard Worker longarg = strtol(optarg, &p, 10);
157*8b26181fSAndroid Build Coastguard Worker if (p == optarg || *p != '\0') {
158*8b26181fSAndroid Build Coastguard Worker error("Timeout value \"%s\" is not a number",
159*8b26181fSAndroid Build Coastguard Worker optarg);
160*8b26181fSAndroid Build Coastguard Worker /* NOTREACHED */
161*8b26181fSAndroid Build Coastguard Worker }
162*8b26181fSAndroid Build Coastguard Worker if (longarg < 0) {
163*8b26181fSAndroid Build Coastguard Worker error("Timeout value %ld is negative", longarg);
164*8b26181fSAndroid Build Coastguard Worker /* NOTREACHED */
165*8b26181fSAndroid Build Coastguard Worker }
166*8b26181fSAndroid Build Coastguard Worker if (longarg > INT_MAX) {
167*8b26181fSAndroid Build Coastguard Worker error("Timeout value %ld is too large (> %d)",
168*8b26181fSAndroid Build Coastguard Worker longarg, INT_MAX);
169*8b26181fSAndroid Build Coastguard Worker /* NOTREACHED */
170*8b26181fSAndroid Build Coastguard Worker }
171*8b26181fSAndroid Build Coastguard Worker timeout = (int)longarg;
172*8b26181fSAndroid Build Coastguard Worker break;
173*8b26181fSAndroid Build Coastguard Worker
174*8b26181fSAndroid Build Coastguard Worker default:
175*8b26181fSAndroid Build Coastguard Worker usage();
176*8b26181fSAndroid Build Coastguard Worker /* NOTREACHED */
177*8b26181fSAndroid Build Coastguard Worker }
178*8b26181fSAndroid Build Coastguard Worker }
179*8b26181fSAndroid Build Coastguard Worker
180*8b26181fSAndroid Build Coastguard Worker if (device == NULL) {
181*8b26181fSAndroid Build Coastguard Worker if (pcap_findalldevs(&devlist, ebuf) == -1)
182*8b26181fSAndroid Build Coastguard Worker error("%s", ebuf);
183*8b26181fSAndroid Build Coastguard Worker if (devlist == NULL)
184*8b26181fSAndroid Build Coastguard Worker error("no interfaces available for capture");
185*8b26181fSAndroid Build Coastguard Worker device = strdup(devlist->name);
186*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(devlist);
187*8b26181fSAndroid Build Coastguard Worker }
188*8b26181fSAndroid Build Coastguard Worker *ebuf = '\0';
189*8b26181fSAndroid Build Coastguard Worker
190*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
191*8b26181fSAndroid Build Coastguard Worker /*
192*8b26181fSAndroid Build Coastguard Worker * If we were told to catch SIGINT, do so.
193*8b26181fSAndroid Build Coastguard Worker */
194*8b26181fSAndroid Build Coastguard Worker if (catchsigint) {
195*8b26181fSAndroid Build Coastguard Worker struct sigaction action;
196*8b26181fSAndroid Build Coastguard Worker
197*8b26181fSAndroid Build Coastguard Worker action.sa_handler = sigint_handler;
198*8b26181fSAndroid Build Coastguard Worker sigemptyset(&action.sa_mask);
199*8b26181fSAndroid Build Coastguard Worker
200*8b26181fSAndroid Build Coastguard Worker /*
201*8b26181fSAndroid Build Coastguard Worker * Should SIGINT interrupt, or restart, system calls?
202*8b26181fSAndroid Build Coastguard Worker */
203*8b26181fSAndroid Build Coastguard Worker action.sa_flags = sigrestart ? SA_RESTART : 0;
204*8b26181fSAndroid Build Coastguard Worker
205*8b26181fSAndroid Build Coastguard Worker if (sigaction(SIGINT, &action, NULL) == -1)
206*8b26181fSAndroid Build Coastguard Worker error("Can't catch SIGINT: %s\n",
207*8b26181fSAndroid Build Coastguard Worker strerror(errno));
208*8b26181fSAndroid Build Coastguard Worker }
209*8b26181fSAndroid Build Coastguard Worker #endif
210*8b26181fSAndroid Build Coastguard Worker
211*8b26181fSAndroid Build Coastguard Worker pd = pcap_create(device, ebuf);
212*8b26181fSAndroid Build Coastguard Worker if (pd == NULL)
213*8b26181fSAndroid Build Coastguard Worker error("%s", ebuf);
214*8b26181fSAndroid Build Coastguard Worker status = pcap_set_snaplen(pd, 65535);
215*8b26181fSAndroid Build Coastguard Worker if (status != 0)
216*8b26181fSAndroid Build Coastguard Worker error("%s: pcap_set_snaplen failed: %s",
217*8b26181fSAndroid Build Coastguard Worker device, pcap_statustostr(status));
218*8b26181fSAndroid Build Coastguard Worker if (immediate) {
219*8b26181fSAndroid Build Coastguard Worker status = pcap_set_immediate_mode(pd, 1);
220*8b26181fSAndroid Build Coastguard Worker if (status != 0)
221*8b26181fSAndroid Build Coastguard Worker error("%s: pcap_set_immediate_mode failed: %s",
222*8b26181fSAndroid Build Coastguard Worker device, pcap_statustostr(status));
223*8b26181fSAndroid Build Coastguard Worker }
224*8b26181fSAndroid Build Coastguard Worker status = pcap_set_timeout(pd, timeout);
225*8b26181fSAndroid Build Coastguard Worker if (status != 0)
226*8b26181fSAndroid Build Coastguard Worker error("%s: pcap_set_timeout failed: %s",
227*8b26181fSAndroid Build Coastguard Worker device, pcap_statustostr(status));
228*8b26181fSAndroid Build Coastguard Worker status = pcap_activate(pd);
229*8b26181fSAndroid Build Coastguard Worker if (status < 0) {
230*8b26181fSAndroid Build Coastguard Worker /*
231*8b26181fSAndroid Build Coastguard Worker * pcap_activate() failed.
232*8b26181fSAndroid Build Coastguard Worker */
233*8b26181fSAndroid Build Coastguard Worker error("%s: %s\n(%s)", device,
234*8b26181fSAndroid Build Coastguard Worker pcap_statustostr(status), pcap_geterr(pd));
235*8b26181fSAndroid Build Coastguard Worker } else if (status > 0) {
236*8b26181fSAndroid Build Coastguard Worker /*
237*8b26181fSAndroid Build Coastguard Worker * pcap_activate() succeeded, but it's warning us
238*8b26181fSAndroid Build Coastguard Worker * of a problem it had.
239*8b26181fSAndroid Build Coastguard Worker */
240*8b26181fSAndroid Build Coastguard Worker warning("%s: %s\n(%s)", device,
241*8b26181fSAndroid Build Coastguard Worker pcap_statustostr(status), pcap_geterr(pd));
242*8b26181fSAndroid Build Coastguard Worker }
243*8b26181fSAndroid Build Coastguard Worker if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
244*8b26181fSAndroid Build Coastguard Worker localnet = 0;
245*8b26181fSAndroid Build Coastguard Worker netmask = 0;
246*8b26181fSAndroid Build Coastguard Worker warning("%s", ebuf);
247*8b26181fSAndroid Build Coastguard Worker }
248*8b26181fSAndroid Build Coastguard Worker cmdbuf = copy_argv(&argv[optind]);
249*8b26181fSAndroid Build Coastguard Worker
250*8b26181fSAndroid Build Coastguard Worker if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
251*8b26181fSAndroid Build Coastguard Worker error("%s", pcap_geterr(pd));
252*8b26181fSAndroid Build Coastguard Worker
253*8b26181fSAndroid Build Coastguard Worker if (pcap_setfilter(pd, &fcode) < 0)
254*8b26181fSAndroid Build Coastguard Worker error("%s", pcap_geterr(pd));
255*8b26181fSAndroid Build Coastguard Worker if (pcap_setnonblock(pd, nonblock, ebuf) == -1)
256*8b26181fSAndroid Build Coastguard Worker error("pcap_setnonblock failed: %s", ebuf);
257*8b26181fSAndroid Build Coastguard Worker printf("Listening on %s\n", device);
258*8b26181fSAndroid Build Coastguard Worker for (;;) {
259*8b26181fSAndroid Build Coastguard Worker packet_count = 0;
260*8b26181fSAndroid Build Coastguard Worker status = pcap_dispatch(pd, -1, countme,
261*8b26181fSAndroid Build Coastguard Worker (u_char *)&packet_count);
262*8b26181fSAndroid Build Coastguard Worker if (status < 0)
263*8b26181fSAndroid Build Coastguard Worker break;
264*8b26181fSAndroid Build Coastguard Worker if (status != 0) {
265*8b26181fSAndroid Build Coastguard Worker printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
266*8b26181fSAndroid Build Coastguard Worker status, packet_count);
267*8b26181fSAndroid Build Coastguard Worker struct pcap_stat ps;
268*8b26181fSAndroid Build Coastguard Worker pcap_stats(pd, &ps);
269*8b26181fSAndroid Build Coastguard Worker printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
270*8b26181fSAndroid Build Coastguard Worker ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
271*8b26181fSAndroid Build Coastguard Worker }
272*8b26181fSAndroid Build Coastguard Worker }
273*8b26181fSAndroid Build Coastguard Worker if (status == -2) {
274*8b26181fSAndroid Build Coastguard Worker /*
275*8b26181fSAndroid Build Coastguard Worker * We got interrupted, so perhaps we didn't
276*8b26181fSAndroid Build Coastguard Worker * manage to finish a line we were printing.
277*8b26181fSAndroid Build Coastguard Worker * Print an extra newline, just in case.
278*8b26181fSAndroid Build Coastguard Worker */
279*8b26181fSAndroid Build Coastguard Worker putchar('\n');
280*8b26181fSAndroid Build Coastguard Worker printf("Broken out of loop from SIGINT handler\n");
281*8b26181fSAndroid Build Coastguard Worker }
282*8b26181fSAndroid Build Coastguard Worker (void)fflush(stdout);
283*8b26181fSAndroid Build Coastguard Worker if (status == -1) {
284*8b26181fSAndroid Build Coastguard Worker /*
285*8b26181fSAndroid Build Coastguard Worker * Error. Report it.
286*8b26181fSAndroid Build Coastguard Worker */
287*8b26181fSAndroid Build Coastguard Worker (void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
288*8b26181fSAndroid Build Coastguard Worker program_name, pcap_geterr(pd));
289*8b26181fSAndroid Build Coastguard Worker }
290*8b26181fSAndroid Build Coastguard Worker pcap_close(pd);
291*8b26181fSAndroid Build Coastguard Worker pcap_freecode(&fcode);
292*8b26181fSAndroid Build Coastguard Worker free(cmdbuf);
293*8b26181fSAndroid Build Coastguard Worker exit(status == -1 ? 1 : 0);
294*8b26181fSAndroid Build Coastguard Worker }
295*8b26181fSAndroid Build Coastguard Worker
296*8b26181fSAndroid Build Coastguard Worker static void
countme(u_char * user,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)297*8b26181fSAndroid Build Coastguard Worker countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
298*8b26181fSAndroid Build Coastguard Worker {
299*8b26181fSAndroid Build Coastguard Worker int *counterp = (int *)user;
300*8b26181fSAndroid Build Coastguard Worker
301*8b26181fSAndroid Build Coastguard Worker (*counterp)++;
302*8b26181fSAndroid Build Coastguard Worker }
303*8b26181fSAndroid Build Coastguard Worker
304*8b26181fSAndroid Build Coastguard Worker static void
usage(void)305*8b26181fSAndroid Build Coastguard Worker usage(void)
306*8b26181fSAndroid Build Coastguard Worker {
307*8b26181fSAndroid Build Coastguard Worker (void)fprintf(stderr, "Usage: %s [ " USAGE_OPTIONS " ] [ -i interface ] [ -t timeout] [expression]\n",
308*8b26181fSAndroid Build Coastguard Worker program_name);
309*8b26181fSAndroid Build Coastguard Worker exit(1);
310*8b26181fSAndroid Build Coastguard Worker }
311*8b26181fSAndroid Build Coastguard Worker
312*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
313*8b26181fSAndroid Build Coastguard Worker static void
error(const char * fmt,...)314*8b26181fSAndroid Build Coastguard Worker error(const char *fmt, ...)
315*8b26181fSAndroid Build Coastguard Worker {
316*8b26181fSAndroid Build Coastguard Worker va_list ap;
317*8b26181fSAndroid Build Coastguard Worker
318*8b26181fSAndroid Build Coastguard Worker (void)fprintf(stderr, "%s: ", program_name);
319*8b26181fSAndroid Build Coastguard Worker va_start(ap, fmt);
320*8b26181fSAndroid Build Coastguard Worker (void)vfprintf(stderr, fmt, ap);
321*8b26181fSAndroid Build Coastguard Worker va_end(ap);
322*8b26181fSAndroid Build Coastguard Worker if (*fmt) {
323*8b26181fSAndroid Build Coastguard Worker fmt += strlen(fmt);
324*8b26181fSAndroid Build Coastguard Worker if (fmt[-1] != '\n')
325*8b26181fSAndroid Build Coastguard Worker (void)fputc('\n', stderr);
326*8b26181fSAndroid Build Coastguard Worker }
327*8b26181fSAndroid Build Coastguard Worker exit(1);
328*8b26181fSAndroid Build Coastguard Worker /* NOTREACHED */
329*8b26181fSAndroid Build Coastguard Worker }
330*8b26181fSAndroid Build Coastguard Worker
331*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
332*8b26181fSAndroid Build Coastguard Worker static void
warning(const char * fmt,...)333*8b26181fSAndroid Build Coastguard Worker warning(const char *fmt, ...)
334*8b26181fSAndroid Build Coastguard Worker {
335*8b26181fSAndroid Build Coastguard Worker va_list ap;
336*8b26181fSAndroid Build Coastguard Worker
337*8b26181fSAndroid Build Coastguard Worker (void)fprintf(stderr, "%s: WARNING: ", program_name);
338*8b26181fSAndroid Build Coastguard Worker va_start(ap, fmt);
339*8b26181fSAndroid Build Coastguard Worker (void)vfprintf(stderr, fmt, ap);
340*8b26181fSAndroid Build Coastguard Worker va_end(ap);
341*8b26181fSAndroid Build Coastguard Worker if (*fmt) {
342*8b26181fSAndroid Build Coastguard Worker fmt += strlen(fmt);
343*8b26181fSAndroid Build Coastguard Worker if (fmt[-1] != '\n')
344*8b26181fSAndroid Build Coastguard Worker (void)fputc('\n', stderr);
345*8b26181fSAndroid Build Coastguard Worker }
346*8b26181fSAndroid Build Coastguard Worker }
347*8b26181fSAndroid Build Coastguard Worker
348*8b26181fSAndroid Build Coastguard Worker /*
349*8b26181fSAndroid Build Coastguard Worker * Copy arg vector into a new buffer, concatenating arguments with spaces.
350*8b26181fSAndroid Build Coastguard Worker */
351*8b26181fSAndroid Build Coastguard Worker static char *
copy_argv(register char ** argv)352*8b26181fSAndroid Build Coastguard Worker copy_argv(register char **argv)
353*8b26181fSAndroid Build Coastguard Worker {
354*8b26181fSAndroid Build Coastguard Worker register char **p;
355*8b26181fSAndroid Build Coastguard Worker register size_t len = 0;
356*8b26181fSAndroid Build Coastguard Worker char *buf;
357*8b26181fSAndroid Build Coastguard Worker char *src, *dst;
358*8b26181fSAndroid Build Coastguard Worker
359*8b26181fSAndroid Build Coastguard Worker p = argv;
360*8b26181fSAndroid Build Coastguard Worker if (*p == 0)
361*8b26181fSAndroid Build Coastguard Worker return 0;
362*8b26181fSAndroid Build Coastguard Worker
363*8b26181fSAndroid Build Coastguard Worker while (*p)
364*8b26181fSAndroid Build Coastguard Worker len += strlen(*p++) + 1;
365*8b26181fSAndroid Build Coastguard Worker
366*8b26181fSAndroid Build Coastguard Worker buf = (char *)malloc(len);
367*8b26181fSAndroid Build Coastguard Worker if (buf == NULL)
368*8b26181fSAndroid Build Coastguard Worker error("copy_argv: malloc");
369*8b26181fSAndroid Build Coastguard Worker
370*8b26181fSAndroid Build Coastguard Worker p = argv;
371*8b26181fSAndroid Build Coastguard Worker dst = buf;
372*8b26181fSAndroid Build Coastguard Worker while ((src = *p++) != NULL) {
373*8b26181fSAndroid Build Coastguard Worker while ((*dst++ = *src++) != '\0')
374*8b26181fSAndroid Build Coastguard Worker ;
375*8b26181fSAndroid Build Coastguard Worker dst[-1] = ' ';
376*8b26181fSAndroid Build Coastguard Worker }
377*8b26181fSAndroid Build Coastguard Worker dst[-1] = '\0';
378*8b26181fSAndroid Build Coastguard Worker
379*8b26181fSAndroid Build Coastguard Worker return buf;
380*8b26181fSAndroid Build Coastguard Worker }
381