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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)apply.c 8.4 (Berkeley) 4/4/94
33 * $FreeBSD: src/usr.bin/apply/apply.c,v 1.8.2.3 2001/08/01 23:28:04 obrien Exp $
34 * $DragonFly: src/usr.bin/apply/apply.c,v 1.5 2005/01/04 05:45:02 cpressey Exp $
37 #include <sys/types.h>
52 static int exec_shell(const char *, char *, char *);
53 static void usage(void);
56 main(int argc, char *argv[])
58 int ch, debug, i, magic, n, nargs, offset, rval;
59 size_t clen, cmdsize, l;
60 char *c, *cmd, *name, *p, *q, *shell, *slashp, *tmpshell;
63 magic = '%'; /* Default magic char is `%'. */
65 while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
68 if (optarg[1] != '\0')
70 "illegal magic character specification");
76 case '0': case '1': case '2': case '3': case '4':
77 case '5': case '6': case '7': case '8': case '9':
80 "only one -# argument may be specified");
93 * The command to run is argv[0], and the args are argv[1..].
94 * Look for %digit references in the command, remembering the
97 for (n = 0, p = argv[0]; *p != '\0'; ++p)
98 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
105 * Figure out the shell and name arguments to pass to execl()
106 * in exec_shell(). Always malloc() shell and just set name
107 * to point at the last part of shell if there are any backslashes,
108 * otherwise just set it to point at the space malloc()'d. If
109 * SHELL environment variable exists, replace contents of
113 tmpshell = getenv("SHELL");
114 shell = (tmpshell != NULL) ? strdup(tmpshell) : strdup(_PATH_BSHELL);
116 err(1, "strdup() failed");
117 slashp = strrchr(shell, '/');
118 name = (slashp != NULL) ? slashp + 1 : shell;
121 * If there were any %digit references, then use those, otherwise
122 * build a new command string with sufficient %digit references at
123 * the end to consume (nargs) arguments each time round the loop.
124 * Allocate enough space to hold the maximum command. Save the
125 * size to pass to snprintf().
127 cmdsize = sizeof(EXEC) - 1 + strlen(argv[0])
128 + 9 * (sizeof(" %1") - 1) + 1;
129 if ((cmd = malloc(cmdsize)) == NULL)
133 /* If nargs not set, default to a single argument. */
138 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
139 if ((size_t)offset >= cmdsize)
140 err(1, "snprintf() failed");
143 for (i = 1; i <= nargs; i++) {
144 offset = snprintf(p, cmdsize, " %c%d", magic, i);
145 if ((size_t)offset >= cmdsize)
146 err(1, "snprintf() failed");
152 * If nargs set to the special value 0, eat a single
153 * argument for each command execution.
158 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
159 if ((size_t)offset >= cmdsize)
160 err(1, "snprintf() failed");
165 * Grab some space in which to build the command. Allocate
166 * as necessary later, but no reason to build it up slowly
167 * for the normal case.
169 if ((c = malloc(clen = 1024)) == NULL)
173 * (argc) and (argv) are still offset by one to make it simpler to
174 * expand %digit references. At the end of the loop check for (argc)
175 * equals 1 means that all the (argv) has been consumed.
177 for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
179 * Find a max value for the command length, and ensure
180 * there's enough space to build it.
182 for (l = strlen(cmd), i = 0; i < nargs; i++)
183 l += strlen(argv[i+1]);
184 if (l > clen && (c = realloc(c, clen = l)) == NULL)
187 /* Expand command argv references. */
188 for (p = cmd, q = c; *p != '\0'; ++p) {
189 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
190 offset = snprintf(q, l, "%s",
191 argv[(++p)[0] - '0']);
192 if ((size_t)offset >= l)
193 err(1, "snprintf() failed");
201 /* Terminate the command string. */
204 /* Run the command. */
208 if (exec_shell(c, shell, name))
214 errx(1, "expecting additional argument%s after \"%s\"",
215 (nargs - argc) ? "s" : "", argv[argc - 1]);
224 * Execute a shell command using passed use_shell and use_name
228 exec_shell(const char *command, char *use_shell, char *use_name)
232 sig_t intsave, quitsave;
234 if (command == NULL) /* just checking... */
237 omask = sigblock(sigmask(SIGCHLD));
238 switch(pid = vfork()) {
242 (void)sigsetmask(omask);
243 execl(use_shell, use_name, "-c", command, NULL);
244 warn("%s", use_shell);
247 intsave = signal(SIGINT, SIG_IGN);
248 quitsave = signal(SIGQUIT, SIG_IGN);
249 pid = waitpid(pid, &pstat, 0);
251 signal(SIGINT, intsave);
252 signal(SIGQUIT, quitsave);
253 return(pid == -1 ? -1 : pstat);
260 "usage: apply [-a magic] [-d] [-0123456789] "
261 "command arguments ...\n");