apply.c revision 1591
156893Sfenner/*-
256893Sfenner * Copyright (c) 1994
356893Sfenner *	The Regents of the University of California.  All rights reserved.
456893Sfenner *
556893Sfenner * This code is derived from software contributed to Berkeley by
656893Sfenner * Jan-Simon Pendry.
756893Sfenner *
856893Sfenner * Redistribution and use in source and binary forms, with or without
956893Sfenner * modification, are permitted provided that the following conditions
1056893Sfenner * are met:
1156893Sfenner * 1. Redistributions of source code must retain the above copyright
1256893Sfenner *    notice, this list of conditions and the following disclaimer.
1356893Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1456893Sfenner *    notice, this list of conditions and the following disclaimer in the
1556893Sfenner *    documentation and/or other materials provided with the distribution.
1656893Sfenner * 3. All advertising materials mentioning features or use of this software
1756893Sfenner *    must display the following acknowledgement:
1856893Sfenner *	This product includes software developed by the University of
1956893Sfenner *	California, Berkeley and its contributors.
2056893Sfenner * 4. Neither the name of the University nor the names of its contributors
2156893Sfenner *    may be used to endorse or promote products derived from this software
2256893Sfenner *    without specific prior written permission.
23127668Sbms *
24190207Srpaulo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2556893Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2656893Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2756893Sfenner * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2856893Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2956893Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3056893Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3156893Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3256893Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33127668Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3456893Sfenner * SUCH DAMAGE.
3556893Sfenner */
3656893Sfenner
3756893Sfenner#ifndef lint
3856893Sfennerstatic char sccsid[] = "@(#)apply.c	8.4 (Berkeley) 4/4/94";
39127668Sbms#endif /* not lint */
4056893Sfenner
41146773Ssam#include <sys/wait.h>
42146773Ssam
43146773Ssam#include <ctype.h>
44146773Ssam#include <err.h>
45146773Ssam#include <paths.h>
46146773Ssam#include <signal.h>
47146773Ssam#include <stdio.h>
48146773Ssam#include <stdlib.h>
4956893Sfenner#include <string.h>
5056893Sfenner#include <unistd.h>
5156893Sfenner
5256893Sfennervoid	usage __P((void));
5356893Sfennerint	system __P((const char *));
5456893Sfenner
55127668Sbmsint
5656893Sfennermain(argc, argv)
5756893Sfenner	int argc;
5856893Sfenner	char *argv[];
5956893Sfenner{
6056893Sfenner	int ch, clen, debug, i, l, magic, n, nargs, rval;
6156893Sfenner	char *c, *cmd, *p, *q;
6298524Sfenner
6356893Sfenner	debug = 0;
6456893Sfenner	magic = '%';		/* Default magic char is `%'. */
6556893Sfenner	nargs = -1;
66127668Sbms	while ((ch = getopt(argc, argv, "a:d0123456789")) != EOF)
67127668Sbms		switch (ch) {
6856893Sfenner		case 'a':
6956893Sfenner			if (optarg[1] != '\0')
7056893Sfenner				errx(1,
71127668Sbms				    "illegal magic character specification.");
7256893Sfenner			magic = optarg[0];
73127668Sbms			break;
74127668Sbms		case 'd':
75127668Sbms			debug = 1;
76127668Sbms			break;
77127668Sbms		case '0': case '1': case '2': case '3': case '4':
7856893Sfenner		case '5': case '6': case '7': case '8': case '9':
7956893Sfenner			if (nargs != -1)
8056893Sfenner				errx(1,
8156893Sfenner				    "only one -# argument may be specified.");
8256893Sfenner			nargs = optopt - '0';
8356893Sfenner			break;
8456893Sfenner		default:
8556893Sfenner			usage();
8656893Sfenner		}
8756893Sfenner	argc -= optind;
8856893Sfenner	argv += optind;
8956893Sfenner
9098524Sfenner	if (argc < 2)
9156893Sfenner		usage();
9256893Sfenner
9356893Sfenner	/*
94127668Sbms	 * The command to run is argv[0], and the args are argv[1..].
95127668Sbms	 * Look for %digit references in the command, remembering the
9698524Sfenner	 * largest one.
9756893Sfenner	 */
9856893Sfenner	for (n = 0, p = argv[0]; *p != '\0'; ++p)
9956893Sfenner		if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
10056893Sfenner			++p;
10156893Sfenner			if (p[0] - '0' > n)
10256893Sfenner				n = p[0] - '0';
10356893Sfenner		}
10456893Sfenner
10556893Sfenner	/*
10698524Sfenner	 * If there were any %digit references, then use those, otherwise
10756893Sfenner	 * build a new command string with sufficient %digit references at
10856893Sfenner	 * the end to consume (nargs) arguments each time round the loop.
10956893Sfenner	 * Allocate enough space to hold the maximum command.
110127668Sbms	 */
111127668Sbms	if ((cmd = malloc(sizeof("exec ") - 1 +
11298524Sfenner	    strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL)
11356893Sfenner		err(1, NULL);
11456893Sfenner
11556893Sfenner	if (n == 0) {
11656893Sfenner		/* If nargs not set, default to a single argument. */
11756893Sfenner		if (nargs == -1)
11856893Sfenner			nargs = 1;
11998524Sfenner
12056893Sfenner		p = cmd;
12156893Sfenner		p += sprintf(cmd, "exec %s", argv[0]);
12298524Sfenner		for (i = 1; i <= nargs; i++)
12356893Sfenner			p += sprintf(p, " %c%d", magic, i);
12456893Sfenner
12556893Sfenner		/*
12656893Sfenner		 * If nargs set to the special value 0, eat a single
12756893Sfenner		 * argument for each command execution.
12856893Sfenner		 */
129		if (nargs == 0)
130			nargs = 1;
131	} else {
132		(void)sprintf(cmd, "exec %s", argv[0]);
133		nargs = n;
134	}
135
136	/*
137	 * Grab some space in which to build the command.  Allocate
138	 * as necessary later, but no reason to build it up slowly
139	 * for the normal case.
140	 */
141	if ((c = malloc(clen = 1024)) == NULL)
142		err(1, NULL);
143
144	/*
145	 * (argc) and (argv) are still offset by one to make it simpler to
146	 * expand %digit references.  At the end of the loop check for (argc)
147	 * equals 1 means that all the (argv) has been consumed.
148	 */
149	for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
150		/*
151		 * Find a max value for the command length, and ensure
152		 * there's enough space to build it.
153		 */
154		for (l = strlen(cmd), i = 0; i < nargs; i++)
155			l += strlen(argv[i]);
156		if (l > clen && (c = realloc(c, clen = l)) == NULL)
157			err(1, NULL);
158
159		/* Expand command argv references. */
160		for (p = cmd, q = c; *p != '\0'; ++p)
161			if (p[0] == magic && isdigit(p[1]) && p[1] != '0')
162				q += sprintf(q, "%s", argv[(++p)[0] - '0']);
163			else
164				*q++ = *p;
165
166		/* Terminate the command string. */
167		*q = '\0';
168
169		/* Run the command. */
170		if (debug)
171			(void)printf("%s\n", c);
172		else
173			if (system(c))
174				rval = 1;
175	}
176
177	if (argc != 1)
178		errx(1, "expecting additional argument%s after \"%s\"",
179		    (nargs - argc) ? "s" : "", argv[argc - 1]);
180	exit(rval);
181}
182
183/*
184 * system --
185 * 	Private version of system(3).  Use the user's SHELL environment
186 *	variable as the shell to execute.
187 */
188int
189system(command)
190	const char *command;
191{
192	static char *name, *shell;
193	union wait pstat;
194	pid_t pid;
195	int omask;
196	sig_t intsave, quitsave;
197
198	if (shell == NULL) {
199		if ((shell = getenv("SHELL")) == NULL)
200			shell = _PATH_BSHELL;
201		if ((name = strrchr(shell, '/')) == NULL)
202			name = shell;
203		else
204			++name;
205	}
206	if (!command)		/* just checking... */
207		return(1);
208
209	omask = sigblock(sigmask(SIGCHLD));
210	switch(pid = vfork()) {
211	case -1:			/* error */
212		err(1, "fork");
213	case 0:				/* child */
214		(void)sigsetmask(omask);
215		execl(shell, name, "-c", command, NULL);
216		err(1, "%s", shell);
217	}
218	intsave = signal(SIGINT, SIG_IGN);
219	quitsave = signal(SIGQUIT, SIG_IGN);
220	pid = waitpid(pid, (int *)&pstat, 0);
221	(void)sigsetmask(omask);
222	(void)signal(SIGINT, intsave);
223	(void)signal(SIGQUIT, quitsave);
224	return(pid == -1 ? -1 : pstat.w_status);
225}
226
227void
228usage()
229{
230
231	(void)fprintf(stderr,
232	    "usage: apply [-a magic] [-0123456789] command arguments ...\n");
233	exit(1);
234}
235