[hackers] [sbase] Allocate the arg buffer once to its maximum size || sin

From: <git_AT_suckless.org>
Date: Tue, 07 Jan 2014 18:04:43 +0100

commit 1233cf31e5e2a5e40652dcaea3410967974705b6
Author: sin <sin_AT_2f30.org>
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
_AT_@ -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);
_AT_@ -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;
_AT_@ -40,7 +40,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
- long argsz, argmaxsz;
+ long argsz;
         char *arg;
         int i;
 
_AT_@ -65,7 +65,7 @@ main(int argc, char *argv[])
         if (!cmd)
                 eprintf("malloc:");
 
- argb = malloc(argbsz);
+ argb = malloc(argmaxsz);
         if (!argb)
                 eprintf("malloc:");
 
_AT_@ -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
_AT_@ -156,11 +153,13 @@ parsequote(int q)
 
         while ((ch = inputc()) != EOF) {
                 if (ch == q) {
- fillargbuf('
Received on Tue Jan 07 2014 - 18:04:43 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 07 2014 - 18:12:10 CET