xref: /freebsd/contrib/libpcap/testprogs/selpolltest.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
157e22627SCy Schubert /*
257e22627SCy Schubert  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
357e22627SCy Schubert  *	The Regents of the University of California.  All rights reserved.
457e22627SCy Schubert  *
557e22627SCy Schubert  * Redistribution and use in source and binary forms, with or without
657e22627SCy Schubert  * modification, are permitted provided that: (1) source code distributions
757e22627SCy Schubert  * retain the above copyright notice and this paragraph in its entirety, (2)
857e22627SCy Schubert  * distributions including binary code include the above copyright notice and
957e22627SCy Schubert  * this paragraph in its entirety in the documentation or other materials
1057e22627SCy Schubert  * provided with the distribution, and (3) all advertising materials mentioning
1157e22627SCy Schubert  * features or use of this software display the following acknowledgement:
1257e22627SCy Schubert  * ``This product includes software developed by the University of California,
1357e22627SCy Schubert  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1457e22627SCy Schubert  * the University nor the names of its contributors may be used to endorse
1557e22627SCy Schubert  * or promote products derived from this software without specific prior
1657e22627SCy Schubert  * written permission.
1757e22627SCy Schubert  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1857e22627SCy Schubert  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1957e22627SCy Schubert  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2057e22627SCy Schubert  */
2157e22627SCy Schubert 
2257e22627SCy Schubert #include "varattrs.h"
2357e22627SCy Schubert 
2457e22627SCy Schubert #ifndef lint
2557e22627SCy Schubert static const char copyright[] _U_ =
2657e22627SCy Schubert     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
2757e22627SCy Schubert The Regents of the University of California.  All rights reserved.\n";
2857e22627SCy Schubert #endif
2957e22627SCy Schubert 
3057e22627SCy Schubert /*
3157e22627SCy Schubert  * Tests how select() and poll() behave on the selectable file descriptor
3257e22627SCy Schubert  * for a pcap_t.
3357e22627SCy Schubert  *
3457e22627SCy Schubert  * This would be significantly different on Windows, as it'd test
3557e22627SCy Schubert  * how WaitForMultipleObjects() would work on the event handle for a
3657e22627SCy Schubert  * pcap_t.
3757e22627SCy Schubert  */
3857e22627SCy Schubert #include <pcap.h>
3957e22627SCy Schubert #include <stdio.h>
4057e22627SCy Schubert #include <stdlib.h>
4157e22627SCy Schubert #include <string.h>
4257e22627SCy Schubert #include <stdarg.h>
4357e22627SCy Schubert #include <unistd.h>
4457e22627SCy Schubert #include <errno.h>
4557e22627SCy Schubert #include <sys/types.h>
4657e22627SCy Schubert #ifdef HAVE_SYS_SELECT_H
4757e22627SCy Schubert #include <sys/select.h>
4857e22627SCy Schubert #else
4957e22627SCy Schubert #include <sys/time.h>	/* older UN*Xes */
5057e22627SCy Schubert #endif
5157e22627SCy Schubert #include <poll.h>
5257e22627SCy Schubert 
5357e22627SCy Schubert #include "pcap/funcattrs.h"
5457e22627SCy Schubert 
5557e22627SCy Schubert static char *program_name;
5657e22627SCy Schubert 
5757e22627SCy Schubert /* Forwards */
5857e22627SCy Schubert static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
5957e22627SCy Schubert static void PCAP_NORETURN usage(void);
6057e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
6157e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
6257e22627SCy Schubert static char *copy_argv(char **);
6357e22627SCy Schubert 
6457e22627SCy Schubert static pcap_t *pd;
6557e22627SCy Schubert 
6657e22627SCy Schubert int
main(int argc,char ** argv)6757e22627SCy Schubert main(int argc, char **argv)
6857e22627SCy Schubert {
6957e22627SCy Schubert 	register int op;
7057e22627SCy Schubert 	bpf_u_int32 localnet, netmask;
7157e22627SCy Schubert 	register char *cp, *cmdbuf, *device;
72*6f9cba8fSJoseph Mingrone 	int doselect, dopoll, dotimeout, dononblock, quiet;
7357e22627SCy Schubert 	const char *mechanism;
7457e22627SCy Schubert 	struct bpf_program fcode;
7557e22627SCy Schubert 	char ebuf[PCAP_ERRBUF_SIZE];
7657e22627SCy Schubert 	pcap_if_t *devlist;
77*6f9cba8fSJoseph Mingrone 	int selectable_fd = -1;
78*6f9cba8fSJoseph Mingrone 	const struct timeval *required_timeout;
7957e22627SCy Schubert 	int status;
8057e22627SCy Schubert 	int packet_count;
8157e22627SCy Schubert 
8257e22627SCy Schubert 	device = NULL;
8357e22627SCy Schubert 	doselect = 0;
8457e22627SCy Schubert 	dopoll = 0;
8557e22627SCy Schubert 	mechanism = NULL;
8657e22627SCy Schubert 	dotimeout = 0;
8757e22627SCy Schubert 	dononblock = 0;
88*6f9cba8fSJoseph Mingrone 	quiet = 0;
8957e22627SCy Schubert 	if ((cp = strrchr(argv[0], '/')) != NULL)
9057e22627SCy Schubert 		program_name = cp + 1;
9157e22627SCy Schubert 	else
9257e22627SCy Schubert 		program_name = argv[0];
9357e22627SCy Schubert 
9457e22627SCy Schubert 	opterr = 0;
95*6f9cba8fSJoseph Mingrone 	while ((op = getopt(argc, argv, "i:sptnq")) != -1) {
9657e22627SCy Schubert 		switch (op) {
9757e22627SCy Schubert 
9857e22627SCy Schubert 		case 'i':
9957e22627SCy Schubert 			device = optarg;
10057e22627SCy Schubert 			break;
10157e22627SCy Schubert 
10257e22627SCy Schubert 		case 's':
10357e22627SCy Schubert 			doselect = 1;
10457e22627SCy Schubert 			mechanism = "select() and pcap_dispatch()";
10557e22627SCy Schubert 			break;
10657e22627SCy Schubert 
10757e22627SCy Schubert 		case 'p':
10857e22627SCy Schubert 			dopoll = 1;
10957e22627SCy Schubert 			mechanism = "poll() and pcap_dispatch()";
11057e22627SCy Schubert 			break;
11157e22627SCy Schubert 
11257e22627SCy Schubert 		case 't':
11357e22627SCy Schubert 			dotimeout = 1;
11457e22627SCy Schubert 			break;
11557e22627SCy Schubert 
11657e22627SCy Schubert 		case 'n':
11757e22627SCy Schubert 			dononblock = 1;
11857e22627SCy Schubert 			break;
11957e22627SCy Schubert 
120*6f9cba8fSJoseph Mingrone 		case 'q':
121*6f9cba8fSJoseph Mingrone 			quiet = 1;
122*6f9cba8fSJoseph Mingrone 			break;
123*6f9cba8fSJoseph Mingrone 
12457e22627SCy Schubert 		default:
12557e22627SCy Schubert 			usage();
12657e22627SCy Schubert 			/* NOTREACHED */
12757e22627SCy Schubert 		}
12857e22627SCy Schubert 	}
12957e22627SCy Schubert 
13057e22627SCy Schubert 	if (doselect && dopoll) {
13157e22627SCy Schubert 		fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n");
13257e22627SCy Schubert 		return 1;
13357e22627SCy Schubert 	}
13457e22627SCy Schubert 	if (dotimeout && !doselect && !dopoll) {
13557e22627SCy Schubert 		fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
13657e22627SCy Schubert 		return 1;
13757e22627SCy Schubert 	}
13857e22627SCy Schubert 	if (device == NULL) {
13957e22627SCy Schubert 		if (pcap_findalldevs(&devlist, ebuf) == -1)
14057e22627SCy Schubert 			error("%s", ebuf);
14157e22627SCy Schubert 		if (devlist == NULL)
14257e22627SCy Schubert 			error("no interfaces available for capture");
14357e22627SCy Schubert 		device = strdup(devlist->name);
14457e22627SCy Schubert 		pcap_freealldevs(devlist);
14557e22627SCy Schubert 	}
14657e22627SCy Schubert 	*ebuf = '\0';
14757e22627SCy Schubert 	pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
14857e22627SCy Schubert 	if (pd == NULL)
14957e22627SCy Schubert 		error("%s", ebuf);
15057e22627SCy Schubert 	else if (*ebuf)
15157e22627SCy Schubert 		warning("%s", ebuf);
15257e22627SCy Schubert 	if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
15357e22627SCy Schubert 		localnet = 0;
15457e22627SCy Schubert 		netmask = 0;
15557e22627SCy Schubert 		warning("%s", ebuf);
15657e22627SCy Schubert 	}
15757e22627SCy Schubert 	cmdbuf = copy_argv(&argv[optind]);
15857e22627SCy Schubert 
15957e22627SCy Schubert 	if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
16057e22627SCy Schubert 		error("%s", pcap_geterr(pd));
16157e22627SCy Schubert 	if (pcap_setfilter(pd, &fcode) < 0)
16257e22627SCy Schubert 		error("%s", pcap_geterr(pd));
16357e22627SCy Schubert 
16457e22627SCy Schubert 	if (doselect || dopoll) {
16557e22627SCy Schubert 		/*
16657e22627SCy Schubert 		 * We need either an FD on which to do select()/poll()
16757e22627SCy Schubert 		 * or, if there isn't one, a timeout to use in select()/
16857e22627SCy Schubert 		 * poll().
16957e22627SCy Schubert 		 */
17057e22627SCy Schubert 		selectable_fd = pcap_get_selectable_fd(pd);
17157e22627SCy Schubert 		if (selectable_fd == -1) {
17257e22627SCy Schubert 			printf("Listening on %s, using %s, with a timeout\n",
17357e22627SCy Schubert 			    device, mechanism);
17457e22627SCy Schubert 			required_timeout = pcap_get_required_select_timeout(pd);
17557e22627SCy Schubert 			if (required_timeout == NULL)
17657e22627SCy Schubert 				error("select()/poll() isn't supported on %s, even with a timeout",
17757e22627SCy Schubert 				    device);
17857e22627SCy Schubert 
17957e22627SCy Schubert 			/*
18057e22627SCy Schubert 			 * As we won't be notified by select() or poll()
18157e22627SCy Schubert 			 * that a read can be done, we'll have to periodically
18257e22627SCy Schubert 			 * try reading from the device every time the required
18357e22627SCy Schubert 			 * timeout expires, and we don't want those attempts
18457e22627SCy Schubert 			 * to block if nothing has arrived in that interval,
18557e22627SCy Schubert 			 * so we want to force non-blocking mode.
18657e22627SCy Schubert 			 */
18757e22627SCy Schubert 			dononblock = 1;
18857e22627SCy Schubert 		} else {
18957e22627SCy Schubert 			printf("Listening on %s, using %s\n", device,
19057e22627SCy Schubert 			    mechanism);
19157e22627SCy Schubert 			required_timeout = NULL;
19257e22627SCy Schubert 		}
19357e22627SCy Schubert 	} else
19457e22627SCy Schubert 		printf("Listening on %s, using pcap_dispatch()\n", device);
19557e22627SCy Schubert 
19657e22627SCy Schubert 	if (dononblock) {
19757e22627SCy Schubert 		if (pcap_setnonblock(pd, 1, ebuf) == -1)
19857e22627SCy Schubert 			error("pcap_setnonblock failed: %s", ebuf);
19957e22627SCy Schubert 	}
20057e22627SCy Schubert 	if (doselect) {
20157e22627SCy Schubert 		for (;;) {
20257e22627SCy Schubert 			fd_set setread, setexcept;
20357e22627SCy Schubert 			struct timeval seltimeout;
204*6f9cba8fSJoseph Mingrone 			struct timeval *timeoutp;
20557e22627SCy Schubert 
20657e22627SCy Schubert 			FD_ZERO(&setread);
20757e22627SCy Schubert 			if (selectable_fd != -1) {
20857e22627SCy Schubert 				FD_SET(selectable_fd, &setread);
20957e22627SCy Schubert 				FD_ZERO(&setexcept);
21057e22627SCy Schubert 				FD_SET(selectable_fd, &setexcept);
21157e22627SCy Schubert 			}
212*6f9cba8fSJoseph Mingrone 			required_timeout = pcap_get_required_select_timeout(pd);
21357e22627SCy Schubert 			if (dotimeout) {
21457e22627SCy Schubert 				seltimeout.tv_sec = 0;
21557e22627SCy Schubert 				if (required_timeout != NULL &&
21657e22627SCy Schubert 				    required_timeout->tv_usec < 1000)
21757e22627SCy Schubert 					seltimeout.tv_usec = required_timeout->tv_usec;
21857e22627SCy Schubert 				else
21957e22627SCy Schubert 					seltimeout.tv_usec = 1000;
220*6f9cba8fSJoseph Mingrone 				timeoutp = &seltimeout;
22157e22627SCy Schubert 			} else if (required_timeout != NULL) {
22257e22627SCy Schubert 				seltimeout = *required_timeout;
223*6f9cba8fSJoseph Mingrone 				timeoutp = &seltimeout;
22457e22627SCy Schubert 			} else {
225*6f9cba8fSJoseph Mingrone 				timeoutp = NULL;
22657e22627SCy Schubert 			}
227*6f9cba8fSJoseph Mingrone 			status = select((selectable_fd == -1) ?
228*6f9cba8fSJoseph Mingrone 			    0 : selectable_fd + 1, &setread, NULL, &setexcept,
229*6f9cba8fSJoseph Mingrone 			    timeoutp);
23057e22627SCy Schubert 			if (status == -1) {
23157e22627SCy Schubert 				printf("Select returns error (%s)\n",
23257e22627SCy Schubert 				    strerror(errno));
23357e22627SCy Schubert 			} else {
234*6f9cba8fSJoseph Mingrone 				if (!quiet) {
23557e22627SCy Schubert 					if (status == 0)
23657e22627SCy Schubert 						printf("Select timed out: ");
237*6f9cba8fSJoseph Mingrone 					else{
23857e22627SCy Schubert 						printf("Select returned a descriptor: ");
23957e22627SCy Schubert 						if (FD_ISSET(selectable_fd, &setread))
24057e22627SCy Schubert 							printf("readable, ");
24157e22627SCy Schubert 						else
24257e22627SCy Schubert 							printf("not readable, ");
24357e22627SCy Schubert 						if (FD_ISSET(selectable_fd, &setexcept))
24457e22627SCy Schubert 							printf("exceptional condition\n");
24557e22627SCy Schubert 						else
24657e22627SCy Schubert 							printf("no exceptional condition\n");
24757e22627SCy Schubert 					}
248*6f9cba8fSJoseph Mingrone 				}
24957e22627SCy Schubert 				packet_count = 0;
25057e22627SCy Schubert 				status = pcap_dispatch(pd, -1, countme,
25157e22627SCy Schubert 				    (u_char *)&packet_count);
25257e22627SCy Schubert 				if (status < 0)
25357e22627SCy Schubert 					break;
25457e22627SCy Schubert 				/*
25557e22627SCy Schubert 				 * Don't report this if we're using a
25657e22627SCy Schubert 				 * required timeout and we got no packets,
25757e22627SCy Schubert 				 * because that could be a very short timeout,
25857e22627SCy Schubert 				 * and we don't want to spam the user with
25957e22627SCy Schubert 				 * a ton of "no packets" reports.
26057e22627SCy Schubert 				 */
26157e22627SCy Schubert 				if (status != 0 || packet_count != 0 ||
26257e22627SCy Schubert 				    required_timeout != NULL) {
26357e22627SCy Schubert 					printf("%d packets seen, %d packets counted after select returns\n",
26457e22627SCy Schubert 					    status, packet_count);
26557e22627SCy Schubert 				}
26657e22627SCy Schubert 			}
26757e22627SCy Schubert 		}
26857e22627SCy Schubert 	} else if (dopoll) {
26957e22627SCy Schubert 		for (;;) {
27057e22627SCy Schubert 			struct pollfd fd;
27157e22627SCy Schubert 			int polltimeout;
27257e22627SCy Schubert 
27357e22627SCy Schubert 			fd.fd = selectable_fd;
27457e22627SCy Schubert 			fd.events = POLLIN;
275*6f9cba8fSJoseph Mingrone 			required_timeout = pcap_get_required_select_timeout(pd);
27657e22627SCy Schubert 			if (dotimeout)
27757e22627SCy Schubert 				polltimeout = 1;
27857e22627SCy Schubert 			else if (required_timeout != NULL &&
27957e22627SCy Schubert 			    required_timeout->tv_usec >= 1000)
280*6f9cba8fSJoseph Mingrone 				polltimeout = (int)(required_timeout->tv_usec/1000);
28157e22627SCy Schubert 			else
28257e22627SCy Schubert 				polltimeout = -1;
28357e22627SCy Schubert 			status = poll(&fd, (selectable_fd == -1) ? 0 : 1, polltimeout);
28457e22627SCy Schubert 			if (status == -1) {
28557e22627SCy Schubert 				printf("Poll returns error (%s)\n",
28657e22627SCy Schubert 				    strerror(errno));
28757e22627SCy Schubert 			} else {
288*6f9cba8fSJoseph Mingrone 				if (!quiet) {
28957e22627SCy Schubert 					if (status == 0)
29057e22627SCy Schubert 						printf("Poll timed out\n");
29157e22627SCy Schubert 					else {
29257e22627SCy Schubert 						printf("Poll returned a descriptor: ");
29357e22627SCy Schubert 						if (fd.revents & POLLIN)
29457e22627SCy Schubert 							printf("readable, ");
29557e22627SCy Schubert 						else
29657e22627SCy Schubert 							printf("not readable, ");
29757e22627SCy Schubert 						if (fd.revents & POLLERR)
29857e22627SCy Schubert 							printf("exceptional condition, ");
29957e22627SCy Schubert 						else
30057e22627SCy Schubert 							printf("no exceptional condition, ");
30157e22627SCy Schubert 						if (fd.revents & POLLHUP)
30257e22627SCy Schubert 							printf("disconnect, ");
30357e22627SCy Schubert 						else
30457e22627SCy Schubert 							printf("no disconnect, ");
30557e22627SCy Schubert 						if (fd.revents & POLLNVAL)
30657e22627SCy Schubert 							printf("invalid\n");
30757e22627SCy Schubert 						else
30857e22627SCy Schubert 							printf("not invalid\n");
30957e22627SCy Schubert 					}
31057e22627SCy Schubert 				}
31157e22627SCy Schubert 				packet_count = 0;
31257e22627SCy Schubert 				status = pcap_dispatch(pd, -1, countme,
31357e22627SCy Schubert 				    (u_char *)&packet_count);
31457e22627SCy Schubert 				if (status < 0)
31557e22627SCy Schubert 					break;
31657e22627SCy Schubert 				/*
31757e22627SCy Schubert 				 * Don't report this if we're using a
31857e22627SCy Schubert 				 * required timeout and we got no packets,
31957e22627SCy Schubert 				 * because that could be a very short timeout,
32057e22627SCy Schubert 				 * and we don't want to spam the user with
32157e22627SCy Schubert 				 * a ton of "no packets" reports.
32257e22627SCy Schubert 				 */
32357e22627SCy Schubert 				if (status != 0 || packet_count != 0 ||
32457e22627SCy Schubert 				    required_timeout != NULL) {
32557e22627SCy Schubert 					printf("%d packets seen, %d packets counted after poll returns\n",
32657e22627SCy Schubert 					    status, packet_count);
32757e22627SCy Schubert 				}
32857e22627SCy Schubert 			}
32957e22627SCy Schubert 		}
33057e22627SCy Schubert 	} else {
33157e22627SCy Schubert 		for (;;) {
33257e22627SCy Schubert 			packet_count = 0;
33357e22627SCy Schubert 			status = pcap_dispatch(pd, -1, countme,
33457e22627SCy Schubert 			    (u_char *)&packet_count);
33557e22627SCy Schubert 			if (status < 0)
33657e22627SCy Schubert 				break;
33757e22627SCy Schubert 			printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
33857e22627SCy Schubert 			    status, packet_count);
33957e22627SCy Schubert 		}
34057e22627SCy Schubert 	}
34157e22627SCy Schubert 	if (status == -2) {
34257e22627SCy Schubert 		/*
34357e22627SCy Schubert 		 * We got interrupted, so perhaps we didn't
34457e22627SCy Schubert 		 * manage to finish a line we were printing.
34557e22627SCy Schubert 		 * Print an extra newline, just in case.
34657e22627SCy Schubert 		 */
34757e22627SCy Schubert 		putchar('\n');
34857e22627SCy Schubert 	}
34957e22627SCy Schubert 	(void)fflush(stdout);
35057e22627SCy Schubert 	if (status == -1) {
35157e22627SCy Schubert 		/*
35257e22627SCy Schubert 		 * Error.  Report it.
35357e22627SCy Schubert 		 */
354*6f9cba8fSJoseph Mingrone 		(void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
35557e22627SCy Schubert 		    program_name, pcap_geterr(pd));
35657e22627SCy Schubert 	}
35757e22627SCy Schubert 	pcap_close(pd);
35857e22627SCy Schubert 	exit(status == -1 ? 1 : 0);
35957e22627SCy Schubert }
36057e22627SCy Schubert 
36157e22627SCy Schubert static void
countme(u_char * user,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)36257e22627SCy Schubert countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
36357e22627SCy Schubert {
36457e22627SCy Schubert 	int *counterp = (int *)user;
36557e22627SCy Schubert 
36657e22627SCy Schubert 	(*counterp)++;
36757e22627SCy Schubert }
36857e22627SCy Schubert 
36957e22627SCy Schubert static void
usage(void)37057e22627SCy Schubert usage(void)
37157e22627SCy Schubert {
372*6f9cba8fSJoseph Mingrone 	(void)fprintf(stderr, "Usage: %s [ -sptnq ] [ -i interface ] [expression]\n",
37357e22627SCy Schubert 	    program_name);
37457e22627SCy Schubert 	exit(1);
37557e22627SCy Schubert }
37657e22627SCy Schubert 
37757e22627SCy Schubert /* VARARGS */
37857e22627SCy Schubert static void
error(const char * fmt,...)37957e22627SCy Schubert error(const char *fmt, ...)
38057e22627SCy Schubert {
38157e22627SCy Schubert 	va_list ap;
38257e22627SCy Schubert 
38357e22627SCy Schubert 	(void)fprintf(stderr, "%s: ", program_name);
38457e22627SCy Schubert 	va_start(ap, fmt);
38557e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
38657e22627SCy Schubert 	va_end(ap);
38757e22627SCy Schubert 	if (*fmt) {
38857e22627SCy Schubert 		fmt += strlen(fmt);
38957e22627SCy Schubert 		if (fmt[-1] != '\n')
39057e22627SCy Schubert 			(void)fputc('\n', stderr);
39157e22627SCy Schubert 	}
39257e22627SCy Schubert 	exit(1);
39357e22627SCy Schubert 	/* NOTREACHED */
39457e22627SCy Schubert }
39557e22627SCy Schubert 
39657e22627SCy Schubert /* VARARGS */
39757e22627SCy Schubert static void
warning(const char * fmt,...)39857e22627SCy Schubert warning(const char *fmt, ...)
39957e22627SCy Schubert {
40057e22627SCy Schubert 	va_list ap;
40157e22627SCy Schubert 
40257e22627SCy Schubert 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
40357e22627SCy Schubert 	va_start(ap, fmt);
40457e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
40557e22627SCy Schubert 	va_end(ap);
40657e22627SCy Schubert 	if (*fmt) {
40757e22627SCy Schubert 		fmt += strlen(fmt);
40857e22627SCy Schubert 		if (fmt[-1] != '\n')
40957e22627SCy Schubert 			(void)fputc('\n', stderr);
41057e22627SCy Schubert 	}
41157e22627SCy Schubert }
41257e22627SCy Schubert 
41357e22627SCy Schubert /*
41457e22627SCy Schubert  * Copy arg vector into a new buffer, concatenating arguments with spaces.
41557e22627SCy Schubert  */
41657e22627SCy Schubert static char *
copy_argv(register char ** argv)41757e22627SCy Schubert copy_argv(register char **argv)
41857e22627SCy Schubert {
41957e22627SCy Schubert 	register char **p;
420*6f9cba8fSJoseph Mingrone 	register size_t len = 0;
42157e22627SCy Schubert 	char *buf;
42257e22627SCy Schubert 	char *src, *dst;
42357e22627SCy Schubert 
42457e22627SCy Schubert 	p = argv;
42557e22627SCy Schubert 	if (*p == 0)
42657e22627SCy Schubert 		return 0;
42757e22627SCy Schubert 
42857e22627SCy Schubert 	while (*p)
42957e22627SCy Schubert 		len += strlen(*p++) + 1;
43057e22627SCy Schubert 
43157e22627SCy Schubert 	buf = (char *)malloc(len);
43257e22627SCy Schubert 	if (buf == NULL)
43357e22627SCy Schubert 		error("copy_argv: malloc");
43457e22627SCy Schubert 
43557e22627SCy Schubert 	p = argv;
43657e22627SCy Schubert 	dst = buf;
43757e22627SCy Schubert 	while ((src = *p++) != NULL) {
43857e22627SCy Schubert 		while ((*dst++ = *src++) != '\0')
43957e22627SCy Schubert 			;
44057e22627SCy Schubert 		dst[-1] = ' ';
44157e22627SCy Schubert 	}
44257e22627SCy Schubert 	dst[-1] = '\0';
44357e22627SCy Schubert 
44457e22627SCy Schubert 	return buf;
44557e22627SCy Schubert }
446