|
#include <unp.h>
#include <sys/mman.h>
#ifndef MAP_FILE
#define MAP_FILE 0
#endif
int main(int argc, char *argv[])
{
int src = 0, dst = 0;
void *csrc = NULL, *cdst = NULL;
struct stat statbuff;
if (argc != 3)
{
err_quit("usage: copy <src> <dst>");
}
if ((src = open(argv[1], O_RDONLY)) < 0)
{
err_sys("open error");
}
if ((dst = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0)
{
err_sys("open error");
}
if (fstat(src, &statbuff) < 0)
{
err_sys("fstat error");
}
if (lseek(dst, statbuff.st_size - 1, SEEK_SET) == -1)
{
err_sys("lseek error");
}
if (write(dst, "", 1) != 1)
{
err_sys("write error");
}
if ((csrc = mmap((void *)0, statbuff.st_size, PROT_READ, MAP_FILE | MAP_SHARED, src, 0)) == MAP_FAILED)
{
err_sys("error for mmap src");
}
if ((cdst = mmap((void *)0, statbuff.st_size, PROT_WRITE | PROT_READ, MAP_FILE | MAP_SHARED, dst, 0)) == MAP_FAILED)
{
err_sys("error for mmap dst");
}
memcpy(cdst, csrc, statbuff.st_size);
return 0;
} |
|