2014-01-03 11:52:47 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-01-04 13:51:13 +00:00
|
|
|
#include <errno.h>
|
2014-01-03 11:52:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum {
|
2014-01-16 10:56:13 +00:00
|
|
|
NARGS = 10000
|
2014-01-03 11:52:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int inputc(void);
|
|
|
|
static void deinputc(int);
|
2014-01-07 14:56:05 +00:00
|
|
|
static void fillargbuf(int);
|
2014-01-06 18:12:06 +00:00
|
|
|
static int eatspace(void);
|
2014-01-03 11:52:47 +00:00
|
|
|
static int parsequote(int);
|
2014-01-06 18:20:26 +00:00
|
|
|
static int parseescape(void);
|
2014-01-03 11:52:47 +00:00
|
|
|
static char *poparg(void);
|
2014-01-07 14:50:59 +00:00
|
|
|
static void waitchld(void);
|
2014-01-08 20:27:27 +00:00
|
|
|
static void spawn(void);
|
2014-01-03 11:52:47 +00:00
|
|
|
|
2014-01-16 11:51:19 +00:00
|
|
|
static char *cmd[NARGS];
|
2014-01-03 11:52:47 +00:00
|
|
|
static char *argb;
|
2014-01-08 20:29:34 +00:00
|
|
|
static size_t argbsz;
|
2014-01-03 11:52:47 +00:00
|
|
|
static size_t argbpos;
|
2014-04-09 15:35:29 +00:00
|
|
|
static long maxargs = 0;
|
2014-01-06 18:51:06 +00:00
|
|
|
static int nerrors = 0;
|
2014-01-07 11:53:55 +00:00
|
|
|
static char *eofstr;
|
2014-04-09 15:35:29 +00:00
|
|
|
static int rflag = 0, nflag = 0;
|
2014-01-03 11:52:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-04-09 17:20:24 +00:00
|
|
|
eprintf("usage: %s [-n maxargs] [-r] [-E eofstr] [cmd [arg...]]\n", argv0);
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-05-12 11:01:47 +00:00
|
|
|
int leftover = 0;
|
2014-01-03 11:52:47 +00:00
|
|
|
long argsz, argmaxsz;
|
2014-05-12 11:01:47 +00:00
|
|
|
char *arg = "";
|
2014-04-09 15:35:29 +00:00
|
|
|
int i, a;
|
2014-01-03 11:52:47 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2014-04-09 15:35:29 +00:00
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
if((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0)
|
|
|
|
eprintf("%s: value for -n option should be >= 1\n", argv0);
|
|
|
|
break;
|
2014-01-03 11:52:47 +00:00
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2014-01-07 11:53:55 +00:00
|
|
|
case 'E':
|
|
|
|
eofstr = EARGF(usage());
|
|
|
|
break;
|
2014-01-03 11:52:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
argmaxsz = sysconf(_SC_ARG_MAX);
|
|
|
|
if (argmaxsz < 0)
|
|
|
|
eprintf("sysconf:");
|
|
|
|
/* Leave some room for environment variables */
|
|
|
|
argmaxsz -= 4 * 1024;
|
|
|
|
|
|
|
|
do {
|
2014-04-09 15:35:29 +00:00
|
|
|
argsz = 0; i = 0; a = 0;
|
2014-01-03 11:52:47 +00:00
|
|
|
if (argc > 0) {
|
|
|
|
for (; i < argc; i++) {
|
|
|
|
cmd[i] = strdup(argv[i]);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd[i] = strdup("/bin/echo");
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
|
|
|
}
|
2014-01-08 20:47:21 +00:00
|
|
|
while (leftover == 1 || (arg = poparg())) {
|
2014-01-03 11:52:47 +00:00
|
|
|
if (argsz + strlen(arg) + 1 > argmaxsz ||
|
|
|
|
i >= NARGS - 1) {
|
2014-01-08 20:20:04 +00:00
|
|
|
if (strlen(arg) + 1 > argmaxsz)
|
2014-01-30 13:54:16 +00:00
|
|
|
eprintf("insufficient argument space\n");
|
2014-01-08 20:47:21 +00:00
|
|
|
leftover = 1;
|
2014-01-03 11:52:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd[i] = strdup(arg);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
2014-04-09 15:35:29 +00:00
|
|
|
a++;
|
2014-01-08 20:47:21 +00:00
|
|
|
leftover = 0;
|
2014-04-09 15:35:29 +00:00
|
|
|
if(nflag == 1 && a >= maxargs)
|
|
|
|
break;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
cmd[i] = NULL;
|
2014-04-09 15:35:29 +00:00
|
|
|
if(a >= maxargs && nflag == 1)
|
|
|
|
spawn();
|
|
|
|
else if(!a || (i == 1 && rflag == 1))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
spawn();
|
2014-01-03 11:52:47 +00:00
|
|
|
for (; i >= 0; i--)
|
|
|
|
free(cmd[i]);
|
|
|
|
} while (arg);
|
|
|
|
|
|
|
|
free(argb);
|
2014-01-06 18:51:06 +00:00
|
|
|
|
|
|
|
return nerrors > 0 ? 123 : 0;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
inputc(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
ch = getc(stdin);
|
|
|
|
if (ch == EOF && ferror(stdin))
|
|
|
|
eprintf("stdin: read error:");
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deinputc(int ch)
|
|
|
|
{
|
|
|
|
ungetc(ch, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-07 14:56:05 +00:00
|
|
|
fillargbuf(int ch)
|
2014-01-03 11:52:47 +00:00
|
|
|
{
|
|
|
|
if (argbpos >= argbsz) {
|
2014-01-08 20:29:34 +00:00
|
|
|
argbsz = argbpos == 0 ? 1 : argbsz * 2;
|
2014-01-03 11:52:47 +00:00
|
|
|
argb = realloc(argb, argbsz);
|
|
|
|
if (!argb)
|
|
|
|
eprintf("realloc:");
|
|
|
|
}
|
|
|
|
argb[argbpos] = ch;
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:12:06 +00:00
|
|
|
static int
|
2014-01-03 11:52:47 +00:00
|
|
|
eatspace(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
deinputc(ch);
|
2014-01-06 18:12:06 +00:00
|
|
|
return ch;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 18:12:06 +00:00
|
|
|
return -1;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parsequote(int q)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
2014-01-08 20:22:10 +00:00
|
|
|
if (ch == q)
|
2014-01-03 11:52:47 +00:00
|
|
|
return 0;
|
|
|
|
if (ch != '\n') {
|
2014-01-07 14:56:05 +00:00
|
|
|
fillargbuf(ch);
|
2014-01-03 11:52:47 +00:00
|
|
|
argbpos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:20:26 +00:00
|
|
|
static int
|
2014-01-03 11:52:47 +00:00
|
|
|
parseescape(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if ((ch = inputc()) != EOF) {
|
2014-01-07 14:56:05 +00:00
|
|
|
fillargbuf(ch);
|
2014-01-03 11:52:47 +00:00
|
|
|
argbpos++;
|
2014-01-06 18:20:26 +00:00
|
|
|
return ch;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
2014-01-06 18:20:26 +00:00
|
|
|
return -1;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
poparg(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
argbpos = 0;
|
2014-01-06 18:12:06 +00:00
|
|
|
if (eatspace() == -1)
|
|
|
|
return NULL;
|
2014-01-03 11:52:47 +00:00
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
2014-01-07 11:53:55 +00:00
|
|
|
goto out;
|
2014-01-03 11:52:47 +00:00
|
|
|
case '\'':
|
|
|
|
if (parsequote('\'') == -1)
|
2014-01-30 13:54:16 +00:00
|
|
|
eprintf("unterminated single quote\n");
|
2014-01-03 11:52:47 +00:00
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
if (parsequote('\"') == -1)
|
2014-01-30 13:54:16 +00:00
|
|
|
eprintf("unterminated double quote\n");
|
2014-01-03 11:52:47 +00:00
|
|
|
break;
|
|
|
|
case '\\':
|
2014-01-06 18:20:26 +00:00
|
|
|
if (parseescape() == -1)
|
2014-01-30 13:54:16 +00:00
|
|
|
eprintf("backslash at EOF\n");
|
2014-01-03 11:52:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-07 14:56:05 +00:00
|
|
|
fillargbuf(ch);
|
2014-01-03 11:52:47 +00:00
|
|
|
argbpos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 11:53:55 +00:00
|
|
|
out:
|
2014-01-08 20:25:07 +00:00
|
|
|
fillargbuf('\0');
|
|
|
|
if (eofstr && strcmp(argb, eofstr) == 0)
|
|
|
|
return NULL;
|
|
|
|
return argb;
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 13:51:13 +00:00
|
|
|
static void
|
2014-01-07 14:50:59 +00:00
|
|
|
waitchld(void)
|
2014-01-03 11:52:47 +00:00
|
|
|
{
|
2014-01-07 14:50:59 +00:00
|
|
|
int status;
|
2014-01-03 11:52:47 +00:00
|
|
|
|
|
|
|
wait(&status);
|
2014-01-04 14:01:22 +00:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status) == 255)
|
|
|
|
exit(124);
|
|
|
|
if (WEXITSTATUS(status) == 127 ||
|
|
|
|
WEXITSTATUS(status) == 126)
|
|
|
|
exit(WEXITSTATUS(status));
|
2014-01-06 18:51:06 +00:00
|
|
|
if (status != 0)
|
|
|
|
nerrors++;
|
2014-01-04 14:01:22 +00:00
|
|
|
}
|
2014-01-06 18:03:34 +00:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
exit(125);
|
2014-01-03 11:52:47 +00:00
|
|
|
}
|
2014-01-07 14:50:59 +00:00
|
|
|
|
|
|
|
static void
|
2014-01-08 20:27:27 +00:00
|
|
|
spawn(void)
|
2014-01-07 14:50:59 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
2014-01-27 15:16:43 +00:00
|
|
|
int savederrno;
|
2014-01-07 14:50:59 +00:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
eprintf("fork:");
|
|
|
|
if (pid == 0) {
|
|
|
|
execvp(*cmd, cmd);
|
2014-01-27 15:16:43 +00:00
|
|
|
savederrno = errno;
|
2014-01-07 14:50:59 +00:00
|
|
|
weprintf("execvp %s:", *cmd);
|
2014-01-27 15:16:43 +00:00
|
|
|
_exit(savederrno == ENOENT ? 127 : 126);
|
2014-01-07 14:50:59 +00:00
|
|
|
}
|
|
|
|
waitchld();
|
|
|
|
}
|