From fbd786d22aa097f81cea6d4323f79df22fda9d2e Mon Sep 17 00:00:00 2001 From: sin Date: Wed, 8 Jan 2014 20:47:21 +0000 Subject: [PATCH] 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. --- xargs.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/xargs.c b/xargs.c index 09002d9..59028f1 100644 --- 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(); @@ -118,15 +120,6 @@ deinputc(int ch) ungetc(ch, stdin); } -static void -deinputstr(char *arg) -{ - char *p; - - for (p = &arg[strlen(arg) - 1]; p >= arg; p--) - deinputc(*p); -} - static void fillargbuf(int ch) {