commit fbd786d22aa097f81cea6d4323f79df22fda9d2e
parent 1963a7cfb37e30cb4707e6d1f45166f8cebda608
Author: sin <sin@2f30.org>
Date: Wed, 8 Jan 2014 20:47:21 +0000
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.
Diffstat:
1 file changed, 5 insertions(+), 12 deletions(-)
diff --git a/xargs.c b/xargs.c
@@ -14,7 +14,6 @@ enum {
static int inputc(void);
static void deinputc(int);
-static void deinputstr(char *);
static void fillargbuf(int);
static int eatspace(void);
static int parsequote(int);
@@ -40,6 +39,7 @@ usage(void)
int
main(int argc, char *argv[])
{
+ int leftover;
long argsz, argmaxsz;
char *arg;
int i;
@@ -65,6 +65,7 @@ main(int argc, char *argv[])
if (!cmd)
eprintf("malloc:");
+ leftover = 0;
do {
argsz = 0; i = 0;
if (argc > 0) {
@@ -77,17 +78,18 @@ main(int argc, char *argv[])
argsz += strlen(cmd[i]) + 1;
i++;
}
- while ((arg = poparg())) {
+ while (leftover == 1 || (arg = poparg())) {
if (argsz + strlen(arg) + 1 > argmaxsz ||
i >= NARGS - 1) {
if (strlen(arg) + 1 > argmaxsz)
enprintf(EXIT_FAILURE, "insufficient argument space\n");
- deinputstr(arg);
+ leftover = 1;
break;
}
cmd[i] = strdup(arg);
argsz += strlen(cmd[i]) + 1;
i++;
+ leftover = 0;
}
cmd[i] = NULL;
if (i == 1 && rflag == 1); else spawn();
@@ -119,15 +121,6 @@ deinputc(int ch)
}
static void
-deinputstr(char *arg)
-{
- char *p;
-
- for (p = &arg[strlen(arg) - 1]; p >= arg; p--)
- deinputc(*p);
-}
-
-static void
fillargbuf(int ch)
{
if (argbpos >= argbsz) {