#include #include #include #include int grep(Biobuf*, Reprog*, char*); char* lower(char*); int negate = 0; int cflag = 0; int hflag = 0; int iflag = 0; int lflag = 0; int Lflag = 0; int nflag = 0; int sflag = 0; void main(int argc, char *argv[]) { Reprog *r; Biobuf *b; Biobuf bin; int i, match; ARGBEGIN{ case 'c': cflag = 1; break; case 'h': hflag = 1; break; case 'i': iflag = 1; break; case 'L': Lflag = 1; /* fall through */ case 'l': lflag = 1; break; case 'n': nflag = 1; break; case 's': sflag = 1; break; case 'v': negate = 1; break; default: goto Usage; }ARGEND; if(argc < 1) Usage: regerror("usage: grep [-chiLlnsv] pattern [file ...]"); r = regcomp(lower(argv[0])); match = 0; if(argc == 1){ hflag = 1; Binit(&bin, 0, OREAD); match = grep(&bin, r, "stdin"); }else for(i=1; i 0){ /* line too long; skip and continue */ Bseek(b, BLINELEN(b), 1); goto Loop; } if(cflag && !sflag){ if(hflag) print("%d\n", count); else print("%s:%d\n", file, count); } if(lflag){ if((count==0) == Lflag) print("%s\n", file); } return count; } char* lower(char *s) { char *t; int w; Rune r; if(iflag) for(t=s; r=*t; t+=w){ if(r < Runeself) w = 1; else w = chartorune(&r, t); r = tolower(r); if(runetochar(t, &r) != w) regerror("rune incomprehensible"); } return s; } void regerror(char *s) { fprint(2, "grep: %s\n", s); exits(s); }