Advertisement
Guest User

A test for map

a guest
Oct 6th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <sys/stat.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6.  
  7. int copy_file (const char *in, const char *out)
  8. {
  9.     int copy_ok = 1;
  10.     int fin = open (in, O_RDONLY);
  11.     if (fin == -1) goto ret;
  12.     int fout = open (out, O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE);
  13.     if (fout == -1) goto close_in;
  14.  
  15. #define BLOCK_SIZE 10
  16.     uint8_t buf[BLOCK_SIZE];
  17.     ssize_t bytes_read, bytes_written;
  18.     do
  19.     {
  20.         bytes_read = read (fin, buf, sizeof(buf));
  21.         if (bytes_read < 0) {copy_ok = 0; goto close_out;}
  22.         bytes_written = write (fout, buf, bytes_read);
  23.         if (bytes_written != bytes_read) {copy_ok = 0; goto close_out;}
  24.     }
  25.     while (bytes_read != 0);
  26.  
  27. close_out:
  28.     close (fout);
  29. close_in: ;
  30.     close (fin);
  31. ret:
  32.     return ((fin != -1) && (fout != -1) && copy_ok) ? 0 : -1;
  33. }
  34.  
  35. int main ()
  36. {
  37.     int err;
  38.     err = copy_file ("/proc/curproc/map", "map");
  39.     printf ("%i\n", err);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement