Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- int copy_file (const char *in, const char *out)
- {
- int copy_ok = 1;
- int fin = open (in, O_RDONLY);
- if (fin == -1) goto ret;
- int fout = open (out, O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE);
- if (fout == -1) goto close_in;
- #define BLOCK_SIZE 10
- uint8_t buf[BLOCK_SIZE];
- ssize_t bytes_read, bytes_written;
- do
- {
- bytes_read = read (fin, buf, sizeof(buf));
- if (bytes_read < 0) {copy_ok = 0; goto close_out;}
- bytes_written = write (fout, buf, bytes_read);
- if (bytes_written != bytes_read) {copy_ok = 0; goto close_out;}
- }
- while (bytes_read != 0);
- close_out:
- close (fout);
- close_in: ;
- close (fin);
- ret:
- return ((fin != -1) && (fout != -1) && copy_ok) ? 0 : -1;
- }
- int main ()
- {
- int err;
- err = copy_file ("/proc/curproc/map", "map");
- printf ("%i\n", err);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement