/* grep main: search for re in files */ int main(int argc, char *argv[]) { int i, nmatch; FILE *f; if (argc < 2) { fprintf(stderr, "usage: grep pattern [file ...]\n"); exit(2); } nmatch = 0; if (argc < 3) { if (grep(argv[1], stdin, NULL)) nmatch++; } else { for (i = 2; i < argc; i++) { f = fopen(argv[i], "r"); if (f == NULL) { fprintf(stderr, "grep: can't open %s\n", argv[i]); continue; } if (grep(argv[1], f, argc>3 ? argv[i] : NULL)) nmatch++; fclose(f); } } return nmatch == 0; }
Example 4: Main routine of an implementation of grep that uses match. (Assumes command interpreter expands wild cards in file specification on the command line into lists of file names. UNIX shells exhibit this behavior, although MS-DOS COMMAND.COM does not.)
Copyright © 1999, Dr. Dobb's Journal