apply.c revision 43928
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
3841568Sarchiestatic const char sccsid[] = "@(#)apply.c	8.4 (Berkeley) 4/4/94";
391590Srgrimes#endif /* not lint */
401590Srgrimes
411590Srgrimes#include <sys/wait.h>
421590Srgrimes
431590Srgrimes#include <ctype.h>
441590Srgrimes#include <err.h>
451590Srgrimes#include <paths.h>
461590Srgrimes#include <signal.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <stdlib.h>
491590Srgrimes#include <string.h>
501590Srgrimes#include <unistd.h>
511590Srgrimes
521590Srgrimesvoid	usage __P((void));
531590Srgrimesint	system __P((const char *));
541590Srgrimes
551590Srgrimesint
561590Srgrimesmain(argc, argv)
571590Srgrimes	int argc;
581590Srgrimes	char *argv[];
591590Srgrimes{
601590Srgrimes	int ch, clen, debug, i, l, magic, n, nargs, rval;
611590Srgrimes	char *c, *cmd, *p, *q;
621590Srgrimes
631590Srgrimes	debug = 0;
641590Srgrimes	magic = '%';		/* Default magic char is `%'. */
651590Srgrimes	nargs = -1;
6624360Simp	while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
671590Srgrimes		switch (ch) {
681590Srgrimes		case 'a':
691590Srgrimes			if (optarg[1] != '\0')
701590Srgrimes				errx(1,
711590Srgrimes				    "illegal magic character specification.");
721590Srgrimes			magic = optarg[0];
731590Srgrimes			break;
741590Srgrimes		case 'd':
751590Srgrimes			debug = 1;
761590Srgrimes			break;
771590Srgrimes		case '0': case '1': case '2': case '3': case '4':
781590Srgrimes		case '5': case '6': case '7': case '8': case '9':
791590Srgrimes			if (nargs != -1)
801590Srgrimes				errx(1,
811590Srgrimes				    "only one -# argument may be specified.");
821590Srgrimes			nargs = optopt - '0';
831590Srgrimes			break;
841590Srgrimes		default:
851590Srgrimes			usage();
861590Srgrimes		}
871590Srgrimes	argc -= optind;
881590Srgrimes	argv += optind;
891590Srgrimes
901590Srgrimes	if (argc < 2)
911590Srgrimes		usage();
921590Srgrimes
931590Srgrimes	/*
941590Srgrimes	 * The command to run is argv[0], and the args are argv[1..].
951590Srgrimes	 * Look for %digit references in the command, remembering the
961590Srgrimes	 * largest one.
971590Srgrimes	 */
981590Srgrimes	for (n = 0, p = argv[0]; *p != '\0'; ++p)
991590Srgrimes		if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
1001590Srgrimes			++p;
1011590Srgrimes			if (p[0] - '0' > n)
1021590Srgrimes				n = p[0] - '0';
1031590Srgrimes		}
1041590Srgrimes
1051590Srgrimes	/*
1061590Srgrimes	 * If there were any %digit references, then use those, otherwise
1071590Srgrimes	 * build a new command string with sufficient %digit references at
1081590Srgrimes	 * the end to consume (nargs) arguments each time round the loop.
1091590Srgrimes	 * Allocate enough space to hold the maximum command.
1101590Srgrimes	 */
1111590Srgrimes	if ((cmd = malloc(sizeof("exec ") - 1 +
1121590Srgrimes	    strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL)
1131590Srgrimes		err(1, NULL);
1148874Srgrimes
1151590Srgrimes	if (n == 0) {
1161590Srgrimes		/* If nargs not set, default to a single argument. */
1171590Srgrimes		if (nargs == -1)
1181590Srgrimes			nargs = 1;
1191590Srgrimes
1201590Srgrimes		p = cmd;
1211590Srgrimes		p += sprintf(cmd, "exec %s", argv[0]);
1221590Srgrimes		for (i = 1; i <= nargs; i++)
1231590Srgrimes			p += sprintf(p, " %c%d", magic, i);
1241590Srgrimes
1251590Srgrimes		/*
1261590Srgrimes		 * If nargs set to the special value 0, eat a single
1271590Srgrimes		 * argument for each command execution.
1281590Srgrimes		 */
1291590Srgrimes		if (nargs == 0)
1301590Srgrimes			nargs = 1;
1311590Srgrimes	} else {
1321590Srgrimes		(void)sprintf(cmd, "exec %s", argv[0]);
1331590Srgrimes		nargs = n;
1341590Srgrimes	}
1351590Srgrimes
1361590Srgrimes	/*
1371590Srgrimes	 * Grab some space in which to build the command.  Allocate
1381590Srgrimes	 * as necessary later, but no reason to build it up slowly
1391590Srgrimes	 * for the normal case.
1401590Srgrimes	 */
1411590Srgrimes	if ((c = malloc(clen = 1024)) == NULL)
1421590Srgrimes		err(1, NULL);
1431590Srgrimes
1441590Srgrimes	/*
1451590Srgrimes	 * (argc) and (argv) are still offset by one to make it simpler to
1461590Srgrimes	 * expand %digit references.  At the end of the loop check for (argc)
1471590Srgrimes	 * equals 1 means that all the (argv) has been consumed.
1481590Srgrimes	 */
1491590Srgrimes	for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
1501590Srgrimes		/*
1511590Srgrimes		 * Find a max value for the command length, and ensure
1521590Srgrimes		 * there's enough space to build it.
1531590Srgrimes		 */
1541590Srgrimes		for (l = strlen(cmd), i = 0; i < nargs; i++)
1551590Srgrimes			l += strlen(argv[i]);
1561590Srgrimes		if (l > clen && (c = realloc(c, clen = l)) == NULL)
1571590Srgrimes			err(1, NULL);
1581590Srgrimes
1591590Srgrimes		/* Expand command argv references. */
1601590Srgrimes		for (p = cmd, q = c; *p != '\0'; ++p)
1611590Srgrimes			if (p[0] == magic && isdigit(p[1]) && p[1] != '0')
1621590Srgrimes				q += sprintf(q, "%s", argv[(++p)[0] - '0']);
1631590Srgrimes			else
1641590Srgrimes				*q++ = *p;
1651590Srgrimes
1661590Srgrimes		/* Terminate the command string. */
1671590Srgrimes		*q = '\0';
1681590Srgrimes
1691590Srgrimes		/* Run the command. */
1701590Srgrimes		if (debug)
1711590Srgrimes			(void)printf("%s\n", c);
1721590Srgrimes		else
1731590Srgrimes			if (system(c))
1741590Srgrimes				rval = 1;
1751590Srgrimes	}
1761590Srgrimes
1771590Srgrimes	if (argc != 1)
1781590Srgrimes		errx(1, "expecting additional argument%s after \"%s\"",
1791590Srgrimes		    (nargs - argc) ? "s" : "", argv[argc - 1]);
1801590Srgrimes	exit(rval);
1811590Srgrimes}
1821590Srgrimes
1831590Srgrimes/*
1841590Srgrimes * system --
1851590Srgrimes * 	Private version of system(3).  Use the user's SHELL environment
1861590Srgrimes *	variable as the shell to execute.
1871590Srgrimes */
1881590Srgrimesint
1891590Srgrimessystem(command)
1901590Srgrimes	const char *command;
1911590Srgrimes{
1921590Srgrimes	static char *name, *shell;
1931590Srgrimes	pid_t pid;
19443928Seivind	int omask, pstat;
1951590Srgrimes	sig_t intsave, quitsave;
1961590Srgrimes
1971590Srgrimes	if (shell == NULL) {
1981590Srgrimes		if ((shell = getenv("SHELL")) == NULL)
1991590Srgrimes			shell = _PATH_BSHELL;
2001590Srgrimes		if ((name = strrchr(shell, '/')) == NULL)
2011590Srgrimes			name = shell;
2021590Srgrimes		else
2031590Srgrimes			++name;
2041590Srgrimes	}
2051590Srgrimes	if (!command)		/* just checking... */
2061590Srgrimes		return(1);
2071590Srgrimes
2081590Srgrimes	omask = sigblock(sigmask(SIGCHLD));
20940301Sdes	switch(pid = fork()) {
2101590Srgrimes	case -1:			/* error */
2111590Srgrimes		err(1, "fork");
2121590Srgrimes	case 0:				/* child */
2131590Srgrimes		(void)sigsetmask(omask);
2141590Srgrimes		execl(shell, name, "-c", command, NULL);
2151590Srgrimes		err(1, "%s", shell);
2161590Srgrimes	}
2171590Srgrimes	intsave = signal(SIGINT, SIG_IGN);
2181590Srgrimes	quitsave = signal(SIGQUIT, SIG_IGN);
21943928Seivind	pid = waitpid(pid, &pstat, 0);
2201590Srgrimes	(void)sigsetmask(omask);
2211590Srgrimes	(void)signal(SIGINT, intsave);
2221590Srgrimes	(void)signal(SIGQUIT, quitsave);
22343928Seivind	return(pid == -1 ? -1 : pstat);
2241590Srgrimes}
2251590Srgrimes
2261590Srgrimesvoid
2271590Srgrimesusage()
2281590Srgrimes{
2291590Srgrimes
2301590Srgrimes	(void)fprintf(stderr,
2311590Srgrimes	    "usage: apply [-a magic] [-0123456789] command arguments ...\n");
2321590Srgrimes	exit(1);
2331590Srgrimes}
234