19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1994
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes * Jan-Simon Pendry.
99b50d902SRodney W. Grimes *
109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes * are met:
139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes * without specific prior written permission.
219b50d902SRodney W. Grimes *
229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes * SUCH DAMAGE.
339b50d902SRodney W. Grimes */
349b50d902SRodney W. Grimes
35aab65414SBrian Somers #include <sys/types.h>
3612794df2SJaakko Heinonen #include <sys/sbuf.h>
379b50d902SRodney W. Grimes #include <sys/wait.h>
389b50d902SRodney W. Grimes
399b50d902SRodney W. Grimes #include <ctype.h>
409b50d902SRodney W. Grimes #include <err.h>
4112794df2SJaakko Heinonen #include <errno.h>
429b50d902SRodney W. Grimes #include <paths.h>
439b50d902SRodney W. Grimes #include <signal.h>
449b50d902SRodney W. Grimes #include <stdio.h>
459b50d902SRodney W. Grimes #include <stdlib.h>
469b50d902SRodney W. Grimes #include <string.h>
479b50d902SRodney W. Grimes #include <unistd.h>
489b50d902SRodney W. Grimes
49f032f7b3SKyle Evans #define ISMAGICNO(p) \
50f032f7b3SKyle Evans (p)[0] == magic && isdigit((unsigned char)(p)[1]) && (p)[1] != '0'
519b50d902SRodney W. Grimes
52befcdd43SXin LI static int exec_shell(const char *, const char *, const char *);
53cdc8ffbbSWill Andrews static void usage(void);
54cdc8ffbbSWill Andrews
559b50d902SRodney W. Grimes int
main(int argc,char * argv[])5612794df2SJaakko Heinonen main(int argc, char *argv[])
5712794df2SJaakko Heinonen {
5812794df2SJaakko Heinonen struct sbuf *cmdbuf;
5912794df2SJaakko Heinonen long arg_max;
60f032f7b3SKyle Evans int ch, debug, i, magic, n, nargs, rval;
6112794df2SJaakko Heinonen size_t cmdsize;
62f032f7b3SKyle Evans char buf[4];
6312794df2SJaakko Heinonen char *cmd, *name, *p, *shell, *slashp, *tmpshell;
649b50d902SRodney W. Grimes
659b50d902SRodney W. Grimes debug = 0;
669b50d902SRodney W. Grimes magic = '%'; /* Default magic char is `%'. */
679b50d902SRodney W. Grimes nargs = -1;
689861e6dfSWill Andrews while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
699b50d902SRodney W. Grimes switch (ch) {
709b50d902SRodney W. Grimes case 'a':
71f032f7b3SKyle Evans if (optarg[0] == '\0' || optarg[1] != '\0')
729b50d902SRodney W. Grimes errx(1,
736ec34d21SPhilippe Charnier "illegal magic character specification");
749b50d902SRodney W. Grimes magic = optarg[0];
759b50d902SRodney W. Grimes break;
769b50d902SRodney W. Grimes case 'd':
779b50d902SRodney W. Grimes debug = 1;
789b50d902SRodney W. Grimes break;
799b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4':
809b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9':
819b50d902SRodney W. Grimes if (nargs != -1)
829b50d902SRodney W. Grimes errx(1,
836ec34d21SPhilippe Charnier "only one -# argument may be specified");
849b50d902SRodney W. Grimes nargs = optopt - '0';
859b50d902SRodney W. Grimes break;
869b50d902SRodney W. Grimes default:
879b50d902SRodney W. Grimes usage();
889b50d902SRodney W. Grimes }
899b50d902SRodney W. Grimes argc -= optind;
909b50d902SRodney W. Grimes argv += optind;
919b50d902SRodney W. Grimes
929b50d902SRodney W. Grimes if (argc < 2)
939b50d902SRodney W. Grimes usage();
949b50d902SRodney W. Grimes
959b50d902SRodney W. Grimes /*
969b50d902SRodney W. Grimes * The command to run is argv[0], and the args are argv[1..].
979b50d902SRodney W. Grimes * Look for %digit references in the command, remembering the
989b50d902SRodney W. Grimes * largest one.
999b50d902SRodney W. Grimes */
1009b50d902SRodney W. Grimes for (n = 0, p = argv[0]; *p != '\0'; ++p)
101f032f7b3SKyle Evans if (ISMAGICNO(p)) {
1029b50d902SRodney W. Grimes ++p;
1039b50d902SRodney W. Grimes if (p[0] - '0' > n)
1049b50d902SRodney W. Grimes n = p[0] - '0';
1059b50d902SRodney W. Grimes }
1069b50d902SRodney W. Grimes
1079b50d902SRodney W. Grimes /*
108f6973480SWill Andrews * Figure out the shell and name arguments to pass to execl()
109f6973480SWill Andrews * in exec_shell(). Always malloc() shell and just set name
110f6973480SWill Andrews * to point at the last part of shell if there are any backslashes,
111f6973480SWill Andrews * otherwise just set it to point at the space malloc()'d. If
112f6973480SWill Andrews * SHELL environment variable exists, replace contents of
113f6973480SWill Andrews * shell with it.
114f6973480SWill Andrews */
115f6973480SWill Andrews shell = name = NULL;
116f6973480SWill Andrews tmpshell = getenv("SHELL");
117f6973480SWill Andrews shell = (tmpshell != NULL) ? strdup(tmpshell) : strdup(_PATH_BSHELL);
118f6973480SWill Andrews if (shell == NULL)
119f6973480SWill Andrews err(1, "strdup() failed");
120f6973480SWill Andrews slashp = strrchr(shell, '/');
121f6973480SWill Andrews name = (slashp != NULL) ? slashp + 1 : shell;
122f6973480SWill Andrews
123f6973480SWill Andrews /*
1249b50d902SRodney W. Grimes * If there were any %digit references, then use those, otherwise
1259b50d902SRodney W. Grimes * build a new command string with sufficient %digit references at
1269b50d902SRodney W. Grimes * the end to consume (nargs) arguments each time round the loop.
127f6973480SWill Andrews * Allocate enough space to hold the maximum command. Save the
128f6973480SWill Andrews * size to pass to snprintf().
1299b50d902SRodney W. Grimes */
130f032f7b3SKyle Evans if (n == 0) {
131f032f7b3SKyle Evans cmdsize = strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1;
132f6973480SWill Andrews if ((cmd = malloc(cmdsize)) == NULL)
1339b50d902SRodney W. Grimes err(1, NULL);
134f032f7b3SKyle Evans strlcpy(cmd, argv[0], cmdsize);
1359b50d902SRodney W. Grimes
1369b50d902SRodney W. Grimes /* If nargs not set, default to a single argument. */
1379b50d902SRodney W. Grimes if (nargs == -1)
1389b50d902SRodney W. Grimes nargs = 1;
1399b50d902SRodney W. Grimes
140f6973480SWill Andrews for (i = 1; i <= nargs; i++) {
141f032f7b3SKyle Evans snprintf(buf, sizeof(buf), " %c%d", magic, i);
142f032f7b3SKyle Evans strlcat(cmd, buf, cmdsize);
143f6973480SWill Andrews }
1449b50d902SRodney W. Grimes
1459b50d902SRodney W. Grimes /*
1469b50d902SRodney W. Grimes * If nargs set to the special value 0, eat a single
1479b50d902SRodney W. Grimes * argument for each command execution.
1489b50d902SRodney W. Grimes */
1499b50d902SRodney W. Grimes if (nargs == 0)
1509b50d902SRodney W. Grimes nargs = 1;
1519b50d902SRodney W. Grimes } else {
152f032f7b3SKyle Evans if ((cmd = strdup(argv[0])) == NULL)
153f032f7b3SKyle Evans err(1, NULL);
1549b50d902SRodney W. Grimes nargs = n;
1559b50d902SRodney W. Grimes }
1569b50d902SRodney W. Grimes
15712794df2SJaakko Heinonen cmdbuf = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
15812794df2SJaakko Heinonen if (cmdbuf == NULL)
1599b50d902SRodney W. Grimes err(1, NULL);
1609b50d902SRodney W. Grimes
16112794df2SJaakko Heinonen arg_max = sysconf(_SC_ARG_MAX);
16212794df2SJaakko Heinonen
1639b50d902SRodney W. Grimes /*
1649b50d902SRodney W. Grimes * (argc) and (argv) are still offset by one to make it simpler to
1659b50d902SRodney W. Grimes * expand %digit references. At the end of the loop check for (argc)
1669b50d902SRodney W. Grimes * equals 1 means that all the (argv) has been consumed.
1679b50d902SRodney W. Grimes */
1689b50d902SRodney W. Grimes for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
16912794df2SJaakko Heinonen sbuf_clear(cmdbuf);
170*441202c0SDag-Erling Smørgrav if (sbuf_cat(cmdbuf, "exec ") != 0)
171*441202c0SDag-Erling Smørgrav err(1, "sbuf");
1729b50d902SRodney W. Grimes /* Expand command argv references. */
17312794df2SJaakko Heinonen for (p = cmd; *p != '\0'; ++p) {
174f032f7b3SKyle Evans if (ISMAGICNO(p)) {
175*441202c0SDag-Erling Smørgrav if (sbuf_cat(cmdbuf, argv[*++p - '0']) != 0)
176*441202c0SDag-Erling Smørgrav err(1, "sbuf");
17712794df2SJaakko Heinonen } else {
178*441202c0SDag-Erling Smørgrav if (sbuf_putc(cmdbuf, *p) != 0)
179*441202c0SDag-Erling Smørgrav err(1, "sbuf");
18012794df2SJaakko Heinonen }
18112794df2SJaakko Heinonen if (sbuf_len(cmdbuf) > arg_max)
18212794df2SJaakko Heinonen errc(1, E2BIG, NULL);
18312794df2SJaakko Heinonen }
1849b50d902SRodney W. Grimes
1859b50d902SRodney W. Grimes /* Terminate the command string. */
186*441202c0SDag-Erling Smørgrav if (sbuf_finish(cmdbuf) != 0)
187*441202c0SDag-Erling Smørgrav err(1, "sbuf");
1889b50d902SRodney W. Grimes
1899b50d902SRodney W. Grimes /* Run the command. */
1909b50d902SRodney W. Grimes if (debug)
19112794df2SJaakko Heinonen (void)printf("%s\n", sbuf_data(cmdbuf));
1929b50d902SRodney W. Grimes else
19312794df2SJaakko Heinonen if (exec_shell(sbuf_data(cmdbuf), shell, name))
1949b50d902SRodney W. Grimes rval = 1;
1959b50d902SRodney W. Grimes }
1969b50d902SRodney W. Grimes
1979b50d902SRodney W. Grimes if (argc != 1)
1989b50d902SRodney W. Grimes errx(1, "expecting additional argument%s after \"%s\"",
1999b50d902SRodney W. Grimes (nargs - argc) ? "s" : "", argv[argc - 1]);
200f6973480SWill Andrews free(cmd);
20112794df2SJaakko Heinonen sbuf_delete(cmdbuf);
202f6973480SWill Andrews free(shell);
2039b50d902SRodney W. Grimes exit(rval);
2049b50d902SRodney W. Grimes }
2059b50d902SRodney W. Grimes
2069b50d902SRodney W. Grimes /*
207f6973480SWill Andrews * exec_shell --
208f6973480SWill Andrews * Execute a shell command using passed use_shell and use_name
209f6973480SWill Andrews * arguments.
2109b50d902SRodney W. Grimes */
211f6973480SWill Andrews static int
exec_shell(const char * command,const char * use_shell,const char * use_name)212befcdd43SXin LI exec_shell(const char *command, const char *use_shell, const char *use_name)
2139b50d902SRodney W. Grimes {
2149b50d902SRodney W. Grimes pid_t pid;
215ec10002dSEivind Eklund int omask, pstat;
2169b50d902SRodney W. Grimes sig_t intsave, quitsave;
2179b50d902SRodney W. Grimes
2189861e6dfSWill Andrews if (!command) /* just checking... */
2199b50d902SRodney W. Grimes return(1);
2209b50d902SRodney W. Grimes
2219b50d902SRodney W. Grimes omask = sigblock(sigmask(SIGCHLD));
2226ad13586SKris Kennaway switch(pid = vfork()) {
2239b50d902SRodney W. Grimes case -1: /* error */
2246ad13586SKris Kennaway err(1, "vfork");
2259b50d902SRodney W. Grimes case 0: /* child */
2269b50d902SRodney W. Grimes (void)sigsetmask(omask);
2277bc6d015SBrian Somers execl(use_shell, use_name, "-c", command, (char *)NULL);
228f6973480SWill Andrews warn("%s", use_shell);
2296ad13586SKris Kennaway _exit(1);
2309b50d902SRodney W. Grimes }
2319b50d902SRodney W. Grimes intsave = signal(SIGINT, SIG_IGN);
2329b50d902SRodney W. Grimes quitsave = signal(SIGQUIT, SIG_IGN);
233ec10002dSEivind Eklund pid = waitpid(pid, &pstat, 0);
2349b50d902SRodney W. Grimes (void)sigsetmask(omask);
2359b50d902SRodney W. Grimes (void)signal(SIGINT, intsave);
2369b50d902SRodney W. Grimes (void)signal(SIGQUIT, quitsave);
237ec10002dSEivind Eklund return(pid == -1 ? -1 : pstat);
2389b50d902SRodney W. Grimes }
2399b50d902SRodney W. Grimes
240befcdd43SXin LI static void
usage(void)241da8de1e2SAlfred Perlstein usage(void)
2429b50d902SRodney W. Grimes {
2439b50d902SRodney W. Grimes
2449b50d902SRodney W. Grimes (void)fprintf(stderr,
2459861e6dfSWill Andrews "usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n");
2469b50d902SRodney W. Grimes exit(1);
2479b50d902SRodney W. Grimes }
248