Correctly handle leftover input

We cannot rely on ungetc() pushing back more than 1 character
reliably on all systems, so just note if we have leftover input and
process it in the next run.
This commit is contained in:
sin 2014-01-08 20:47:21 +00:00
parent 1963a7cfb3
commit fbd786d22a

17
xargs.c
View File

@ -14,7 +14,6 @@ enum {
static int inputc(void); static int inputc(void);
static void deinputc(int); static void deinputc(int);
static void deinputstr(char *);
static void fillargbuf(int); static void fillargbuf(int);
static int eatspace(void); static int eatspace(void);
static int parsequote(int); static int parsequote(int);
@ -40,6 +39,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int leftover;
long argsz, argmaxsz; long argsz, argmaxsz;
char *arg; char *arg;
int i; int i;
@ -65,6 +65,7 @@ main(int argc, char *argv[])
if (!cmd) if (!cmd)
eprintf("malloc:"); eprintf("malloc:");
leftover = 0;
do { do {
argsz = 0; i = 0; argsz = 0; i = 0;
if (argc > 0) { if (argc > 0) {
@ -77,17 +78,18 @@ main(int argc, char *argv[])
argsz += strlen(cmd[i]) + 1; argsz += strlen(cmd[i]) + 1;
i++; i++;
} }
while ((arg = poparg())) { while (leftover == 1 || (arg = poparg())) {
if (argsz + strlen(arg) + 1 > argmaxsz || if (argsz + strlen(arg) + 1 > argmaxsz ||
i >= NARGS - 1) { i >= NARGS - 1) {
if (strlen(arg) + 1 > argmaxsz) if (strlen(arg) + 1 > argmaxsz)
enprintf(EXIT_FAILURE, "insufficient argument space\n"); enprintf(EXIT_FAILURE, "insufficient argument space\n");
deinputstr(arg); leftover = 1;
break; break;
} }
cmd[i] = strdup(arg); cmd[i] = strdup(arg);
argsz += strlen(cmd[i]) + 1; argsz += strlen(cmd[i]) + 1;
i++; i++;
leftover = 0;
} }
cmd[i] = NULL; cmd[i] = NULL;
if (i == 1 && rflag == 1); else spawn(); if (i == 1 && rflag == 1); else spawn();
@ -118,15 +120,6 @@ deinputc(int ch)
ungetc(ch, stdin); ungetc(ch, stdin);
} }
static void
deinputstr(char *arg)
{
char *p;
for (p = &arg[strlen(arg) - 1]; p >= arg; p--)
deinputc(*p);
}
static void static void
fillargbuf(int ch) fillargbuf(int ch)
{ {