[hackers] [sbase] Correctly handle leftover input || sin

From: <git_AT_suckless.org>
Date: Wed, 08 Jan 2014 21:51:13 +0100

commit fbd786d22aa097f81cea6d4323f79df22fda9d2e
Author: sin <sin_AT_2f30.org>
Date: Wed Jan 8 20:47:21 2014 +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.

diff --git a/xargs.c b/xargs.c
index 09002d9..59028f1 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -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);
_AT_@ -40,6 +39,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
+ int leftover;
         long argsz, argmaxsz;
         char *arg;
         int i;
_AT_@ -65,6 +65,7 @@ main(int argc, char *argv[])
         if (!cmd)
                 eprintf("malloc:");
 
+ leftover = 0;
         do {
                 argsz = 0; i = 0;
                 if (argc > 0) {
_AT_@ -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
");
- 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();
_AT_@ -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) {
Received on Wed Jan 08 2014 - 21:51:13 CET

This archive was generated by hypermail 2.3.0 : Wed Jan 08 2014 - 22:00:12 CET