#if 0
#ifndef lint
static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
#endif
#endif
#include <sys/cdefs.h>
__RCSID("$FreeBSD: src/usr.bin/apply/apply.c,v 1.22 2002/07/14 18:22:12 alfred Exp $");
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define EXEC "exec "
static int exec_shell(const char *, char *, char *);
static void usage(void);
int
main(int argc, char *argv[]) {
int ch, debug, i, magic, n, nargs, offset, rval;
size_t clen, cmdsize, l;
char *c, *cmd, *name, *p, *q, *shell, *slashp, *tmpshell;
debug = 0;
magic = '%';
nargs = -1;
while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
switch (ch) {
case 'a':
if (optarg[1] != '\0')
errx(1,
"illegal magic character specification");
magic = optarg[0];
break;
case 'd':
debug = 1;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (nargs != -1)
errx(1,
"only one -# argument may be specified");
nargs = optopt - '0';
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc < 2)
usage();
for (n = 0, p = argv[0]; *p != '\0'; ++p)
if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
++p;
if (p[0] - '0' > n)
n = p[0] - '0';
}
shell = name = NULL;
tmpshell = getenv("SHELL");
shell = (tmpshell != NULL) ? strdup(tmpshell) : strdup(_PATH_BSHELL);
if (shell == NULL)
err(1, "strdup() failed");
slashp = strrchr(shell, '/');
name = (slashp != NULL) ? slashp + 1 : shell;
cmdsize = sizeof(EXEC) - 1 + strlen(argv[0])
+ 9 * (sizeof(" %1") - 1) + 1;
if ((cmd = malloc(cmdsize)) == NULL)
err(1, NULL);
if (n == 0) {
if (nargs == -1)
nargs = 1;
p = cmd;
offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
if ((size_t)offset >= cmdsize)
err(1, "snprintf() failed");
p += offset;
cmdsize -= offset;
for (i = 1; i <= nargs; i++) {
offset = snprintf(p, cmdsize, " %c%d", magic, i);
if ((size_t)offset >= cmdsize)
err(1, "snprintf() failed");
p += offset;
cmdsize -= offset;
}
if (nargs == 0)
nargs = 1;
} else {
offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]);
if ((size_t)offset >= cmdsize)
err(1, "snprintf() failed");
nargs = n;
}
if ((c = malloc(clen = 1024)) == NULL)
err(1, NULL);
for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
for (l = strlen(cmd), i = 0; i < nargs; i++)
l += strlen(argv[i+1]);
if (l > clen && (c = realloc(c, clen = l)) == NULL)
err(1, NULL);
for (p = cmd, q = c; *p != '\0'; ++p)
if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
offset = snprintf(q, l, "%s",
argv[(++p)[0] - '0']);
if ((size_t)offset >= l)
err(1, "snprintf() failed");
q += offset;
l -= offset;
} else
*q++ = *p;
*q = '\0';
if (debug)
(void)printf("%s\n", c);
else
if (exec_shell(c, shell, name))
rval = 1;
}
if (argc != 1)
errx(1, "expecting additional argument%s after \"%s\"",
(nargs - argc) ? "s" : "", argv[argc - 1]);
free(cmd);
free(c);
free(shell);
exit(rval);
}
static int
exec_shell(const char *command, char *use_shell, char *use_name)
{
pid_t pid;
int omask, pstat;
sig_t intsave, quitsave;
if (!command)
return(1);
omask = sigblock(sigmask(SIGCHLD));
switch(pid = vfork()) {
case -1:
err(1, "vfork");
case 0:
(void)sigsetmask(omask);
execl(use_shell, use_name, "-c", command, (char *)NULL);
warn("%s", use_shell);
_exit(1);
}
intsave = signal(SIGINT, SIG_IGN);
quitsave = signal(SIGQUIT, SIG_IGN);
pid = waitpid(pid, &pstat, 0);
(void)sigsetmask(omask);
(void)signal(SIGINT, intsave);
(void)signal(SIGQUIT, quitsave);
return(pid == -1 ? -1 : pstat);
}
void
usage(void)
{
(void)fprintf(stderr,
"usage: apply [-a magic] [-d] [-0123456789] command arguments ...\n");
exit(1);
}