home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / util / shell / Compare10.lha / compare.c next >
Encoding:
C/C++ Source or Header  |  1997-01-31  |  1.9 KB  |  99 lines

  1. /* Compare two files */
  2.  
  3. #include <stdio.h>
  4.  
  5. BOOL check_bufs(char *buf1, char *buf2, int size);
  6. int filesize(FILE *fp);
  7. void msg(char *text);
  8.  
  9. BOOL verbose=1;
  10.  
  11. int main(int argc, char **argv) {
  12.     FILE *f1,*f2;
  13.     char *buf1,*buf2;
  14.     int count= -1;
  15.     BOOL result=RETURN_OK;
  16.     
  17.     if(argc>2 && argc<5) {
  18.         if(argc==4 && !strcmp(strlwr(argv[3]),"quiet")) verbose=0;
  19.         if(buf1=malloc(10240)) {
  20.             if(buf2=malloc(10240)) {
  21.                 if(f1=fopen(argv[1],"rb")) {
  22.                     if(f2=fopen(argv[2],"rb")) {
  23.                         if(filesize(f1)==filesize(f2)) {
  24.                             while(count!=0) {
  25.                                 count=fread(buf1,1,10240,f1);
  26.                                 count=fread(buf2,1,10240,f2);
  27.                                 if(check_bufs(buf1,buf2,count)) {
  28.                                     result=RETURN_WARN;
  29.                                     msg("Files contain different data\n");
  30.                                     break;
  31.                                 }
  32.                             }
  33.                         } else {
  34.                             msg("Files are of different size\n");
  35.                             result=RETURN_WARN;
  36.                         }
  37.                         fclose(f2);
  38.                     } else {
  39.                         char str[200];
  40.                         sprintf(str,"File %s does not exist!\n",argv[2]);
  41.                         msg(str);
  42.                         result=RETURN_ERROR;
  43.                     }
  44.                     fclose(f1);
  45.                 } else {
  46.                     char str[200];
  47.                     sprintf(str,"File %s does not exist!\n",argv[1]);
  48.                     msg(str);
  49.                     result=RETURN_ERROR;
  50.                 }
  51.                 free(buf2);
  52.             } else
  53.                 result=RETURN_FAIL;
  54.             free(buf1);
  55.         } else
  56.             result=RETURN_FAIL;
  57.     } else {
  58.         
  59.         msg("\tUsage: compare {first file} {second file} [QUIET]\n");
  60.         result=RETURN_FAIL;
  61.     }
  62.     
  63.     if(result==RETURN_OK)
  64.         msg("Files are the same\n");
  65.     
  66.     return(result);
  67. }
  68.  
  69. BOOL check_bufs(char *buf1, char *buf2, int size) {
  70.     int i;
  71.     BOOL result=0;
  72.  
  73.     for(i=0; i<size; i++) {
  74.         if(buf1[i]!=buf2[i])
  75.             result=1;
  76.     }
  77.     
  78.     return(result);
  79. }
  80.  
  81. int filesize(FILE *fp) {
  82.     long oldpos;
  83.     int length;
  84.  
  85.     oldpos=ftell(fp);
  86.  
  87.     fseek(fp,0,SEEK_END);
  88.     length=ftell(fp);
  89.     fseek(fp,oldpos,SEEK_SET);
  90.  
  91.     return(length);
  92. }
  93.  
  94. void msg(char *text) {
  95.     if(verbose) {
  96.         printf("Compare 1.0 by Ben Matthew (31.1.97)\n");
  97.         printf("%s",text);
  98.     }
  99. }