[hackers] [sbase] grep: Reuse allocated buffers || sin

From: <git_AT_suckless.org>
Date: Tue, 16 Dec 2014 21:22:00 +0100

commit fb85f99c0a5bb330178d29d582aedc07b1a0f5db
Author: sin <sin_AT_2f30.org>
Date: Tue Dec 16 20:20:41 2014 +0000

    grep: Reuse allocated buffers

diff --git a/grep.c b/grep.c
index 86b8ca5..1376940 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -177,15 +177,15 @@ addpattern(const char *pattern)
 static void
 addpatternfile(FILE *fp)
 {
- char *buf = NULL;
- size_t len = 0, size = 0;
+ static char *buf = NULL;
+ static size_t size = 0;
+ size_t len = 0;
 
         while ((len = getline(&buf, &size, fp)) != -1) {
                 if (len && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
                 addpattern(buf);
         }
- free(buf);
         if (ferror(fp))
                 enprintf(Error, "read error:");
 }
_AT_@ -193,8 +193,9 @@ addpatternfile(FILE *fp)
 static int
 grep(FILE *fp, const char *str)
 {
- char *buf = NULL;
- size_t len = 0, size = 0;
+ static char *buf = NULL;
+ static size_t size = 0;
+ size_t len = 0;
         long c = 0, n;
         struct pattern *pnode;
         int match = NoMatch;
_AT_@ -250,6 +251,5 @@ end:
                 weprintf("%s: read error:", str);
                 match = Error;
         }
- free(buf);
         return match;
 }
Received on Tue Dec 16 2014 - 21:22:00 CET

This archive was generated by hypermail 2.3.0 : Tue Dec 16 2014 - 21:24:10 CET