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