home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 193_01 / sp.c < prev    next >
Text File  |  1985-11-14  |  3KB  |  121 lines

  1. /*    
  2. ** sp.c        Search Pattern Program        by F.A.Scacchitti 10/15/85
  3. **
  4. **        Written in Small-C Version 2.10 or later
  5. **
  6. **        Searches file for repetitive pattern.
  7. **
  8. **
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #define BUFSIZE 16384
  14.  
  15.  
  16. int  fdin;        /* file  i/o channel pointers */
  17. int i, j, n, block, start, depth, count, limit;
  18. char *c, *inbuf; 
  19.  
  20. main(argc,argv) int argc, argv[]; {
  21.  
  22. /*
  23. ** Set defaults
  24. */
  25.    block = 128;
  26.    depth = 4;
  27.    start = 0;
  28.  
  29. /*
  30. ** Allocate memory for buffer
  31. */
  32.    inbuf = malloc(BUFSIZE);
  33.  
  34. /*
  35. ** Check arguments passed and open file stream
  36. */
  37.    if(argc < 2) {
  38.       printf("\nsp usage: sp <source file> [nnnn] [dddd] [ssss] [x]\n");
  39.       printf("          nnnn = block size to search   default = 128\n");
  40.       printf("          dddd = minimum depth of comparison  default = 4\n");
  41.       printf("          ssss = starting point in buffer default = 0\n");
  42.       printf("          x - any char. gen's difference buffer ((n+1)-n)\n");
  43.       exit();
  44.    }
  45.    if((fdin = fopen(argv[1],"r")) == NULL) {
  46.       printf("\nUnable to open %s\n", argv[1]);
  47.       exit();
  48.    }
  49.  
  50. /*
  51. ** Convert optional inputs to integer and implement
  52. */
  53.    if(argc > 2)
  54.       if((n = atoi(argv[2])) != NULL) 
  55.          block = n;
  56.  
  57.    if(argc > 3)
  58.       if((n = atoi(argv[3])) != NULL) 
  59.          depth = n;
  60.  
  61.    if(argc > 4)
  62.       if((n = atoi(argv[4])) != NULL) 
  63.          start = n;
  64.  
  65. /*
  66. ** Fill the buffer with as much as possible
  67. */
  68.    count = read(fdin,inbuf,BUFSIZE);
  69.  
  70.    limit = count - depth;
  71.  
  72. /*
  73. ** If there's a sixth argument, convert the file to numerical sequence
  74. */
  75.  
  76.    if(argc > 5){
  77.       for(i = 0; i < count - 1; i++)
  78.          inbuf[i] = inbuf[i + 1] - inbuf[i];
  79.    }
  80.  
  81.    for(i = start; i < block; i++){
  82.       printf("%c", (i % 10 == 0) ? '|' : '.');
  83.       chkkbd();
  84.       for(j = i + 1; j <= limit; j++)
  85.          if(inbuf[i] == inbuf[j]){
  86.             if((n = chkdepth(i, j, 0)) >= depth)
  87.                printf("\nmatch at %d (%x hex) and %d (%x hex) - %d deep\n", i, i, j, j, n);
  88.             if(n >= block) exit();
  89.          }
  90.    }
  91. /*
  92. ** Close up shop
  93. */
  94.    printf("\n");    /* Flush print buffer */
  95.  
  96.    fclose(fdin);
  97. }
  98.  
  99. chkdepth(pointer, offset, k)int pointer, offset, k;{
  100.  
  101.  
  102.    while(inbuf[pointer] == inbuf[offset] && k < count){
  103.       pointer++;
  104.       offset++;
  105.       k++;
  106.    }
  107.    return(k);
  108. }
  109.  
  110. chkkbd(){
  111.  
  112. char c;
  113.  
  114.    if((c = bdos(6,255)) == 19){     /* hold on ^S */
  115.       if((c = getchx()) == 3)
  116.          exit();                    /* exit on ^C */
  117.  
  118.    }                                /* continue   */
  119. }
  120.  
  121.