[hackers] [sbase] grep: improvements || Hiltjo Posthuma
commit 97fb4a1f9cf68f78da3838723086515072525408
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Jun 1 14:03:10 2014 +0200
grep: improvements
improvements:
- improve statuscode behaviour
- don't exit if a file in a series fails. exit 2 if an error occured
in a file series. don't exit if there is a read error (like: grep
input file is a directory).
- use agetline instead of agets().
with the simple test: time seq 1 100000000 | grep 'a'
its 12 seconds (from 24 seconds) on my machine.
Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
diff --git a/grep.c b/grep.c
index f4fe7df..715f864 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -11,7 +11,7 @@
enum { Match = 0, NoMatch = 1, Error = 2 };
static void addpattern(const char *);
-static bool grep(FILE *, const char *);
+static int grep(FILE *, const char *);
static bool eflag = false;
static bool vflag = false;
_AT_@ -33,9 +33,9 @@ usage(void)
int
main(int argc, char *argv[])
{
- bool match = false;
struct plist *pnode, *tmp;
- int i, n, flags = REG_NOSUB;
+ int i, n, m, flags = REG_NOSUB, match = NoMatch;
+ char buf[BUFSIZ];
FILE *fp;
ARGBEGIN {
_AT_@ -75,8 +75,6 @@ main(int argc, char *argv[])
/* Compile regex for all search patterns */
for(pnode = phead; pnode; pnode = pnode->next) {
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
- char buf[BUFSIZ];
-
regerror(n, &pnode->preg, buf, sizeof buf);
enprintf(Error, "invalid pattern: %s
", buf);
}
_AT_@ -86,10 +84,14 @@ main(int argc, char *argv[])
match = grep(stdin, "<stdin>");
} else {
for(i = 0; i < argc; i++) {
- if(!(fp = fopen(argv[i], "r")))
- enprintf(Error, "fopen %s:", argv[i]);
- if(grep(fp, argv[i]))
- match = true;
+ if(!(fp = fopen(argv[i], "r"))) {
+ weprintf("fopen %s:", argv[i]);
+ match = Error;
+ continue;
+ }
+ m = grep(fp, argv[i]);
+ if(m == Error || (match != Error && m == Match))
+ match = m;
fclose(fp);
}
}
_AT_@ -101,37 +103,33 @@ main(int argc, char *argv[])
free(pnode);
pnode = tmp;
}
- return match ? Match : NoMatch;
+ return match;
}
-void
+static void
addpattern(const char *pattern)
{
struct plist *pnode;
- pnode = malloc(sizeof(*pnode));
- if(!pnode)
+ if(!(pnode = malloc(sizeof(*pnode))))
eprintf("malloc:");
- pnode->pattern = strdup(pattern);
- if(!pnode->pattern)
+ if(!(pnode->pattern = strdup(pattern)))
eprintf("strdup:");
pnode->next = phead;
phead = pnode;
}
-bool
+static int
grep(FILE *fp, const char *str)
{
char *buf = NULL;
- long n, c = 0;
- size_t size = 0, len;
+ size_t len = 0, size = 0;
+ long c = 0, n;
struct plist *pnode;
- bool match = false;
+ int match = NoMatch;
- for(n = 1; afgets(&buf, &size, fp); n++) {
+ for(n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) {
for(pnode = phead; pnode; pnode = pnode->next) {
- if(buf[(len = strlen(buf))-1] == '
')
- buf[--len] = '
Received on Sun Jun 01 2014 - 19:02:05 CEST
This archive was generated by hypermail 2.3.0
: Sun Jun 01 2014 - 19:12:11 CEST