3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
41 static const char rcsid[] =
56 void usage __P((void));
57 int system __P((const char *));
64 int ch, clen, debug, i, l, magic, n, nargs, rval;
65 char *c, *cmd, *p, *q;
68 magic = '%'; /* Default magic char is `%'. */
70 while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
73 if (optarg[1] != '\0')
75 "illegal magic character specification");
81 case '0': case '1': case '2': case '3': case '4':
82 case '5': case '6': case '7': case '8': case '9':
85 "only one -# argument may be specified");
98 * The command to run is argv[0], and the args are argv[1..].
99 * Look for %digit references in the command, remembering the
102 for (n = 0, p = argv[0]; *p != '\0'; ++p)
103 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
110 * If there were any %digit references, then use those, otherwise
111 * build a new command string with sufficient %digit references at
112 * the end to consume (nargs) arguments each time round the loop.
113 * Allocate enough space to hold the maximum command.
115 if ((cmd = malloc(sizeof("exec ") - 1 +
116 strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL)
120 /* If nargs not set, default to a single argument. */
125 p += sprintf(cmd, "exec %s", argv[0]);
126 for (i = 1; i <= nargs; i++)
127 p += sprintf(p, " %c%d", magic, i);
130 * If nargs set to the special value 0, eat a single
131 * argument for each command execution.
136 (void)sprintf(cmd, "exec %s", argv[0]);
141 * Grab some space in which to build the command. Allocate
142 * as necessary later, but no reason to build it up slowly
143 * for the normal case.
145 if ((c = malloc(clen = 1024)) == NULL)
149 * (argc) and (argv) are still offset by one to make it simpler to
150 * expand %digit references. At the end of the loop check for (argc)
151 * equals 1 means that all the (argv) has been consumed.
153 for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
155 * Find a max value for the command length, and ensure
156 * there's enough space to build it.
158 for (l = strlen(cmd), i = 0; i < nargs; i++)
159 l += strlen(argv[i+1]);
160 if (l > clen && (c = realloc(c, clen = l)) == NULL)
163 /* Expand command argv references. */
164 for (p = cmd, q = c; *p != '\0'; ++p)
165 if (p[0] == magic && isdigit(p[1]) && p[1] != '0')
166 q += sprintf(q, "%s", argv[(++p)[0] - '0']);
170 /* Terminate the command string. */
173 /* Run the command. */
175 (void)printf("%s\n", c);
182 errx(1, "expecting additional argument%s after \"%s\"",
183 (nargs - argc) ? "s" : "", argv[argc - 1]);
189 * Private version of system(3). Use the user's SHELL environment
190 * variable as the shell to execute.
196 static char *name, *shell;
199 sig_t intsave, quitsave;
202 if ((shell = getenv("SHELL")) == NULL)
203 shell = _PATH_BSHELL;
204 if ((name = strrchr(shell, '/')) == NULL)
209 if (!command) /* just checking... */
212 omask = sigblock(sigmask(SIGCHLD));
213 switch(pid = fork()) {
217 (void)sigsetmask(omask);
218 execl(shell, name, "-c", command, NULL);
221 intsave = signal(SIGINT, SIG_IGN);
222 quitsave = signal(SIGQUIT, SIG_IGN);
223 pid = waitpid(pid, &pstat, 0);
224 (void)sigsetmask(omask);
225 (void)signal(SIGINT, intsave);
226 (void)signal(SIGQUIT, quitsave);
227 return(pid == -1 ? -1 : pstat);
234 (void)fprintf(stderr,
235 "usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n");