Mercurial > hg > Members > shinya > pyrect
view pyrect/template/grep.c @ 34:50b10929be29
change compile-method to full-source-compile.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 Jul 2010 22:50:37 +0900 |
parents | e9e90c006760 |
children |
line wrap: on
line source
/* Excerpted from 'The Practice of Programming' */ /* by Brian W. Kernighan and Rob Pike */ int grep(char * regexp, FILE *f, char *name) { int n, nmatch; char buf[LINEBUFSIZE]; 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 grepmain(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 (READBUFSIZE > 0) setvbuf(f, readbuf, _IOFBF, READBUFSIZE); if (grep(argv[1], f, argc > 3 ? argv[i] : NULL) > 0) nmatch++; fclose(f); } } return nmatch; }