Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7.  
  8. const int BLOCK_SIZE = 64;
  9.  
  10. void copyfile (int fd1, int fd2){
  11.     char buffer[BLOCK_SIZE];
  12.     int chars;
  13.     while ((chars = read(fd1, &buffer, BLOCK_SIZE)) > 0){
  14.         write(fd2, &buffer, chars);
  15.     }
  16. }
  17.  
  18. int main(int argc, char* argv[]) {
  19.   int f, f2;
  20.   struct timeval tv;
  21.   gettimeofday(&tv, NULL);
  22.   int start = tv.tv_usec;
  23.   if (argc != 3){
  24.     printf("Invallid number of arguments\n");
  25.     return 0;
  26.   }
  27.   if ((f = open(argv[1], O_RDONLY)) == -1){
  28.     printf("Invallid path to first file1\n");
  29.     return 0;
  30.   }
  31.   if((f2 = open(argv[2], O_WRONLY | O_CREAT, 0644)) == -1){
  32.     printf("Can't create file\n");
  33.     close(f);
  34.     return 0;
  35.   }
  36.   copyfile(f, f2);
  37.   close(f);
  38.   close(f2);
  39.   gettimeofday(&tv, NULL);
  40.   int end = tv.tv_usec;
  41.   double time = (double)(end - start) / 1000000;
  42.   printf("File copied in %f seconds\n", time);
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement