r305_dm/TP2/Exo2/exo2.c

46 lines
905 B
C
Raw Normal View History

2023-09-15 10:29:01 +02:00
#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;
}