apply.c revision 67189
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Jan-Simon Pendry.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3854113Skris#if 0
3941568Sarchiestatic const char sccsid[] = "@(#)apply.c	8.4 (Berkeley) 4/4/94";
4054113Skris#endif
4154113Skrisstatic const char rcsid[] =
4254113Skris  "$FreeBSD: head/usr.bin/apply/apply.c 67189 2000-10-16 08:13:59Z brian $";
431590Srgrimes#endif /* not lint */
441590Srgrimes
4567189Sbrian#include <sys/types.h>
4667189Sbrian
471590Srgrimes#include <sys/wait.h>
481590Srgrimes
491590Srgrimes#include <ctype.h>
501590Srgrimes#include <err.h>
511590Srgrimes#include <paths.h>
521590Srgrimes#include <signal.h>
531590Srgrimes#include <stdio.h>
541590Srgrimes#include <stdlib.h>
551590Srgrimes#include <string.h>
561590Srgrimes#include <unistd.h>
571590Srgrimes
581590Srgrimesvoid	usage __P((void));
591590Srgrimesint	system __P((const char *));
601590Srgrimes
611590Srgrimesint
621590Srgrimesmain(argc, argv)
631590Srgrimes	int argc;
641590Srgrimes	char *argv[];
651590Srgrimes{
661590Srgrimes	int ch, clen, debug, i, l, magic, n, nargs, rval;
671590Srgrimes	char *c, *cmd, *p, *q;
681590Srgrimes
691590Srgrimes	debug = 0;
701590Srgrimes	magic = '%';		/* Default magic char is `%'. */
711590Srgrimes	nargs = -1;
7224360Simp	while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
731590Srgrimes		switch (ch) {
741590Srgrimes		case 'a':
751590Srgrimes			if (optarg[1] != '\0')
761590Srgrimes				errx(1,
7754157Scharnier				    "illegal magic character specification");
781590Srgrimes			magic = optarg[0];
791590Srgrimes			break;
801590Srgrimes		case 'd':
811590Srgrimes			debug = 1;
821590Srgrimes			break;
831590Srgrimes		case '0': case '1': case '2': case '3': case '4':
841590Srgrimes		case '5': case '6': case '7': case '8': case '9':
851590Srgrimes			if (nargs != -1)
861590Srgrimes				errx(1,
8754157Scharnier				    "only one -# argument may be specified");
881590Srgrimes			nargs = optopt - '0';
891590Srgrimes			break;
901590Srgrimes		default:
911590Srgrimes			usage();
921590Srgrimes		}
931590Srgrimes	argc -= optind;
941590Srgrimes	argv += optind;
951590Srgrimes
961590Srgrimes	if (argc < 2)
971590Srgrimes		usage();
981590Srgrimes
991590Srgrimes	/*
1001590Srgrimes	 * The command to run is argv[0], and the args are argv[1..].
1011590Srgrimes	 * Look for %digit references in the command, remembering the
1021590Srgrimes	 * largest one.
1031590Srgrimes	 */
1041590Srgrimes	for (n = 0, p = argv[0]; *p != '\0'; ++p)
1051590Srgrimes		if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
1061590Srgrimes			++p;
1071590Srgrimes			if (p[0] - '0' > n)
1081590Srgrimes				n = p[0] - '0';
1091590Srgrimes		}
1101590Srgrimes
1111590Srgrimes	/*
1121590Srgrimes	 * If there were any %digit references, then use those, otherwise
1131590Srgrimes	 * build a new command string with sufficient %digit references at
1141590Srgrimes	 * the end to consume (nargs) arguments each time round the loop.
1151590Srgrimes	 * Allocate enough space to hold the maximum command.
1161590Srgrimes	 */
1171590Srgrimes	if ((cmd = malloc(sizeof("exec ") - 1 +
1181590Srgrimes	    strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL)
1191590Srgrimes		err(1, NULL);
1208874Srgrimes
1211590Srgrimes	if (n == 0) {
1221590Srgrimes		/* If nargs not set, default to a single argument. */
1231590Srgrimes		if (nargs == -1)
1241590Srgrimes			nargs = 1;
1251590Srgrimes
1261590Srgrimes		p = cmd;
1271590Srgrimes		p += sprintf(cmd, "exec %s", argv[0]);
1281590Srgrimes		for (i = 1; i <= nargs; i++)
1291590Srgrimes			p += sprintf(p, " %c%d", magic, i);
1301590Srgrimes
1311590Srgrimes		/*
1321590Srgrimes		 * If nargs set to the special value 0, eat a single
1331590Srgrimes		 * argument for each command execution.
1341590Srgrimes		 */
1351590Srgrimes		if (nargs == 0)
1361590Srgrimes			nargs = 1;
1371590Srgrimes	} else {
1381590Srgrimes		(void)sprintf(cmd, "exec %s", argv[0]);
1391590Srgrimes		nargs = n;
1401590Srgrimes	}
1411590Srgrimes
1421590Srgrimes	/*
1431590Srgrimes	 * Grab some space in which to build the command.  Allocate
1441590Srgrimes	 * as necessary later, but no reason to build it up slowly
1451590Srgrimes	 * for the normal case.
1461590Srgrimes	 */
1471590Srgrimes	if ((c = malloc(clen = 1024)) == NULL)
1481590Srgrimes		err(1, NULL);
1491590Srgrimes
1501590Srgrimes	/*
1511590Srgrimes	 * (argc) and (argv) are still offset by one to make it simpler to
1521590Srgrimes	 * expand %digit references.  At the end of the loop check for (argc)
1531590Srgrimes	 * equals 1 means that all the (argv) has been consumed.
1541590Srgrimes	 */
1551590Srgrimes	for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
1561590Srgrimes		/*
1571590Srgrimes		 * Find a max value for the command length, and ensure
1581590Srgrimes		 * there's enough space to build it.
1591590Srgrimes		 */
1601590Srgrimes		for (l = strlen(cmd), i = 0; i < nargs; i++)
16154113Skris			l += strlen(argv[i+1]);
1621590Srgrimes		if (l > clen && (c = realloc(c, clen = l)) == NULL)
1631590Srgrimes			err(1, NULL);
1641590Srgrimes
1651590Srgrimes		/* Expand command argv references. */
1661590Srgrimes		for (p = cmd, q = c; *p != '\0'; ++p)
1671590Srgrimes			if (p[0] == magic && isdigit(p[1]) && p[1] != '0')
1681590Srgrimes				q += sprintf(q, "%s", argv[(++p)[0] - '0']);
1691590Srgrimes			else
1701590Srgrimes				*q++ = *p;
1711590Srgrimes
1721590Srgrimes		/* Terminate the command string. */
1731590Srgrimes		*q = '\0';
1741590Srgrimes
1751590Srgrimes		/* Run the command. */
1761590Srgrimes		if (debug)
1771590Srgrimes			(void)printf("%s\n", c);
1781590Srgrimes		else
1791590Srgrimes			if (system(c))
1801590Srgrimes				rval = 1;
1811590Srgrimes	}
1821590Srgrimes
1831590Srgrimes	if (argc != 1)
1841590Srgrimes		errx(1, "expecting additional argument%s after \"%s\"",
1851590Srgrimes		    (nargs - argc) ? "s" : "", argv[argc - 1]);
1861590Srgrimes	exit(rval);
1871590Srgrimes}
1881590Srgrimes
1891590Srgrimes/*
1901590Srgrimes * system --
1911590Srgrimes * 	Private version of system(3).  Use the user's SHELL environment
1921590Srgrimes *	variable as the shell to execute.
1931590Srgrimes */
1941590Srgrimesint
1951590Srgrimessystem(command)
1961590Srgrimes	const char *command;
1971590Srgrimes{
1981590Srgrimes	static char *name, *shell;
1991590Srgrimes	pid_t pid;
20043928Seivind	int omask, pstat;
2011590Srgrimes	sig_t intsave, quitsave;
2021590Srgrimes
2031590Srgrimes	if (shell == NULL) {
2041590Srgrimes		if ((shell = getenv("SHELL")) == NULL)
2051590Srgrimes			shell = _PATH_BSHELL;
2061590Srgrimes		if ((name = strrchr(shell, '/')) == NULL)
2071590Srgrimes			name = shell;
2081590Srgrimes		else
2091590Srgrimes			++name;
2101590Srgrimes	}
2111590Srgrimes	if (!command)		/* just checking... */
2121590Srgrimes		return(1);
2131590Srgrimes
2141590Srgrimes	omask = sigblock(sigmask(SIGCHLD));
21560706Skris	switch(pid = vfork()) {
2161590Srgrimes	case -1:			/* error */
21760706Skris		err(1, "vfork");
2181590Srgrimes	case 0:				/* child */
2191590Srgrimes		(void)sigsetmask(omask);
2201590Srgrimes		execl(shell, name, "-c", command, NULL);
22160706Skris		warn("%s", shell);
22260706Skris		_exit(1);
2231590Srgrimes	}
2241590Srgrimes	intsave = signal(SIGINT, SIG_IGN);
2251590Srgrimes	quitsave = signal(SIGQUIT, SIG_IGN);
22643928Seivind	pid = waitpid(pid, &pstat, 0);
2271590Srgrimes	(void)sigsetmask(omask);
2281590Srgrimes	(void)signal(SIGINT, intsave);
2291590Srgrimes	(void)signal(SIGQUIT, quitsave);
23043928Seivind	return(pid == -1 ? -1 : pstat);
2311590Srgrimes}
2321590Srgrimes
2331590Srgrimesvoid
2341590Srgrimesusage()
2351590Srgrimes{
2361590Srgrimes
2371590Srgrimes	(void)fprintf(stderr,
23854157Scharnier	"usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n");
2391590Srgrimes	exit(1);
2401590Srgrimes}
241