2014-01-03 06:52:47 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2014-01-04 08:51:13 -05:00
|
|
|
#include <errno.h>
|
2015-03-22 17:53:12 -04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
2014-01-03 06:52:47 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2014-01-03 06:52:47 -05:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-22 17:53:12 -04:00
|
|
|
#define NARGS 10000
|
2014-01-03 06:52:47 -05:00
|
|
|
|
|
|
|
static int inputc(void);
|
2014-01-07 09:56:05 -05:00
|
|
|
static void fillargbuf(int);
|
2014-01-06 13:12:06 -05:00
|
|
|
static int eatspace(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
static int parsequote(int);
|
2014-01-06 13:20:26 -05:00
|
|
|
static int parseescape(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
static char *poparg(void);
|
2014-01-07 09:50:59 -05:00
|
|
|
static void waitchld(void);
|
2014-01-08 15:27:27 -05:00
|
|
|
static void spawn(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
|
2014-01-08 15:29:34 -05:00
|
|
|
static size_t argbsz;
|
2014-01-03 06:52:47 -05:00
|
|
|
static size_t argbpos;
|
2015-03-22 17:53:12 -04:00
|
|
|
static size_t maxargs = 0;
|
|
|
|
static int nerrors = 0;
|
|
|
|
static int rflag = 0, nflag = 0, tflag = 0, xflag = 0;
|
|
|
|
static char *argb;
|
|
|
|
static char *cmd[NARGS];
|
|
|
|
static char *eofstr;
|
2014-01-03 06:52:47 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
inputc(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
ch = getc(stdin);
|
|
|
|
if (ch == EOF && ferror(stdin))
|
2015-03-22 17:53:12 -04:00
|
|
|
eprintf("getc <stdin>:");
|
2014-01-03 06:52:47 -05:00
|
|
|
|
2015-03-22 17:53:12 -04:00
|
|
|
return ch;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(int ch)
|
2014-01-03 06:52:47 -05:00
|
|
|
{
|
|
|
|
if (argbpos >= argbsz) {
|
2014-01-08 15:29:34 -05:00
|
|
|
argbsz = argbpos == 0 ? 1 : argbsz * 2;
|
2014-11-16 05:07:26 -05:00
|
|
|
argb = erealloc(argb, argbsz);
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
argb[argbpos] = ch;
|
|
|
|
}
|
|
|
|
|
2014-01-06 13:12:06 -05:00
|
|
|
static int
|
2014-01-03 06:52:47 -05:00
|
|
|
eatspace(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
|
|
|
break;
|
|
|
|
default:
|
2015-03-22 17:53:12 -04:00
|
|
|
ungetc(ch, stdin);
|
2014-01-06 13:12:06 -05:00
|
|
|
return ch;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 13:12:06 -05:00
|
|
|
return -1;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parsequote(int q)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
2014-01-08 15:22:10 -05:00
|
|
|
if (ch == q)
|
2014-01-03 06:52:47 -05:00
|
|
|
return 0;
|
|
|
|
if (ch != '\n') {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
|
|
|
}
|
|
|
|
}
|
2015-03-22 17:53:12 -04:00
|
|
|
|
2014-01-03 06:52:47 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-06 13:20:26 -05:00
|
|
|
static int
|
2014-01-03 06:52:47 -05:00
|
|
|
parseescape(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if ((ch = inputc()) != EOF) {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
2014-01-06 13:20:26 -05:00
|
|
|
return ch;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
2015-03-22 17:53:12 -04:00
|
|
|
|
2014-01-06 13:20:26 -05:00
|
|
|
return -1;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
poparg(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
argbpos = 0;
|
2014-11-19 14:59:37 -05:00
|
|
|
if (eatspace() < 0)
|
2014-01-06 13:12:06 -05:00
|
|
|
return NULL;
|
2014-01-03 06:52:47 -05:00
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
2014-01-07 06:53:55 -05:00
|
|
|
goto out;
|
2014-01-03 06:52:47 -05:00
|
|
|
case '\'':
|
2014-11-19 14:59:37 -05:00
|
|
|
if (parsequote('\'') < 0)
|
2014-01-30 08:54:16 -05:00
|
|
|
eprintf("unterminated single quote\n");
|
2014-01-03 06:52:47 -05:00
|
|
|
break;
|
|
|
|
case '\"':
|
2014-11-19 14:59:37 -05:00
|
|
|
if (parsequote('\"') < 0)
|
2014-01-30 08:54:16 -05:00
|
|
|
eprintf("unterminated double quote\n");
|
2014-01-03 06:52:47 -05:00
|
|
|
break;
|
|
|
|
case '\\':
|
2014-11-19 14:59:37 -05:00
|
|
|
if (parseescape() < 0)
|
2014-01-30 08:54:16 -05:00
|
|
|
eprintf("backslash at EOF\n");
|
2014-01-03 06:52:47 -05:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 06:53:55 -05:00
|
|
|
out:
|
2014-01-08 15:25:07 -05:00
|
|
|
fillargbuf('\0');
|
2015-03-22 17:53:12 -04:00
|
|
|
|
|
|
|
return (eofstr && !strcmp(argb, eofstr)) ? NULL : argb;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
2014-01-04 08:51:13 -05:00
|
|
|
static void
|
2014-01-07 09:50:59 -05:00
|
|
|
waitchld(void)
|
2014-01-03 06:52:47 -05:00
|
|
|
{
|
2014-01-07 09:50:59 -05:00
|
|
|
int status;
|
2014-01-03 06:52:47 -05:00
|
|
|
|
|
|
|
wait(&status);
|
2014-01-04 09:01:22 -05:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status) == 255)
|
|
|
|
exit(124);
|
|
|
|
if (WEXITSTATUS(status) == 127 ||
|
|
|
|
WEXITSTATUS(status) == 126)
|
|
|
|
exit(WEXITSTATUS(status));
|
2015-03-22 17:53:12 -04:00
|
|
|
if (status)
|
2014-01-06 13:51:06 -05:00
|
|
|
nerrors++;
|
2014-01-04 09:01:22 -05:00
|
|
|
}
|
2014-01-06 13:03:34 -05:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
exit(125);
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
2014-01-07 09:50:59 -05:00
|
|
|
|
|
|
|
static void
|
2014-01-08 15:27:27 -05:00
|
|
|
spawn(void)
|
2014-01-07 09:50:59 -05:00
|
|
|
{
|
2014-01-27 10:16:43 -05:00
|
|
|
int savederrno;
|
2015-03-22 17:53:12 -04:00
|
|
|
char **p;
|
|
|
|
|
|
|
|
if (tflag) {
|
|
|
|
for (p = cmd; *p; p++) {
|
|
|
|
fputs(*p, stderr);
|
|
|
|
fputc(' ', stderr);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2014-01-07 09:50:59 -05:00
|
|
|
|
2015-03-09 10:01:29 -04:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2015-03-10 15:05:18 -04:00
|
|
|
eprintf("fork:");
|
2015-03-09 10:01:29 -04:00
|
|
|
case 0:
|
2014-01-07 09:50:59 -05:00
|
|
|
execvp(*cmd, cmd);
|
2014-01-27 10:16:43 -05:00
|
|
|
savederrno = errno;
|
2014-01-07 09:50:59 -05:00
|
|
|
weprintf("execvp %s:", *cmd);
|
2015-03-08 20:04:34 -04:00
|
|
|
_exit(126 + (savederrno == ENOENT));
|
2014-01-07 09:50:59 -05:00
|
|
|
}
|
|
|
|
waitchld();
|
|
|
|
}
|
2015-03-07 07:36:40 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-22 17:53:12 -04:00
|
|
|
eprintf("usage: %s [-rtx] [-E eofstr] [-n num] [-s num] [cmd [arg ...]]\n", argv0);
|
2015-03-07 07:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-27 17:43:55 -04:00
|
|
|
int leftover = 0, i;
|
2015-03-22 17:53:12 -04:00
|
|
|
size_t argsz, argmaxsz;
|
2015-03-27 17:43:55 -04:00
|
|
|
size_t arglen, a;
|
2015-03-07 07:36:40 -05:00
|
|
|
char *arg = "";
|
|
|
|
|
2015-03-27 17:43:55 -04:00
|
|
|
if ((argmaxsz = sysconf(_SC_ARG_MAX)) == (size_t)-1)
|
|
|
|
argmaxsz = _POSIX_ARG_MAX;
|
2015-03-22 17:53:12 -04:00
|
|
|
/* Leave some room for environment variables */
|
2015-03-27 17:43:55 -04:00
|
|
|
argmaxsz -= 4096;
|
2015-03-22 17:53:12 -04:00
|
|
|
|
2015-03-07 07:36:40 -05:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
2015-03-22 17:53:12 -04:00
|
|
|
maxargs = estrtonum(EARGF(usage()), 1, MIN(SIZE_MAX, LLONG_MAX));
|
2015-03-07 07:36:40 -05:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2015-03-22 17:53:12 -04:00
|
|
|
case 's':
|
|
|
|
argmaxsz = estrtonum(EARGF(usage()), 1, MIN(SIZE_MAX, LLONG_MAX));
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
break;
|
2015-03-07 07:36:40 -05:00
|
|
|
case 'E':
|
|
|
|
eofstr = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
do {
|
|
|
|
argsz = 0; i = 0; a = 0;
|
2015-03-22 17:53:12 -04:00
|
|
|
if (argc) {
|
2015-03-07 07:36:40 -05:00
|
|
|
for (; i < argc; i++) {
|
|
|
|
cmd[i] = estrdup(argv[i]);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd[i] = estrdup("/bin/echo");
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
|
|
|
}
|
2015-03-22 17:53:12 -04:00
|
|
|
while (leftover || (arg = poparg())) {
|
|
|
|
if (argsz + strlen(arg) + 1 > argmaxsz || i >= NARGS - 1) {
|
|
|
|
if (strlen(arg) + 1 > argmaxsz) {
|
|
|
|
weprintf("insufficient argument space\n");
|
|
|
|
if (xflag)
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-03-07 07:36:40 -05:00
|
|
|
leftover = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd[i] = estrdup(arg);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
|
|
|
a++;
|
|
|
|
leftover = 0;
|
2015-03-22 17:53:12 -04:00
|
|
|
if (nflag && a >= maxargs)
|
2015-03-07 07:36:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd[i] = NULL;
|
2015-03-22 17:53:12 -04:00
|
|
|
if (a >= maxargs && nflag)
|
2015-03-07 07:36:40 -05:00
|
|
|
spawn();
|
2015-03-22 17:53:12 -04:00
|
|
|
else if (!a || (i == 1 && rflag))
|
2015-03-07 07:36:40 -05:00
|
|
|
;
|
|
|
|
else
|
|
|
|
spawn();
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
free(cmd[i]);
|
|
|
|
} while (arg);
|
|
|
|
|
|
|
|
free(argb);
|
|
|
|
|
2015-03-22 17:53:12 -04:00
|
|
|
return nerrors ? 123 : 0;
|
2015-03-07 07:36:40 -05:00
|
|
|
}
|