From 9215caf9c5965b2f12b0e6412a593ade94af748b Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 29 Sep 2016 14:28:39 -0400 Subject: [PATCH] init --- mmapcp.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 mmapcp.cc diff --git a/mmapcp.cc b/mmapcp.cc new file mode 100644 index 0000000..ce0e292 --- /dev/null +++ b/mmapcp.cc @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int s, d; + struct stat st; + void *sp, *dp; + s = open(argv[1], O_RDONLY); + if (s == -1) { + perror("open source"); + exit(1); + } + d = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644); + if (d == -1) { + perror("open destintation"); + exit(1); + } + if (fstat(s, &st)) { + perror("stat source"); + exit(1); + } + if (ftruncate(d, st.st_size)) { + perror("truncate destination"); + exit(1); + } + sp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, s, 0); + if (sp == MAP_FAILED) { + perror("map source"); + exit(1); + } + dp = mmap(NULL, st.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, d, 0); + if (dp == MAP_FAILED) { + perror("map destintation"); + exit(1); + } + memcpy(dp, sp, st.st_size); + return 0; +}