This commit is contained in:
Ben Harris 2016-10-02 15:35:02 -04:00
parent 9215caf9c5
commit 6864133d52
6 changed files with 50 additions and 33 deletions

BIN
mmapcp Normal file

Binary file not shown.

View File

@ -8,37 +8,52 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int s, d; int s, s2, d;
struct stat st; struct stat st;
void *sp, *dp; struct stat s2t;
s = open(argv[1], O_RDONLY); void *sp, *s2p, *dp;
if (s == -1) { s = open(argv[1], O_RDONLY);
perror("open source"); if (s == -1) {
exit(1); perror("open source");
} exit(1);
d = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644); }
if (d == -1) { s2 = open(argv[2], O_RDONLY);
perror("open destintation"); if (s2 == -1) {
exit(1); perror("open source2");
} exit(1);
if (fstat(s, &st)) { }
perror("stat source"); d = open(argv[3], O_RDWR | O_CREAT | O_TRUNC, 0644);
exit(1); if (d == -1) {
} perror("open destination");
if (ftruncate(d, st.st_size)) { exit(1);
perror("truncate destination"); }
exit(1); if (fstat(s, &st)) {
} perror("stat source");
sp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, s, 0); exit(1);
if (sp == MAP_FAILED) { }
perror("map source"); if (fstat(s2, &s2t)) {
exit(1); perror("stat source2");
} exit(1);
dp = mmap(NULL, st.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, d, 0); }
if (dp == MAP_FAILED) { if (ftruncate(d, st.st_size + s2t.st_size)) {
perror("map destintation"); perror("truncate destination");
exit(1); exit(1);
} }
memcpy(dp, sp, st.st_size); sp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, s, 0);
return 0; if (sp == MAP_FAILED) {
perror("map source");
exit(1);
}
s2p = mmap(NULL, s2t.st_size, PROT_READ, MAP_SHARED, s2, 0);
if (s2p == MAP_FAILED) {
perror("map source2");
exit(1);
}
dp = mmap(NULL, st.st_size + s2t.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, d, 0);
if (dp == MAP_FAILED) {
perror("map destination");
exit(1);
}
memcpy(dp, sp, st.st_size);
return 0;
} }

1
mymom Normal file
View File

@ -0,0 +1 @@
mymom

BIN
out Normal file

Binary file not shown.

BIN
out.txt Normal file

Binary file not shown.

1
urmom Normal file
View File

@ -0,0 +1 @@
urmom