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