Advertisement
shiftdot515

compare.c

Aug 3rd, 2020
1,891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.52 KB | None | 0 0
  1.  
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/errno.h>
  9.  
  10. #define BUFSIZE (1024*1024)
  11.  
  12. /* when you want to know what went wrong, or how it's different */
  13.  
  14. unsigned long long blocks_read; /* blocks read */
  15. unsigned long long differences;
  16.  
  17. /* change this to take BUFSIZE from environment */
  18. /* env options? bs= , copy to enviroment, update argv */
  19.  
  20. int main ( int argc , char * argv[])
  21. {
  22.   int numfiles;
  23.   ssize_t bytes; /* bytes read, by read for file1 */
  24.   int * files;
  25.   {
  26.     typedef char comparebuf[BUFSIZE];
  27.     comparebuf * buffers;
  28.     int i;
  29.    
  30.     numfiles=argc-1; /* number of files to compare with */
  31.     if ( numfiles > getdtablesize() )
  32.       fprintf(stderr,"Number of files, %d, exceeds, descriptor table size=%d\n"
  33.               ,numfiles,getdtablesize());
  34.  
  35.     files=calloc(numfiles,sizeof(int));
  36.     if ( files == NULL ) {
  37.       perror("calloc, files:");
  38.       exit(10);
  39.     } else { /* open files to compare with, handles in files[] */
  40.       int arg; /* position in argument list */
  41.       files[0]=open( argv[1], O_RDONLY|O_NONBLOCK, 0123 /*file mode not used */ );
  42.       if ( files[0] == -1 ) {  /* files[0] == first file */
  43.         perror("open file1:");
  44.         exit(11);
  45.       }
  46.       for( i = 1,arg=2; i<numfiles ; i++,arg++ ) {
  47.         files[i]=open(argv[arg], O_RDONLY|O_NONBLOCK, 0 );
  48.         if ( files[i] == -1 ) {
  49.           perror("opening files:");
  50.           exit(arg+10);  
  51.         }
  52.       }
  53.     }
  54.     buffers=calloc(numfiles,sizeof(comparebuf));
  55.     if ( buffers == NULL ) {
  56.       perror("calloc, buffers:");
  57.       exit(10);
  58.     }
  59.  
  60.     blocks_read=0;
  61.     differences=0;
  62.     do {
  63.       extern int errno;
  64.       ssize_t rr;
  65.       bytes=0;
  66.       {
  67.         register size_t nbytes=sizeof(comparebuf);
  68.         char * buffer=buffers[0];
  69.       TryAgain:
  70.         rr=read(files[0],buffer,nbytes);
  71.         if ( rr == -1 ) {   /* error ? */
  72.           if ( errno ==  EAGAIN )
  73.             goto TryAgain;
  74.           perror("file1:");
  75.           exit(11);
  76.         } else if ( rr < nbytes && rr > 0 ) {  /* short read, try again */
  77.           bytes+= rr;
  78.           nbytes= nbytes - rr ;
  79.           buffer= buffer + rr ;
  80.           goto TryAgain;
  81.         } else  /* rr == 0 || rr == nbytes,  EOF or complete read */
  82.           bytes+= rr;
  83.       }
  84.       if ( bytes == sizeof(comparebuf) )
  85.         blocks_read++;
  86.  
  87.       /* at this point, bytes, would should be == to sizeof(comparebuf), except
  88.        * for, possibly,the last read of the file, and then, of course,0 for EOF
  89.        */
  90.  
  91.       for(i=1;i<numfiles;i++) {
  92.         ssize_t ibytes; /* bytes read, by read for file i */
  93.         ibytes=0;
  94.         {
  95.           register size_t fbytes;
  96.           char * buffer;
  97.  
  98.           fbytes=bytes;
  99.           if ( fbytes <= 0 )
  100.             fbytes= sizeof(comparebuf);
  101.           buffer=buffers[i];
  102.         FilesTryAgain:
  103.           rr=read(files[i],buffer, fbytes);
  104.           if ( rr == -1 && errno == EAGAIN )
  105.               goto FilesTryAgain;
  106.           else if ( rr == -1 ) {
  107.             fprintf(stderr,"Error file %d %lld blocks read.\n",i+1,
  108.                     blocks_read);
  109.             perror("read files:");
  110.             exit(i+11);
  111.           } else if ( rr < fbytes && rr > 0) {
  112.             /* short read, try to read the rest */
  113.             ibytes+= rr;
  114.             fbytes= fbytes - rr;
  115.             buffer= buffer + rr;
  116.             goto FilesTryAgain;
  117.           } else
  118.             ibytes+= rr;
  119.         }
  120.         if ( ( bytes == 0 && ibytes > 0 )
  121.              || ( bytes != 0 && ( ibytes > bytes ))) {
  122.           printf("File %d too long.\n",i+1);
  123.           differences++;
  124.           continue;
  125.         } else if ( ibytes < bytes ) {
  126.           printf("File %d EOF,     block %lld.\n",i+1,blocks_read);
  127.           differences++;
  128.         } else if (memcmp(buffers[0], buffers[i],bytes) ) {
  129.           printf("File %d differs, block %lld.\n",i+1,blocks_read);
  130.           differences++;
  131.         }
  132.       } /* end for loop */
  133.  
  134.  
  135.                        
  136. #if 1
  137.       if ( blocks_read % 128 == 0 ) {
  138.         register int g;
  139.         g = blocks_read / 1024 ;
  140.         printf("  Thru Block %lld, GB %i.\n",blocks_read,g);
  141.       }
  142. #endif    
  143.     } while ( bytes > 0 );
  144.     printf("%lld blocks read,  %lld differences.\n",blocks_read,differences);
  145.     free(buffers);
  146.     for(i=0;i<numfiles;i++)
  147.       close(files[i]);
  148.     free(files);
  149.     return (differences != 0 ); /* so, == 0, returns 0 */
  150.   }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement