Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/errno.h>
- #define BUFSIZE (1024*1024)
- /* when you want to know what went wrong, or how it's different */
- unsigned long long blocks_read; /* blocks read */
- unsigned long long differences;
- /* change this to take BUFSIZE from environment */
- /* env options? bs= , copy to enviroment, update argv */
- int main ( int argc , char * argv[])
- {
- int numfiles;
- ssize_t bytes; /* bytes read, by read for file1 */
- int * files;
- {
- typedef char comparebuf[BUFSIZE];
- comparebuf * buffers;
- int i;
- numfiles=argc-1; /* number of files to compare with */
- if ( numfiles > getdtablesize() )
- fprintf(stderr,"Number of files, %d, exceeds, descriptor table size=%d\n"
- ,numfiles,getdtablesize());
- files=calloc(numfiles,sizeof(int));
- if ( files == NULL ) {
- perror("calloc, files:");
- exit(10);
- } else { /* open files to compare with, handles in files[] */
- int arg; /* position in argument list */
- files[0]=open( argv[1], O_RDONLY|O_NONBLOCK, 0123 /*file mode not used */ );
- if ( files[0] == -1 ) { /* files[0] == first file */
- perror("open file1:");
- exit(11);
- }
- for( i = 1,arg=2; i<numfiles ; i++,arg++ ) {
- files[i]=open(argv[arg], O_RDONLY|O_NONBLOCK, 0 );
- if ( files[i] == -1 ) {
- perror("opening files:");
- exit(arg+10);
- }
- }
- }
- buffers=calloc(numfiles,sizeof(comparebuf));
- if ( buffers == NULL ) {
- perror("calloc, buffers:");
- exit(10);
- }
- blocks_read=0;
- differences=0;
- do {
- extern int errno;
- ssize_t rr;
- bytes=0;
- {
- register size_t nbytes=sizeof(comparebuf);
- char * buffer=buffers[0];
- TryAgain:
- rr=read(files[0],buffer,nbytes);
- if ( rr == -1 ) { /* error ? */
- if ( errno == EAGAIN )
- goto TryAgain;
- perror("file1:");
- exit(11);
- } else if ( rr < nbytes && rr > 0 ) { /* short read, try again */
- bytes+= rr;
- nbytes= nbytes - rr ;
- buffer= buffer + rr ;
- goto TryAgain;
- } else /* rr == 0 || rr == nbytes, EOF or complete read */
- bytes+= rr;
- }
- if ( bytes == sizeof(comparebuf) )
- blocks_read++;
- /* at this point, bytes, would should be == to sizeof(comparebuf), except
- * for, possibly,the last read of the file, and then, of course,0 for EOF
- */
- for(i=1;i<numfiles;i++) {
- ssize_t ibytes; /* bytes read, by read for file i */
- ibytes=0;
- {
- register size_t fbytes;
- char * buffer;
- fbytes=bytes;
- if ( fbytes <= 0 )
- fbytes= sizeof(comparebuf);
- buffer=buffers[i];
- FilesTryAgain:
- rr=read(files[i],buffer, fbytes);
- if ( rr == -1 && errno == EAGAIN )
- goto FilesTryAgain;
- else if ( rr == -1 ) {
- fprintf(stderr,"Error file %d %lld blocks read.\n",i+1,
- blocks_read);
- perror("read files:");
- exit(i+11);
- } else if ( rr < fbytes && rr > 0) {
- /* short read, try to read the rest */
- ibytes+= rr;
- fbytes= fbytes - rr;
- buffer= buffer + rr;
- goto FilesTryAgain;
- } else
- ibytes+= rr;
- }
- if ( ( bytes == 0 && ibytes > 0 )
- || ( bytes != 0 && ( ibytes > bytes ))) {
- printf("File %d too long.\n",i+1);
- differences++;
- continue;
- } else if ( ibytes < bytes ) {
- printf("File %d EOF, block %lld.\n",i+1,blocks_read);
- differences++;
- } else if (memcmp(buffers[0], buffers[i],bytes) ) {
- printf("File %d differs, block %lld.\n",i+1,blocks_read);
- differences++;
- }
- } /* end for loop */
- #if 1
- if ( blocks_read % 128 == 0 ) {
- register int g;
- g = blocks_read / 1024 ;
- printf(" Thru Block %lld, GB %i.\n",blocks_read,g);
- }
- #endif
- } while ( bytes > 0 );
- printf("%lld blocks read, %lld differences.\n",blocks_read,differences);
- free(buffers);
- for(i=0;i<numfiles;i++)
- close(files[i]);
- free(files);
- return (differences != 0 ); /* so, == 0, returns 0 */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement