FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Architecture & Design
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
April 01, 1999

Regular Expressions

(Page 5 of 7)
Apr99: Regular Expressions


/* 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
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK