Finalisation de l'exo 2
This commit is contained in:
46
TP2/Exo2/exo2.c
Normal file
46
TP2/Exo2/exo2.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BLOCK_SIZE 1
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fin,
|
||||
fout;
|
||||
char buf[BLOCK_SIZE];
|
||||
struct stat st;
|
||||
|
||||
assert( argc == 3 );
|
||||
|
||||
fin = open(argv[1],O_RDONLY);
|
||||
assert( fin >= 0 );
|
||||
|
||||
fstat(fin, &st);
|
||||
|
||||
char *src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fin, 0);
|
||||
assert(src != MAP_FAILED);
|
||||
|
||||
fout = open(argv[2],O_CREAT|O_RDWR|O_TRUNC,0600);
|
||||
assert( fout >= 0 );
|
||||
|
||||
ftruncate(fout, st.st_size);
|
||||
|
||||
char *dst = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fout, 0);
|
||||
assert(dst != MAP_FAILED);
|
||||
|
||||
memcpy(dst, src, st.st_size);
|
||||
|
||||
munmap(src, st.st_size);
|
||||
munmap(dst, st.st_size);
|
||||
|
||||
close(fin);
|
||||
close(fout);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user