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@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-06-01 14:03:10 +02:00 committed by sin
parent d12e953f18
commit 97fb4a1f9c
1 changed files with 28 additions and 26 deletions

54
grep.c
View File

@ -11,7 +11,7 @@
enum { Match = 0, NoMatch = 1, Error = 2 }; enum { Match = 0, NoMatch = 1, Error = 2 };
static void addpattern(const char *); static void addpattern(const char *);
static bool grep(FILE *, const char *); static int grep(FILE *, const char *);
static bool eflag = false; static bool eflag = false;
static bool vflag = false; static bool vflag = false;
@ -33,9 +33,9 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool match = false;
struct plist *pnode, *tmp; struct plist *pnode, *tmp;
int i, n, flags = REG_NOSUB; int i, n, m, flags = REG_NOSUB, match = NoMatch;
char buf[BUFSIZ];
FILE *fp; FILE *fp;
ARGBEGIN { ARGBEGIN {
@ -75,8 +75,6 @@ main(int argc, char *argv[])
/* Compile regex for all search patterns */ /* Compile regex for all search patterns */
for(pnode = phead; pnode; pnode = pnode->next) { for(pnode = phead; pnode; pnode = pnode->next) {
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) { if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
char buf[BUFSIZ];
regerror(n, &pnode->preg, buf, sizeof buf); regerror(n, &pnode->preg, buf, sizeof buf);
enprintf(Error, "invalid pattern: %s\n", buf); enprintf(Error, "invalid pattern: %s\n", buf);
} }
@ -86,10 +84,14 @@ main(int argc, char *argv[])
match = grep(stdin, "<stdin>"); match = grep(stdin, "<stdin>");
} else { } else {
for(i = 0; i < argc; i++) { for(i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r"))) if(!(fp = fopen(argv[i], "r"))) {
enprintf(Error, "fopen %s:", argv[i]); weprintf("fopen %s:", argv[i]);
if(grep(fp, argv[i])) match = Error;
match = true; continue;
}
m = grep(fp, argv[i]);
if(m == Error || (match != Error && m == Match))
match = m;
fclose(fp); fclose(fp);
} }
} }
@ -101,37 +103,33 @@ main(int argc, char *argv[])
free(pnode); free(pnode);
pnode = tmp; pnode = tmp;
} }
return match ? Match : NoMatch; return match;
} }
void static void
addpattern(const char *pattern) addpattern(const char *pattern)
{ {
struct plist *pnode; struct plist *pnode;
pnode = malloc(sizeof(*pnode)); if(!(pnode = malloc(sizeof(*pnode))))
if(!pnode)
eprintf("malloc:"); eprintf("malloc:");
pnode->pattern = strdup(pattern); if(!(pnode->pattern = strdup(pattern)))
if(!pnode->pattern)
eprintf("strdup:"); eprintf("strdup:");
pnode->next = phead; pnode->next = phead;
phead = pnode; phead = pnode;
} }
bool static int
grep(FILE *fp, const char *str) grep(FILE *fp, const char *str)
{ {
char *buf = NULL; char *buf = NULL;
long n, c = 0; size_t len = 0, size = 0;
size_t size = 0, len; long c = 0, n;
struct plist *pnode; 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) { for(pnode = phead; pnode; pnode = pnode->next) {
if(buf[(len = strlen(buf))-1] == '\n')
buf[--len] = '\0';
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag) if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
continue; continue;
switch(mode) { switch(mode) {
@ -148,17 +146,21 @@ grep(FILE *fp, const char *str)
printf("%s:", str); printf("%s:", str);
if(mode == 'n') if(mode == 'n')
printf("%ld:", n); printf("%ld:", n);
printf("%s\n", buf); printf("%s", buf);
if(len && buf[len - 1] != '\n')
putchar('\n');
break; break;
} }
match = true; match = Match;
} }
} }
if(mode == 'c') if(mode == 'c')
printf("%ld\n", c); printf("%ld\n", c);
end: end:
if(ferror(fp)) if(ferror(fp)) {
enprintf(Error, "%s: read error:", str); weprintf("%s: read error:", str);
match = Error;
}
free(buf); free(buf);
return match; return match;
} }