#include #include #include #include #include #include #include #include #include #include #include main( int argc, char **argv ) { int c, fd1, fd2, count, count2; char *source, *dest, *buffer, *buffer2; struct timespec ts; struct utimbuf ut; struct stat st; long blocksize = 64*1024; long nano = 30000000; long rate = 1024; /* 64k every .06 seconds gives a rate of approximately 1M/s */ int sync = 0; time_t start, end; long long total = 0; while( (c = getopt( argc, argv, "b:n:r:s" ) ) != -1 ) switch(c) { case 'b': blocksize = atoi(optarg) * 1024; break; case 'n': nano = atoi(optarg); break; case 'r': rate = atoi(optarg); break; case 's': sync++; break; default: fprintf(stderr, "%s: Bad arg %c\n", argv[0], c ); exit(1); } source = argv[optind++]; dest = argv[optind++]; ts.tv_sec = 0; ts.tv_nsec = nano; if( (fd1 = open( source, O_RDONLY )) < 0 ) { fprintf(stderr, "%s: Can't open source %s: %s\n", argv[0], source, strerror(errno) ); exit(1); } #ifdef COMPARE #define FD2MODE O_RDONLY #else #define FD2MODE O_WRONLY|O_CREAT|O_TRUNC #endif if( (fd2 = open( dest, FD2MODE, 0666 )) < 0 ) { fprintf(stderr, "%s: Can't open dest %s: %s\n", argv[0], dest, strerror(errno) ); exit(1); } if( fstat(fd1, &st) != 0 ) { fprintf( stderr, "%s: Can't stat source %s: %s\n", argv[0], source, strerror(errno)); ut.actime = 0; ut.modtime = 0; } else { ut.actime = st.st_atime; ut.modtime = st.st_mtime; } time(&start); if( (buffer = malloc(blocksize)) == NULL ) { fprintf( stderr, "%s: Can't malloc mem: %s\n", argv[0], strerror(errno)); exit(1); } #ifdef COMPARE if( (buffer2 = malloc(blocksize)) == NULL ) { fprintf( stderr, "%s: Can't malloc mem: %s\n", argv[0], strerror(errno)); exit(1); } #endif while( (count = read( fd1, buffer, blocksize )) > 0 ) { total += count; #ifdef COMPARE count2 = read( fd2, buffer2, blocksize ); if( count != count2 ) { fprintf( stderr, "%s: Bytes read differs\n", argv[0]); exit(1); } while( count ) { if( buffer[--count] != buffer2[--count2] ) { fprintf( stderr, "%s: Contents differ\n", argv[0] ); exit(1); } } #else if( write( fd2, buffer, count ) != count ) { fprintf( stderr, "%s: Write error: %s\n", argv[0], strerror(errno) ); exit(1); } if (sync) fsync(fd2); #endif nanosleep( &ts, NULL ); } time(&end); printf( "Wrote %lld bytes in %ld seconds, %lld k/s\n", total, end - start, (total/1024)/(end-start) ); close(fd1); close(fd2); #ifndef COMPARE if( ut.modtime ) utime( dest, &ut ); #endif exit(0); }