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