home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / bm_src / bm.c next >
Encoding:
C/C++ Source or Header  |  1990-01-16  |  2.4 KB  |  94 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include "bm.h"
  4. #include "Extern.h"
  5.  
  6. int _mneed = 30000;
  7.  
  8. main(argc,argv)
  9. int argc;
  10. char *argv[];
  11. {
  12.     /* grep based on Boyer-Moore algorithm */
  13.     char BigBuff[MAXBUFF+2];
  14.     /*
  15.     * We leave one extra character at the beginning and end of the buffer,
  16.     * but don't tell Execute about it. This is so when someone is
  17.     * scanning the buffer and scans past the end (or beginning)
  18.     * we are still technically in the buffer (picky, but there ARE
  19.     * machines which would complain)
  20.     */
  21.     int ret = 1, /* return code from Execute */
  22.         NotFound = 0,        /* non-zero if file not readable */
  23.         NFiles,
  24.         NPats; /* number of patterns to search for */
  25.     char *FlagPtr,
  26.         **OptPtr; /* used to scan command line */
  27.     int TextFile /* file to search */;
  28.     struct PattDesc *DescVec[MAXPATS];
  29.  
  30.     --argc;
  31.     OptPtr = argv + 1;
  32.     while ( argc && **OptPtr == '-') {
  33.         FlagPtr = *OptPtr + 1;
  34.         do { 
  35.             switch (*FlagPtr) {
  36.                 case 'c': cFlag = 1; break;
  37.                 case 'e': eFlag = 1;
  38.                     /* get the patterns from next arg */
  39.                     NPats = MkDescVec(DescVec,*++OptPtr);
  40.                     --argc;
  41.                     break;
  42.                 case 'f': fFlag = 1; 
  43.                     /* read the patterns from a file */
  44.                     NPats = GetPatFile(*++OptPtr,DescVec);
  45.                     --argc;
  46.                     break;
  47.                 case 'l': lFlag = 1; break;
  48.                 case 'n': nFlag = 1; break;
  49.                 case 's': sFlag = 1; break;
  50.                 case 'x': xFlag = 1; break;
  51.                 case 'h': hFlag = 1; break;
  52.                 default:
  53.                     fprintf(stderr,
  54.                     "bm: invalid option: -%c \n",
  55.                     *FlagPtr);
  56.                     PutUsage();
  57.                     exit(2);
  58.             } /* switch */
  59.             ++FlagPtr;
  60.         } while (*FlagPtr);
  61.         ++OptPtr; --argc;
  62.     } /* while */
  63.     /* OptPtr now points to patterns */
  64.     if (!fFlag && !eFlag) {
  65.         if (!argc) {
  66.             fprintf(stderr,"bm: no pattern specified\n");
  67.             PutUsage();
  68.             exit(2);
  69.         } else
  70.             NPats = MkDescVec(DescVec,*OptPtr);
  71.         ++OptPtr; --argc;
  72.     }
  73.     /* OptPtr now points to first file */
  74.     NFiles = argc;
  75.     if (!NFiles)
  76.         ret &= Execute(DescVec,NPats,0,BigBuff+1);
  77.     else while (argc--) {
  78.         if ((NFiles > 1) || lFlag) FileName = *OptPtr;
  79.         if ((TextFile = open(*OptPtr,O_RDONLY|O_RAW)) < 0) {
  80.             fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  81.             NotFound++;
  82.         }
  83.         else {
  84.             ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  85.             if (sFlag && !ret)
  86.                 exit(0);
  87.             close(TextFile);
  88.         } /* if */
  89.         ++OptPtr;
  90.     } /* while */
  91.     if (cFlag) printf("%d\n",MatchCount);
  92.     exit(NotFound ? 2 : ret);
  93. } /* main */
  94.