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

  1. #include <stdio.h>
  2. #include "bm.h"
  3. int
  4. GetPatFile(PatFile, DescVec)
  5. char *PatFile;
  6. struct PattDesc *DescVec[];
  7. /* read patterns from a file and set up a pattern descriptor vector */
  8. {
  9.     extern char *malloc();
  10.     FILE *PFile;
  11.     int PatSize; /* the number of chars in all the patterns */
  12.     char *PatBuff; /* hold the patterns */
  13.         char *s;
  14.         int readlen;
  15.  
  16.     if (!(strlen(PatFile))) {
  17.         fprintf(stderr,"bm: no pattern file given\n");
  18.         exit(2);
  19.     } /* if */
  20.     if (!strcmp(PatFile,"-")) {
  21.         PatSize = PSIZEDEF;
  22.                 PFile = stdin;
  23.         } else {
  24.             if (!(PFile = fopen(PatFile,"r"))) {
  25.                 fprintf(stderr,
  26.                                 "bm: can't open pattern file %s\n",PatFile);
  27.                 exit(2);
  28.                 }
  29.             if (fseek(PFile,0L,2) == -1) {
  30.                 fprintf(stderr,"bm: can't fseek %s\n",PatFile);
  31.                 exit(2);
  32.             } /* if */
  33.                 PatSize = ftell(PFile);
  34.                 rewind(PFile);
  35.             if (!PatSize) {
  36.                 fprintf(stderr,"bm: pattern file is empty\n");
  37.                 exit(2);
  38.             } /* if */
  39.     } /* if */
  40.     if (!(PatBuff = malloc(PatSize+1))) {
  41.             fprintf(stderr,"bm: insufficient memory to store patterns\n");
  42.         exit(2);
  43.     } /* if */
  44.         for (s = PatBuff; (readlen = (PatBuff + PatSize) - s) > 0
  45.                      && fgets(s,readlen,PFile) != (char *)0;
  46.                     s += strlen(s)) {
  47.         }
  48.     /* make sure the patterns are null-terminated. We can't have
  49.     * nulls in the patterns */
  50.     if (s[-1] == '\n')
  51.         s[-1] = '\0';
  52.     else
  53.         s[0] = '\0';
  54.         if (PFile != stdin) {
  55.         fclose(PFile);
  56.         } else {
  57.         clearerr(PFile);
  58.         }
  59.     return(MkDescVec(DescVec,PatBuff));
  60. } /* GetPatFile */
  61.