Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. cat cp.c
  2. #include<sys/types.h>
  3. #include<sys/stat.h>
  4. #include<fcntl.h>
  5. #include<unistd.h>
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<errno.h>
  9. #define N 1024
  10.  
  11. int main(int argc, char* argv[]) {
  12.   char buf[N];
  13.   int sf, df, size;
  14.  
  15.   if ((sf = open(argv[1], O_RDONLY)) == -1) {
  16.     perror(argv[1]);
  17.     exit(1);
  18.   }
  19.   if ((df = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0755)) == -1) {
  20.     perror(argv[2]);
  21.     close(sf);
  22.     exit(2);
  23.   }
  24.   while((size = read(sf, buf, N)) > 0) {
  25.     write(df, buf, size);
  26.   }
  27.   close(df);
  28.   close(sf);
  29.  
  30.   return 0;
  31. }
  32.  
  33.  
  34. cat cmp.c
  35. #include<sys/types.h>
  36. #include<sys/stat.h>
  37. #include<fcntl.h>
  38. #include<unistd.h>
  39. #include<stdio.h>
  40.  
  41. #define N 1024
  42.  
  43. int main(int argc, char *argv[]) {
  44.  int fd1, fd2;
  45.  char buf1[N], buf2[N];
  46.  int n1, n2, i;
  47.  
  48.  if ((fd1 = open(argv[1], O_RDONLY)) == -1) {
  49.   perror(argv[1]);
  50.   return 2;
  51.  }
  52.  if ((fd2 = open(argv[2], O_RDONLY)) == -1) {
  53.   perror(argv[2]);
  54.   return 3;
  55.  }
  56.  
  57.  while((n1 = read(fd1, buf1, N)) > 0 && (n2 = read(fd2, buf2, N)) > 0) {
  58.   if (n1 != n2) {
  59.    return 1;
  60.   }
  61.   for (i = 0; i < n1; i++) {
  62.    if (buf1[i] != buf2[i]) {
  63.     return 1;
  64.    }
  65.   }
  66.  }
  67.  
  68.  return 0;
  69.  
  70.  close(fd2);
  71.  close(fd1);
  72.  
  73.  return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement