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

  1. /*    
  2. ** fv.c        File View/Compare Program    by F.A.Scacchitti  9/11/85
  3. **
  4. **        Written in Small-C Version 2.10 or later
  5. **
  6. **        Dumps contents of single file to screen
  7. **                or
  8. **        Dumps contents of 2 files and xored difference
  9. ** 
  10. **        Displays in hex and ascii form
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. #define BUFSIZE 1024
  16.  
  17. int fdin1, fdin2;    /* file  i/o channel pointers */
  18. int i, j, k,  val, count, total, offset, numdisp;
  19. char *inbuf1, *inbuf2, *difbuf, c;
  20.  
  21. main(argc,argv) int argc, argv[]; {
  22.  
  23.    switch (argc) {
  24.    case 2:
  25.    case 3:
  26.       inbuf1 = malloc(BUFSIZE);
  27.       if((fdin1 = fopen(argv[1],"r")) == NULL) {
  28.          printf("\nUnable to open %s\n", argv[1]);
  29.          exit();
  30.       }
  31.       numdisp = 1;
  32.       if(argc == 2) break;
  33.    case 3:
  34.       inbuf2 = malloc(BUFSIZE);
  35.       difbuf = malloc(BUFSIZE);
  36.       if((fdin2 = fopen(argv[2],"r")) == NULL) {
  37.          printf("\nUnable to open %s\n", argv[2]);
  38.          exit();
  39.       }
  40.       numdisp = 3;
  41.       break;
  42.    default:
  43.       printf("\nfv usage: fv <file1>          -  dump file\n");
  44.       printf("          fv <file1> <file2>  -  compare 2 files\n");
  45.       exit();
  46.    }
  47.    total = offset = 0;
  48.  
  49.    printf("\n");
  50.  
  51.    do {
  52.       count = read(fdin1,inbuf1,BUFSIZE);
  53.       if(argc >= 3){
  54.          read(fdin2,inbuf2,BUFSIZE);
  55.  
  56.          for(i=0; i< count; i++)
  57.             difbuf[i] = inbuf1[i] ^ inbuf2[i];
  58.       }
  59.  
  60.       for(i=0; i< count; i+=16){
  61.  
  62.          for(k=1; k <= numdisp; k++){
  63.  
  64.             switch (k) {
  65.             case 2:
  66.                offset = BUFSIZE;
  67.                break;
  68.             case 3:
  69.                offset = 2 * BUFSIZE;
  70.                break;
  71.             default:
  72.                offset = 0;
  73.                break;
  74.             }
  75.             if(k < 3)
  76.                printf("f-%d", k);
  77.             else
  78.                printf("dif");
  79.  
  80.             printf(" %04x  ",i+total);
  81.  
  82.             for(j=0; j<=15; j++){
  83.                val = inbuf1[i + j + offset];
  84.                printf("%02x ",val < 0 ? val - 65280 : val);
  85.  
  86.                if((c = bdos(6,255)) == 19){     /* hold on ^S */
  87.                   if((c = getchx()) == 3)
  88.                      exit();                    /* exit on ^C */
  89.  
  90.                }                                /* continue on ^Q */
  91.  
  92.             }
  93.             printf("  ");
  94.  
  95.             for(j=0; j<=15; j++){
  96.                c = inbuf1[i + j + offset];
  97.                if(c > 31)
  98.                   putchar(c);
  99.                else
  100.                   if(c==0)
  101.                      putchar(61);
  102.                   else
  103.                      putchar(94);
  104.             }
  105.             printf("\n");
  106.             if(k == 3) printf("\n");
  107.          }
  108.       }
  109.       total += count;
  110.  
  111.    } while(count == BUFSIZE);
  112.  
  113.       /* close up shop */
  114.  
  115.    fclose(fdin1);
  116.    if(argc == 3)
  117.       fclose(fdin2);
  118. }
  119.  
  120.