GetPatFile.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include "bm.h"
  5. int
  6. GetPatFile(PatFile, DescVec)
  7. char *PatFile;
  8. struct PattDesc *DescVec[];
  9. /* read patterns from a file and set up a pattern descriptor vector */
  10. {
  11. extern char *malloc();
  12. FILE *PFile;
  13. struct stat StatBuff;
  14. int PatSize; /* the number of chars in all the patterns */
  15. char *PatBuff; /* hold the patterns */
  16. if (!(strlen(PatFile))) {
  17. fprintf(stderr,"bm: no pattern file given\n");
  18. exit(2);
  19. } /* if */
  20. if (!(PFile = fopen(PatFile,"r"))) {
  21. fprintf(stderr,"bm: can't open pattern file %s\n",PatFile);
  22. exit(2);
  23. } /* if */
  24. /* find out how big the patterns are */
  25. if (fstat(fileno(PFile),&StatBuff) == -1) {
  26. fprintf(stderr,"bm: can't fstat %s\n",PatFile);
  27. exit(2);
  28. } /* if */
  29. if (isatty(fileno(PFile)))
  30. PatSize = PSIZEDEF;
  31. else PatSize = StatBuff.st_size;
  32. if (!PatSize) {
  33. fprintf(stderr,"bm: pattern file is empty\n");
  34. exit(2);
  35. } /* if */
  36. if (!(PatBuff = malloc(PatSize+1))) {
  37. fprintf(stderr,"bm: insufficient memory to store patterns\n");
  38. exit(2);
  39. } /* if */
  40. fread(PatBuff,1,PatSize,PFile); /* get the patterns */
  41. /* make sure the patterns are null-terminated. We can't have
  42. * nulls in the patterns */
  43. if (PatBuff[PatSize-1] == '\n')
  44. PatBuff[PatSize-1] = '\0';
  45. else
  46. PatBuff[PatSize] = '\0';
  47. fclose(PFile);
  48. return(MkDescVec(DescVec,PatBuff));
  49. } /* GetPatFile */