commit 1233cf31e5e2a5e40652dcaea3410967974705b6
Author: sin <[email protected]>
Date:   Tue Jan 7 16:26:55 2014 +0000

    Allocate the arg buffer once to its maximum size

diff --git a/xargs.c b/xargs.c
index d18cb88..1c7b437 100644
--- a/xargs.c
+++ b/xargs.c
@@ -14,7 +14,7 @@ enum {
 
 static int inputc(void);
 static void deinputc(int);
-static void fillargbuf(int);
+static int fillargbuf(int);
 static int eatspace(void);
 static int parsequote(int);
 static int parseescape(void);
@@ -25,8 +25,8 @@ static void runcmd(void);
 
 static char **cmd;
 static char *argb;
-static size_t argbsz = 1;
-static size_t argbpos;
+static long argmaxsz;
+static long argbpos;
 static int nerrors = 0;
 static char *eofstr;
 static int rflag = 0;
@@ -40,7 +40,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-       long argsz, argmaxsz;
+       long argsz;
        char *arg;
        int i;
 
@@ -65,7 +65,7 @@ main(int argc, char *argv[])
        if (!cmd)
                eprintf("malloc:");
 
-       argb = malloc(argbsz);
+       argb = malloc(argmaxsz);
        if (!argb)
                eprintf("malloc:");
 
@@ -120,16 +120,13 @@ deinputc(int ch)
        ungetc(ch, stdin);
 }
 
-static void
+static int
 fillargbuf(int ch)
 {
-       if (argbpos >= argbsz) {
-               argbsz *= 2;
-               argb = realloc(argb, argbsz);
-               if (!argb)
-                       eprintf("realloc:");
-       }
+       if (argbpos >= argmaxsz)
+               return -1;
        argb[argbpos] = ch;
+       return 0;
 }
 
 static int
@@ -156,11 +153,13 @@ parsequote(int q)
 
        while ((ch = inputc()) != EOF) {
                if (ch == q) {
-                       fillargbuf('

Reply via email to