2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
60 static int exec_shell(const char *, const char *, const char *);
61 static void usage(void);
64 main(int argc, char *argv[])
68 int ch, debug, i, magic, n, nargs, offset, rval;
70 char *cmd, *name, *p, *shell, *slashp, *tmpshell;
73 magic = '%'; /* Default magic char is `%'. */
75 while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
78 if (optarg[1] != '\0')
80 "illegal magic character specification");
86 case '0': case '1': case '2': case '3': case '4':
87 case '5': case '6': case '7': case '8': case '9':
90 "only one -# argument may be specified");
103 * The command to run is argv[0], and the args are argv[1..].
104 * Look for %digit references in the command, remembering the
107 for (n = 0, p = argv[0]; *p != '\0'; ++p)
108 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
115 * Figure out the shell and name arguments to pass to execl()
116 * in exec_shell(). Always malloc() shell and just set name
117 * to point at the last part of shell if there are any backslashes,
118 * otherwise just set it to point at the space malloc()'d. If
119 * SHELL environment variable exists, replace contents of
123 tmpshell = getenv("SHELL");
124 shell = (tmpshell != NULL) ? strdup(tmpshell) : strdup(_PATH_BSHELL);
126 err(1, "strdup() failed");
127 slashp = strrchr(shell, '/');
128 name = (slashp != NULL) ? slashp + 1 : shell;
131 * If there were any %digit references, then use those, otherwise
132 * build a new command string with sufficient %digit references at
133 * the end to consume (nargs) arguments each time round the loop.
134 * Allocate enough space to hold the maximum command. Save the
135 * size to pass to snprintf().
137 cmdsize = sizeof(EXEC) - 1 + strlen(argv[0])
138 + 9 * (sizeof(" %1") - 1) + 1;
139 if ((cmd = malloc(cmdsize)) == NULL)
143 /* If nargs not set, default to a single argument. */
148 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
149 if ((size_t)offset >= cmdsize)
150 errx(1, "snprintf() failed");
153 for (i = 1; i <= nargs; i++) {
154 offset = snprintf(p, cmdsize, " %c%d", magic, i);
155 if ((size_t)offset >= cmdsize)
156 errx(1, "snprintf() failed");
162 * If nargs set to the special value 0, eat a single
163 * argument for each command execution.
168 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
169 if ((size_t)offset >= cmdsize)
170 errx(1, "snprintf() failed");
174 cmdbuf = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
178 arg_max = sysconf(_SC_ARG_MAX);
181 * (argc) and (argv) are still offset by one to make it simpler to
182 * expand %digit references. At the end of the loop check for (argc)
183 * equals 1 means that all the (argv) has been consumed.
185 for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
187 /* Expand command argv references. */
188 for (p = cmd; *p != '\0'; ++p) {
189 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
190 if (sbuf_cat(cmdbuf, argv[(++p)[0] - '0'])
192 errc(1, ENOMEM, "sbuf");
194 if (sbuf_putc(cmdbuf, *p) == -1)
195 errc(1, ENOMEM, "sbuf");
197 if (sbuf_len(cmdbuf) > arg_max)
198 errc(1, E2BIG, NULL);
201 /* Terminate the command string. */
204 /* Run the command. */
206 (void)printf("%s\n", sbuf_data(cmdbuf));
208 if (exec_shell(sbuf_data(cmdbuf), shell, name))
213 errx(1, "expecting additional argument%s after \"%s\"",
214 (nargs - argc) ? "s" : "", argv[argc - 1]);
223 * Execute a shell command using passed use_shell and use_name
227 exec_shell(const char *command, const char *use_shell, const char *use_name)
231 sig_t intsave, quitsave;
233 if (!command) /* just checking... */
236 omask = sigblock(sigmask(SIGCHLD));
237 switch(pid = vfork()) {
241 (void)sigsetmask(omask);
242 execl(use_shell, use_name, "-c", command, (char *)NULL);
243 warn("%s", use_shell);
246 intsave = signal(SIGINT, SIG_IGN);
247 quitsave = signal(SIGQUIT, SIG_IGN);
248 pid = waitpid(pid, &pstat, 0);
249 (void)sigsetmask(omask);
250 (void)signal(SIGINT, intsave);
251 (void)signal(SIGQUIT, quitsave);
252 return(pid == -1 ? -1 : pstat);
259 (void)fprintf(stderr,
260 "usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n");