1 /* $NetBSD: apply.c,v 1.16 2005/05/08 19:53:57 matt Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
40 __RCSID("$NetBSD: apply.c,v 1.16 2005/05/08 19:53:57 matt Exp $");
55 static void usage(void) __dead
;
56 static int shell_system(const char *);
59 main(int argc
, char *argv
[])
62 int ch
, debug
, i
, magic
, n
, nargs
, rval
;
63 char *c
, *cmd
, *p
, *q
, *nc
;
65 (void)setprogname(argv
[0]); /* for portability */
68 magic
= '%'; /* Default magic char is `%'. */
70 while ((ch
= getopt(argc
, argv
, "a:d0123456789")) != -1) {
73 if (optarg
[1] != '\0')
75 "illegal magic character specification.");
81 case '0': case '1': case '2': case '3': case '4':
82 case '5': case '6': case '7': case '8': case '9':
85 "only one -# argument may be specified.");
99 * The command to run is argv[0], and the args are argv[1..].
100 * Look for %digit references in the command, remembering the
103 for (n
= 0, p
= argv
[0]; *p
!= '\0'; ++p
) {
104 if (p
[0] == magic
&& isdigit((unsigned char)p
[1]) &&
112 * If there were any %digit references, then use those, otherwise
113 * build a new command string with sufficient %digit references at
114 * the end to consume (nargs) arguments each time round the loop.
115 * Allocate enough space to hold the maximum command.
117 if ((cmd
= malloc(sizeof("exec ") - 1 +
118 strlen(argv
[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL
)
119 err(EXIT_FAILURE
, "malloc");
122 /* If nargs not set, default to a single argument. */
127 p
+= sprintf(cmd
, "exec %s", argv
[0]);
128 for (i
= 1; i
<= nargs
; i
++)
129 p
+= sprintf(p
, " %c%d", magic
, i
);
132 * If nargs set to the special value 0, eat a single
133 * argument for each command execution.
138 (void)sprintf(cmd
, "exec %s", argv
[0]);
143 * Grab some space in which to build the command. Allocate
144 * as necessary later, but no reason to build it up slowly
145 * for the normal case.
147 if ((c
= malloc(clen
= 1024)) == NULL
)
148 err(EXIT_FAILURE
, "malloc");
151 * (argc) and (argv) are still offset by one to make it simpler to
152 * expand %digit references. At the end of the loop check for (argc)
153 * equals 1 means that all the (argv) has been consumed.
155 for (rval
= 0; argc
> nargs
; argc
-= nargs
, argv
+= nargs
) {
157 * Find a max value for the command length, and ensure
158 * there's enough space to build it.
160 for (l
= strlen(cmd
), i
= 0; i
< nargs
; i
++)
161 l
+= strlen(argv
[i
+1]);
165 err(EXIT_FAILURE
, "malloc");
170 /* Expand command argv references. */
171 for (p
= cmd
, q
= c
; *p
!= '\0'; ++p
) {
172 if (p
[0] == magic
&& isdigit((unsigned char)p
[1]) &&
174 q
+= sprintf(q
, "%s", argv
[(++p
)[0] - '0']);
179 /* Terminate the command string. */
182 /* Run the command. */
184 (void)printf("%s\n", c
);
185 else if (shell_system(c
))
191 "expecting additional argument%s after \"%s\"",
192 (nargs
- argc
) ? "s" : "", argv
[argc
- 1]);
198 * Private version of system(3). Use the user's SHELL environment
199 * variable as the shell to execute.
202 shell_system(const char *command
)
204 static const char *name
, *shell
;
208 sig_t intsave
, quitsave
;
211 if ((shell
= getenv("SHELL")) == NULL
)
212 shell
= _PATH_BSHELL
;
213 if ((name
= strrchr(shell
, '/')) == NULL
)
218 if (!command
) /* just checking... */
221 omask
= sigblock(sigmask(SIGCHLD
));
222 switch (pid
= vfork()) {
224 err(EXIT_FAILURE
, "vfork");
227 (void)sigsetmask(omask
);
228 (void)execl(shell
, name
, "-c", command
, NULL
);
232 default: /* parent */
233 intsave
= signal(SIGINT
, SIG_IGN
);
234 quitsave
= signal(SIGQUIT
, SIG_IGN
);
235 pid
= waitpid(pid
, &status
, 0);
236 (void)sigsetmask(omask
);
237 (void)signal(SIGINT
, intsave
);
238 (void)signal(SIGQUIT
, quitsave
);
239 return pid
== -1 ? -1 : status
;
249 (void)fprintf(stderr
,
250 "usage: %s [-a magic] [-0123456789] command arguments ...\n",