1/* $NetBSD: apply.c,v 1.19 2016/03/12 22:28:04 dholland 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 38static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94"; 39#else 40__RCSID("$NetBSD: apply.c,v 1.19 2016/03/12 22:28:04 dholland 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 55static __dead void usage(void); 56static int shell_system(const char *); 57 58int 59main(int argc, char *argv[]) 60{ 61 size_t clen, l; 62 int ch, debug, i, magic, n, nargs, rval; 63 char *c, *cmd, *p, *q, *nc; 64 65 (void)setprogname(argv[0]); /* for portability */ 66 67 /* Option defaults */ 68 debug = 0; 69 magic = '%'; 70 nargs = -1; 71 72 while ((ch = getopt(argc, argv, "a:d0123456789")) != -1) { 73 switch (ch) { 74 case 'a': 75 if (optarg[1] != '\0') 76 errx(EXIT_FAILURE, 77 "Illegal magic character specification."); 78 magic = optarg[0]; 79 break; 80 case 'd': 81 debug = 1; 82 break; 83 case '0': case '1': case '2': case '3': case '4': 84 case '5': case '6': case '7': case '8': case '9': 85 if (nargs != -1) 86 errx(EXIT_FAILURE, 87 "Only one -# argument may be specified."); 88 nargs = optopt - '0'; 89 break; 90 default: 91 usage(); 92 } 93 } 94 argc -= optind; 95 argv += optind; 96 97 if (argc < 2) 98 usage(); 99 100 /* 101 * The command to run is now argv[0], and the args are argv[1+]. 102 * Look for %digit references in the command, remembering the 103 * largest one. 104 */ 105 n = 0; 106 for (p = argv[0]; p[0] != '\0'; ++p) { 107 if (p[0] == magic && p[1] != '\0' && 108 isdigit((unsigned char)p[1]) && p[1] != '0') { 109 ++p; 110 if (p[0] - '0' > n) 111 n = p[0] - '0'; 112 } 113 } 114 115 /* 116 * If there were any %digit references, then use those, otherwise 117 * build a new command string with sufficient %digit references at 118 * the end to consume (nargs) arguments each time round the loop. 119 * Allocate enough space to hold the maximum command. 120 */ 121 if ((cmd = malloc(sizeof("exec ") - 1 + 122 strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL) 123 err(EXIT_FAILURE, "malloc"); 124 125 if (n == 0) { 126 /* If nargs not set, default to a single argument. */ 127 if (nargs == -1) 128 nargs = 1; 129 130 p = cmd; 131 p += sprintf(cmd, "exec %s", argv[0]); 132 for (i = 1; i <= nargs; i++) 133 p += sprintf(p, " %c%d", magic, i); 134 135 /* 136 * If nargs set to the special value 0, eat a single 137 * argument for each command execution. 138 */ 139 if (nargs == 0) 140 nargs = 1; 141 } else { 142 (void)sprintf(cmd, "exec %s", argv[0]); 143 nargs = n; 144 } 145 146 /* 147 * Grab some space in which to build the command. Allocate 148 * as necessary later, but no reason to build it up slowly 149 * for the normal case. 150 */ 151 if ((c = malloc(clen = 1024)) == NULL) 152 err(EXIT_FAILURE, "malloc"); 153 154 /* 155 * (argc) and (argv) are still offset by one to make it simpler to 156 * expand %digit references. At the end of the loop check for (argc) 157 * equals 1 means that all the (argv) has been consumed. 158 */ 159 for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) { 160 /* 161 * Find a max value for the command length, and ensure 162 * there's enough space to build it. 163 */ 164 for (l = strlen(cmd), i = 0; i < nargs; i++) 165 l += strlen(argv[i+1]); 166 if (l > clen) { 167 nc = realloc(c, l); 168 if (nc == NULL) 169 err(EXIT_FAILURE, "malloc"); 170 c = nc; 171 clen = l; 172 } 173 174 /* Expand command argv references. */ 175 for (p = cmd, q = c; *p != '\0'; ++p) { 176 if (p[0] == magic && isdigit((unsigned char)p[1]) && 177 p[1] != '0') 178 q += sprintf(q, "%s", argv[(++p)[0] - '0']); 179 else 180 *q++ = *p; 181 } 182 183 /* Terminate the command string. */ 184 *q = '\0'; 185 186 /* Run the command. */ 187 if (debug) 188 (void)printf("%s\n", c); 189 else if (shell_system(c)) 190 rval = 1; 191 } 192 193 if (argc != 1) 194 errx(EXIT_FAILURE, 195 "Expecting additional argument%s after \"%s\"", 196 (nargs - argc) ? "s" : "", argv[argc - 1]); 197 return rval; 198} 199 200/* 201 * shell_system -- 202 * Private version of system(3). Use the user's SHELL environment 203 * variable as the shell to execute. 204 */ 205static int 206shell_system(const char *command) 207{ 208 static const char *name, *shell; 209 int status; 210 int omask; 211 pid_t pid; 212 sig_t intsave, quitsave; 213 214 if (shell == NULL) { 215 if ((shell = getenv("SHELL")) == NULL) 216 shell = _PATH_BSHELL; 217 if ((name = strrchr(shell, '/')) == NULL) 218 name = shell; 219 else 220 ++name; 221 } 222 223 if (!command) { 224 /* just checking... */ 225 return(1); 226 } 227 228 omask = sigblock(sigmask(SIGCHLD)); 229 switch (pid = vfork()) { 230 case -1: 231 /* error */ 232 err(EXIT_FAILURE, "vfork"); 233 /*NOTREACHED*/ 234 case 0: 235 /* child */ 236 (void)sigsetmask(omask); 237 (void)execl(shell, name, "-c", command, (char *)NULL); 238 warn("%s", shell); 239 _exit(1); 240 /*NOTREACHED*/ 241 default: 242 /* parent */ 243 intsave = signal(SIGINT, SIG_IGN); 244 quitsave = signal(SIGQUIT, SIG_IGN); 245 pid = waitpid(pid, &status, 0); 246 (void)sigsetmask(omask); 247 (void)signal(SIGINT, intsave); 248 (void)signal(SIGQUIT, quitsave); 249 return pid == -1 ? -1 : status; 250 } 251 /*NOTREACHED*/ 252} 253 254static __dead void 255usage(void) 256{ 257 258 (void)fprintf(stderr, 259 "usage: %s [-a magic] [-0123456789] command arguments ...\n", 260 getprogname()); 261 exit(1); 262} 263