19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1994 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Jan-Simon Pendry. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 379b50d902SRodney W. Grimes #ifndef lint 38b26a5ea6SKris Kennaway #if 0 39fa146c53SArchie Cobbs static const char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94"; 40b26a5ea6SKris Kennaway #endif 41b26a5ea6SKris Kennaway static const char rcsid[] = 42b26a5ea6SKris Kennaway "$FreeBSD$"; 439b50d902SRodney W. Grimes #endif /* not lint */ 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/wait.h> 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <ctype.h> 489b50d902SRodney W. Grimes #include <err.h> 499b50d902SRodney W. Grimes #include <paths.h> 509b50d902SRodney W. Grimes #include <signal.h> 519b50d902SRodney W. Grimes #include <stdio.h> 529b50d902SRodney W. Grimes #include <stdlib.h> 539b50d902SRodney W. Grimes #include <string.h> 549b50d902SRodney W. Grimes #include <unistd.h> 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes void usage __P((void)); 579b50d902SRodney W. Grimes int system __P((const char *)); 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes int 609b50d902SRodney W. Grimes main(argc, argv) 619b50d902SRodney W. Grimes int argc; 629b50d902SRodney W. Grimes char *argv[]; 639b50d902SRodney W. Grimes { 649b50d902SRodney W. Grimes int ch, clen, debug, i, l, magic, n, nargs, rval; 659b50d902SRodney W. Grimes char *c, *cmd, *p, *q; 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes debug = 0; 689b50d902SRodney W. Grimes magic = '%'; /* Default magic char is `%'. */ 699b50d902SRodney W. Grimes nargs = -1; 701c8af878SWarner Losh while ((ch = getopt(argc, argv, "a:d0123456789")) != -1) 719b50d902SRodney W. Grimes switch (ch) { 729b50d902SRodney W. Grimes case 'a': 739b50d902SRodney W. Grimes if (optarg[1] != '\0') 749b50d902SRodney W. Grimes errx(1, 756ec34d21SPhilippe Charnier "illegal magic character specification"); 769b50d902SRodney W. Grimes magic = optarg[0]; 779b50d902SRodney W. Grimes break; 789b50d902SRodney W. Grimes case 'd': 799b50d902SRodney W. Grimes debug = 1; 809b50d902SRodney W. Grimes break; 819b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 829b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 839b50d902SRodney W. Grimes if (nargs != -1) 849b50d902SRodney W. Grimes errx(1, 856ec34d21SPhilippe Charnier "only one -# argument may be specified"); 869b50d902SRodney W. Grimes nargs = optopt - '0'; 879b50d902SRodney W. Grimes break; 889b50d902SRodney W. Grimes default: 899b50d902SRodney W. Grimes usage(); 909b50d902SRodney W. Grimes } 919b50d902SRodney W. Grimes argc -= optind; 929b50d902SRodney W. Grimes argv += optind; 939b50d902SRodney W. Grimes 949b50d902SRodney W. Grimes if (argc < 2) 959b50d902SRodney W. Grimes usage(); 969b50d902SRodney W. Grimes 979b50d902SRodney W. Grimes /* 989b50d902SRodney W. Grimes * The command to run is argv[0], and the args are argv[1..]. 999b50d902SRodney W. Grimes * Look for %digit references in the command, remembering the 1009b50d902SRodney W. Grimes * largest one. 1019b50d902SRodney W. Grimes */ 1029b50d902SRodney W. Grimes for (n = 0, p = argv[0]; *p != '\0'; ++p) 1039b50d902SRodney W. Grimes if (p[0] == magic && isdigit(p[1]) && p[1] != '0') { 1049b50d902SRodney W. Grimes ++p; 1059b50d902SRodney W. Grimes if (p[0] - '0' > n) 1069b50d902SRodney W. Grimes n = p[0] - '0'; 1079b50d902SRodney W. Grimes } 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes /* 1109b50d902SRodney W. Grimes * If there were any %digit references, then use those, otherwise 1119b50d902SRodney W. Grimes * build a new command string with sufficient %digit references at 1129b50d902SRodney W. Grimes * the end to consume (nargs) arguments each time round the loop. 1139b50d902SRodney W. Grimes * Allocate enough space to hold the maximum command. 1149b50d902SRodney W. Grimes */ 1159b50d902SRodney W. Grimes if ((cmd = malloc(sizeof("exec ") - 1 + 1169b50d902SRodney W. Grimes strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL) 1179b50d902SRodney W. Grimes err(1, NULL); 1189b50d902SRodney W. Grimes 1199b50d902SRodney W. Grimes if (n == 0) { 1209b50d902SRodney W. Grimes /* If nargs not set, default to a single argument. */ 1219b50d902SRodney W. Grimes if (nargs == -1) 1229b50d902SRodney W. Grimes nargs = 1; 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes p = cmd; 1259b50d902SRodney W. Grimes p += sprintf(cmd, "exec %s", argv[0]); 1269b50d902SRodney W. Grimes for (i = 1; i <= nargs; i++) 1279b50d902SRodney W. Grimes p += sprintf(p, " %c%d", magic, i); 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes /* 1309b50d902SRodney W. Grimes * If nargs set to the special value 0, eat a single 1319b50d902SRodney W. Grimes * argument for each command execution. 1329b50d902SRodney W. Grimes */ 1339b50d902SRodney W. Grimes if (nargs == 0) 1349b50d902SRodney W. Grimes nargs = 1; 1359b50d902SRodney W. Grimes } else { 1369b50d902SRodney W. Grimes (void)sprintf(cmd, "exec %s", argv[0]); 1379b50d902SRodney W. Grimes nargs = n; 1389b50d902SRodney W. Grimes } 1399b50d902SRodney W. Grimes 1409b50d902SRodney W. Grimes /* 1419b50d902SRodney W. Grimes * Grab some space in which to build the command. Allocate 1429b50d902SRodney W. Grimes * as necessary later, but no reason to build it up slowly 1439b50d902SRodney W. Grimes * for the normal case. 1449b50d902SRodney W. Grimes */ 1459b50d902SRodney W. Grimes if ((c = malloc(clen = 1024)) == NULL) 1469b50d902SRodney W. Grimes err(1, NULL); 1479b50d902SRodney W. Grimes 1489b50d902SRodney W. Grimes /* 1499b50d902SRodney W. Grimes * (argc) and (argv) are still offset by one to make it simpler to 1509b50d902SRodney W. Grimes * expand %digit references. At the end of the loop check for (argc) 1519b50d902SRodney W. Grimes * equals 1 means that all the (argv) has been consumed. 1529b50d902SRodney W. Grimes */ 1539b50d902SRodney W. Grimes for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) { 1549b50d902SRodney W. Grimes /* 1559b50d902SRodney W. Grimes * Find a max value for the command length, and ensure 1569b50d902SRodney W. Grimes * there's enough space to build it. 1579b50d902SRodney W. Grimes */ 1589b50d902SRodney W. Grimes for (l = strlen(cmd), i = 0; i < nargs; i++) 159b26a5ea6SKris Kennaway l += strlen(argv[i+1]); 1609b50d902SRodney W. Grimes if (l > clen && (c = realloc(c, clen = l)) == NULL) 1619b50d902SRodney W. Grimes err(1, NULL); 1629b50d902SRodney W. Grimes 1639b50d902SRodney W. Grimes /* Expand command argv references. */ 1649b50d902SRodney W. Grimes for (p = cmd, q = c; *p != '\0'; ++p) 1659b50d902SRodney W. Grimes if (p[0] == magic && isdigit(p[1]) && p[1] != '0') 1669b50d902SRodney W. Grimes q += sprintf(q, "%s", argv[(++p)[0] - '0']); 1679b50d902SRodney W. Grimes else 1689b50d902SRodney W. Grimes *q++ = *p; 1699b50d902SRodney W. Grimes 1709b50d902SRodney W. Grimes /* Terminate the command string. */ 1719b50d902SRodney W. Grimes *q = '\0'; 1729b50d902SRodney W. Grimes 1739b50d902SRodney W. Grimes /* Run the command. */ 1749b50d902SRodney W. Grimes if (debug) 1759b50d902SRodney W. Grimes (void)printf("%s\n", c); 1769b50d902SRodney W. Grimes else 1779b50d902SRodney W. Grimes if (system(c)) 1789b50d902SRodney W. Grimes rval = 1; 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes if (argc != 1) 1829b50d902SRodney W. Grimes errx(1, "expecting additional argument%s after \"%s\"", 1839b50d902SRodney W. Grimes (nargs - argc) ? "s" : "", argv[argc - 1]); 1849b50d902SRodney W. Grimes exit(rval); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes 1879b50d902SRodney W. Grimes /* 1889b50d902SRodney W. Grimes * system -- 1899b50d902SRodney W. Grimes * Private version of system(3). Use the user's SHELL environment 1909b50d902SRodney W. Grimes * variable as the shell to execute. 1919b50d902SRodney W. Grimes */ 1929b50d902SRodney W. Grimes int 1939b50d902SRodney W. Grimes system(command) 1949b50d902SRodney W. Grimes const char *command; 1959b50d902SRodney W. Grimes { 1969b50d902SRodney W. Grimes static char *name, *shell; 1979b50d902SRodney W. Grimes pid_t pid; 198ec10002dSEivind Eklund int omask, pstat; 1999b50d902SRodney W. Grimes sig_t intsave, quitsave; 2009b50d902SRodney W. Grimes 2019b50d902SRodney W. Grimes if (shell == NULL) { 2029b50d902SRodney W. Grimes if ((shell = getenv("SHELL")) == NULL) 2039b50d902SRodney W. Grimes shell = _PATH_BSHELL; 2049b50d902SRodney W. Grimes if ((name = strrchr(shell, '/')) == NULL) 2059b50d902SRodney W. Grimes name = shell; 2069b50d902SRodney W. Grimes else 2079b50d902SRodney W. Grimes ++name; 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes if (!command) /* just checking... */ 2109b50d902SRodney W. Grimes return(1); 2119b50d902SRodney W. Grimes 2129b50d902SRodney W. Grimes omask = sigblock(sigmask(SIGCHLD)); 2131fd98d7dSDag-Erling Smørgrav switch(pid = fork()) { 2149b50d902SRodney W. Grimes case -1: /* error */ 2159b50d902SRodney W. Grimes err(1, "fork"); 2169b50d902SRodney W. Grimes case 0: /* child */ 2179b50d902SRodney W. Grimes (void)sigsetmask(omask); 2189b50d902SRodney W. Grimes execl(shell, name, "-c", command, NULL); 2199b50d902SRodney W. Grimes err(1, "%s", shell); 2209b50d902SRodney W. Grimes } 2219b50d902SRodney W. Grimes intsave = signal(SIGINT, SIG_IGN); 2229b50d902SRodney W. Grimes quitsave = signal(SIGQUIT, SIG_IGN); 223ec10002dSEivind Eklund pid = waitpid(pid, &pstat, 0); 2249b50d902SRodney W. Grimes (void)sigsetmask(omask); 2259b50d902SRodney W. Grimes (void)signal(SIGINT, intsave); 2269b50d902SRodney W. Grimes (void)signal(SIGQUIT, quitsave); 227ec10002dSEivind Eklund return(pid == -1 ? -1 : pstat); 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes 2309b50d902SRodney W. Grimes void 2319b50d902SRodney W. Grimes usage() 2329b50d902SRodney W. Grimes { 2339b50d902SRodney W. Grimes 2349b50d902SRodney W. Grimes (void)fprintf(stderr, 2356ec34d21SPhilippe Charnier "usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n"); 2369b50d902SRodney W. Grimes exit(1); 2379b50d902SRodney W. Grimes } 238