home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / crc-check / crc_check.c next >
C/C++ Source or Header  |  1989-03-07  |  4KB  |  188 lines

  1.  
  2. /*
  3.     This progam will compare two crc lists and report the differences.
  4.     
  5.     By Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us)
  6.  
  7.     Permission is granted to use this in any manner provided that    
  8.     1) the copyright notice is left intact, 
  9.     2) you don't hold me responsible for any bugs and 
  10.     3) you mail me any improvements that you make.  
  11.  
  12.  
  13.     report:
  14.          corrupt    -    crc changed w/o date change
  15.          replaced    -    crc + date changed
  16.          perm        -    permissions changed
  17.          own/grp    -    owner or group changed
  18.      removed    -    
  19.      added        -
  20.  
  21. Print the info for the new file except for deleted.
  22.  
  23. Use:
  24.  
  25. find / -print | sort | xargs crc -v > crc_file
  26.  
  27. to generate a crc list (crc.c should accompany this source).
  28.  
  29. Assume that no files have tabs or spaces in the name.
  30.  
  31. */
  32.  
  33. /* max size of line */
  34.  
  35. #define BUF_SIZE 1124
  36.  
  37. #include <stdio.h>
  38.  
  39. char    *strrchr();
  40. void    exit();
  41.  
  42. char    new_line[BUF_SIZE];
  43. char    old_line[BUF_SIZE];
  44.  
  45. FILE *new_file;
  46. FILE *old_file;
  47.  
  48. main(argc, argv)
  49. int    argc;
  50. char    **argv;
  51. {
  52.    /*
  53.  
  54.            If line =, read new line from each file
  55.            else
  56.            If date/perm/crc change, report and read new line from each file
  57.            else
  58.            If old_line < new_line, report file removed, read old line
  59.            else
  60.               report new line as added
  61.               read new_line
  62.         loop
  63. */
  64.  
  65.    char    *new_ptr;
  66.    char    *old_ptr;
  67.  
  68.    if (argc != 3) {
  69.       (void) printf("wrong number of arguments\n");
  70.       (void) printf("crc_check old_crc_file new_crc_file\n");
  71.       exit(1);
  72.    }
  73.    new_file = fopen(argv[2], "r");
  74.    old_file = fopen(argv[1], "r");
  75.  
  76.    if (new_file == NULL || old_file == NULL) {
  77.       (void) printf("can't open input files\n");
  78.       (void) printf("crc_check old_crc_file new_crc_file\n");
  79.       exit(1);
  80.    }
  81.  
  82.    get_line(new_line);
  83.    get_line(old_line);
  84.  
  85.    for (; ; ) {
  86.  
  87.       check_eof();
  88.  
  89.       /* If equal, print nothing and get new lines */
  90.  
  91.       if (strcmp(old_line, new_line) == 0) {
  92.          get_line(new_line);
  93.          get_line(old_line);
  94.          continue;
  95.       }
  96.  
  97.       /* Compare just the file names */
  98.  
  99.       new_ptr = strrchr(new_line, ' ');
  100.       old_ptr = strrchr(old_line, ' ');
  101.  
  102.       if (new_ptr == NULL || old_ptr == NULL) {
  103.          (void) printf("Error in input data\n");
  104.          exit(1);
  105.       }
  106.  
  107.       if (strcmp(old_ptr, new_ptr) == 0) {
  108.  
  109.          new_ptr = strrchr(new_line, '\t');
  110.          old_ptr = strrchr(old_line, '\t');
  111.  
  112.          if (new_ptr == NULL || old_ptr == NULL) {
  113.             (void) printf("Error in input data\n");
  114.             exit(1);
  115.          }
  116.  
  117.          /* check crc change */
  118.  
  119.          if (strncmp(new_line, old_line, 4) != 0)
  120.             if (strcmp(new_ptr, old_ptr) == 0)
  121.                (void) printf("corrupt  %s", new_line + 5);
  122.             else
  123.                (void) printf("replaced %s", new_line + 5);
  124.  
  125.  
  126.          /* check permission chenage */
  127.  
  128.          if (strncmp(new_line + 5, old_line + 5, 11) != 0)
  129.             (void) printf("permiss  %s", new_line + 5);
  130.  
  131.          /* check  owner/group */
  132.  
  133.          if (strncmp(new_line+16, old_line+16, new_ptr - new_line - 15) != 0)
  134.             (void) printf("own/grp  %s", new_line + 5);
  135.  
  136.          get_line(new_line);
  137.          get_line(old_line);
  138.          continue;
  139.       }
  140.  
  141.  
  142.       if (strcmp(old_ptr, new_ptr) < 0) {
  143.          (void) printf("removed  %s", old_line + 5);
  144.          get_line(old_line);
  145.          continue;
  146.       }
  147.  
  148.       (void) printf("added    %s", new_line + 5);
  149.       get_line(new_line);
  150.  
  151.    }
  152.  
  153. }
  154.  
  155.  
  156. get_line(string)
  157. char    *string;
  158. {
  159.    if (string == new_line)
  160.       (void) fgets(string, BUF_SIZE, new_file);
  161.    else
  162.       (void) fgets(string, BUF_SIZE, old_file);
  163.  
  164. }
  165.  
  166.  
  167. check_eof()
  168. {
  169.  
  170.    if (feof(new_file)) {
  171.  
  172.       while (!feof(old_file)) {
  173.          (void) printf("removed  %s", old_line + 5);
  174.          (void) fgets(old_line, BUF_SIZE, old_file);
  175.       }
  176.       exit(0);
  177.    } else if (feof(old_file)) {
  178.       while (!feof(new_file)) {
  179.          (void) printf("added    %s", new_line + 5);
  180.          (void) fgets(new_line, BUF_SIZE, new_file);
  181.       }
  182.       exit(0);
  183.    }
  184.  
  185. }
  186.  
  187.  
  188.