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