# HG changeset patch # User Ryoma SHINYA # Date 1278362381 -32400 # Node ID ceb0b8a73d68978df0fe5b494a6f4e13d7a6e691 # Parent 3ac5cabb2d76993d90698461b5c62da9961fa2be add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming" diff -r 3ac5cabb2d76 -r ceb0b8a73d68 src/template/grep.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/template/grep.c Tue Jul 06 05:39:41 2010 +0900 @@ -0,0 +1,84 @@ +/* Excerpted from 'The Practice of Programming' */ +/* by Brian W. Kernighan and Rob Pike */ + +#include +#include +#include + +#define BUFSIZE 1024 + +int match(char *regexp, char *text) { + if (regexp[0] == '^') + return matchhere(regexp+1, text); + do { + if (matchhere(regexp+1, text)) + return 1; + } while (*text++ != '\0'); + return 0; +} + +int matchhere(char *regexp, char *text) { + if (regexp[0] == '\0') + return 1; + if (regexp[1] == '*') + return matchstar(regexp[0], regexp+2, text); + if (regexp[0] == '$' && regexp[1] == '\0') + return *text == '\0'; + if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)) + return matchhere(regexp+1, text+1); + return 0; +} + +int matchstar(int c, char *regexp, char *text) { + do { + if (matchhere(regexp, text)) + return 1; + } while (*text != '\0' && (*text++ == c || c == '.')); + return 0; +} + +int grep(char * regexp, FILE *f, char *name) { + int n, nmatch; + char buf[BUFSIZE]; + nmatch = 0; + while (fgets(buf, sizeof buf, f) != NULL) { + n = strlen(buf); + if (n > 0 && buf[n-1] == '\n') + buf[n-1] = '\0'; + if (match(regexp, buf)) { + nmatch++; + if (name != NULL) + printf("%s:", name); + printf("%s\n", buf); + } + } + return nmatch; +} + +int main(int argc, char* argv[]) { + int i, nmatch; + FILE *f; + + if (argc < 2) { + fprintf(stderr, "usage: grep regexp [file ...]"); + exit(0); + } + nmatch = 0; + if (argc == 2) { + if (grep(argv[1], stdin, NULL)) + nmatch; + } else { + for (i = 2; i < argc; i++) { + f = fopen(argv[i], "r"); + if (f == NULL) { + fprintf(stderr, "can't open %s:", argv[i]); + continue; + } + if (grep(argv[1], f, argc > 3 ? argv[i] : NULL) > 0) + nmatch++; + fclose(f); + } + } + + return nmatch; +}