1 /*- 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Jan-Simon Pendry. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 36 * @(#)apply.c 8.4 (Berkeley) 4/4/94 37 * $FreeBSD: src/usr.bin/apply/apply.c,v 1.8.2.3 2001/08/01 23:28:04 obrien Exp $ 38 * $DragonFly: src/usr.bin/apply/apply.c,v 1.5 2005/01/04 05:45:02 cpressey Exp $ 39 */ 40 41 #include <sys/types.h> 42 43 #include <sys/wait.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <paths.h> 48 #include <signal.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #define EXEC "exec " 55 56 static int exec_shell(const char *, char *, char *); 57 static void usage(void); 58 59 int 60 main(int argc, char *argv[]) 61 { 62 int ch, debug, i, magic, n, nargs, offset, rval; 63 size_t clen, cmdsize, l; 64 char *c, *cmd, *name, *p, *q, *shell, *slashp, *tmpshell; 65 66 debug = 0; 67 magic = '%'; /* Default magic char is `%'. */ 68 nargs = -1; 69 while ((ch = getopt(argc, argv, "a:d0123456789")) != -1) 70 switch (ch) { 71 case 'a': 72 if (optarg[1] != '\0') 73 errx(1, 74 "illegal magic character specification"); 75 magic = optarg[0]; 76 break; 77 case 'd': 78 debug = 1; 79 break; 80 case '0': case '1': case '2': case '3': case '4': 81 case '5': case '6': case '7': case '8': case '9': 82 if (nargs != -1) 83 errx(1, 84 "only one -# argument may be specified"); 85 nargs = optopt - '0'; 86 break; 87 default: 88 usage(); 89 } 90 argc -= optind; 91 argv += optind; 92 93 if (argc < 2) 94 usage(); 95 96 /* 97 * The command to run is argv[0], and the args are argv[1..]. 98 * Look for %digit references in the command, remembering the 99 * largest one. 100 */ 101 for (n = 0, p = argv[0]; *p != '\0'; ++p) 102 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') { 103 ++p; 104 if (p[0] - '0' > n) 105 n = p[0] - '0'; 106 } 107 108 /* 109 * Figure out the shell and name arguments to pass to execl() 110 * in exec_shell(). Always malloc() shell and just set name 111 * to point at the last part of shell if there are any backslashes, 112 * otherwise just set it to point at the space malloc()'d. If 113 * SHELL environment variable exists, replace contents of 114 * shell with it. 115 */ 116 shell = name = NULL; 117 tmpshell = getenv("SHELL"); 118 shell = (tmpshell != NULL) ? strdup(tmpshell) : strdup(_PATH_BSHELL); 119 if (shell == NULL) 120 err(1, "strdup() failed"); 121 slashp = strrchr(shell, '/'); 122 name = (slashp != NULL) ? slashp + 1 : shell; 123 124 /* 125 * If there were any %digit references, then use those, otherwise 126 * build a new command string with sufficient %digit references at 127 * the end to consume (nargs) arguments each time round the loop. 128 * Allocate enough space to hold the maximum command. Save the 129 * size to pass to snprintf(). 130 */ 131 cmdsize = sizeof(EXEC) - 1 + strlen(argv[0]) 132 + 9 * (sizeof(" %1") - 1) + 1; 133 if ((cmd = malloc(cmdsize)) == NULL) 134 err(1, NULL); 135 136 if (n == 0) { 137 /* If nargs not set, default to a single argument. */ 138 if (nargs == -1) 139 nargs = 1; 140 141 p = cmd; 142 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]); 143 if ((size_t)offset >= cmdsize) 144 err(1, "snprintf() failed"); 145 p += offset; 146 cmdsize -= offset; 147 for (i = 1; i <= nargs; i++) { 148 offset = snprintf(p, cmdsize, " %c%d", magic, i); 149 if ((size_t)offset >= cmdsize) 150 err(1, "snprintf() failed"); 151 p += offset; 152 cmdsize -= offset; 153 } 154 155 /* 156 * If nargs set to the special value 0, eat a single 157 * argument for each command execution. 158 */ 159 if (nargs == 0) 160 nargs = 1; 161 } else { 162 offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]); 163 if ((size_t)offset >= cmdsize) 164 err(1, "snprintf() failed"); 165 nargs = n; 166 } 167 168 /* 169 * Grab some space in which to build the command. Allocate 170 * as necessary later, but no reason to build it up slowly 171 * for the normal case. 172 */ 173 if ((c = malloc(clen = 1024)) == NULL) 174 err(1, NULL); 175 176 /* 177 * (argc) and (argv) are still offset by one to make it simpler to 178 * expand %digit references. At the end of the loop check for (argc) 179 * equals 1 means that all the (argv) has been consumed. 180 */ 181 for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) { 182 /* 183 * Find a max value for the command length, and ensure 184 * there's enough space to build it. 185 */ 186 for (l = strlen(cmd), i = 0; i < nargs; i++) 187 l += strlen(argv[i+1]); 188 if (l > clen && (c = realloc(c, clen = l)) == NULL) 189 err(1, NULL); 190 191 /* Expand command argv references. */ 192 for (p = cmd, q = c; *p != '\0'; ++p) { 193 if (p[0] == magic && isdigit(p[1]) && p[1] != '0') { 194 offset = snprintf(q, l, "%s", 195 argv[(++p)[0] - '0']); 196 if ((size_t)offset >= l) 197 err(1, "snprintf() failed"); 198 q += offset; 199 l -= offset; 200 } else { 201 *q++ = *p; 202 } 203 } 204 205 /* Terminate the command string. */ 206 *q = '\0'; 207 208 /* Run the command. */ 209 if (debug) { 210 printf("%s\n", c); 211 } else { 212 if (exec_shell(c, shell, name)) 213 rval = 1; 214 } 215 } 216 217 if (argc != 1) 218 errx(1, "expecting additional argument%s after \"%s\"", 219 (nargs - argc) ? "s" : "", argv[argc - 1]); 220 free(cmd); 221 free(c); 222 free(shell); 223 exit(rval); 224 } 225 226 /* 227 * exec_shell -- 228 * Execute a shell command using passed use_shell and use_name 229 * arguments. 230 */ 231 static int 232 exec_shell(const char *command, char *use_shell, char *use_name) 233 { 234 pid_t pid; 235 int omask, pstat; 236 sig_t intsave, quitsave; 237 238 if (command == NULL) /* just checking... */ 239 return(1); 240 241 omask = sigblock(sigmask(SIGCHLD)); 242 switch(pid = vfork()) { 243 case -1: /* error */ 244 err(1, "vfork"); 245 case 0: /* child */ 246 (void)sigsetmask(omask); 247 execl(use_shell, use_name, "-c", command, NULL); 248 warn("%s", use_shell); 249 _exit(1); 250 } 251 intsave = signal(SIGINT, SIG_IGN); 252 quitsave = signal(SIGQUIT, SIG_IGN); 253 pid = waitpid(pid, &pstat, 0); 254 sigsetmask(omask); 255 signal(SIGINT, intsave); 256 signal(SIGQUIT, quitsave); 257 return(pid == -1 ? -1 : pstat); 258 } 259 260 void 261 usage(void) 262 { 263 fprintf(stderr, 264 "usage: apply [-a magic] [-d] [-0123456789] " 265 "command arguments ...\n"); 266 exit(1); 267 } 268