76 lines
1.9 KiB
C
76 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 3) {
|
|
fprintf(stderr, "Usage: %s <source_file> <destination_file>\n", argv[0]);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
const char *src_path = argv[1];
|
|
const char *dst_path = argv[2];
|
|
|
|
int src_fd = open(src_path, O_RDONLY);
|
|
if (src_fd == -1) {
|
|
perror("Erreur lors de l'ouverture du fichier source");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
struct stat sb;
|
|
if (fstat(src_fd, &sb) == -1) {
|
|
perror("Erreur lors de fstat");
|
|
close(src_fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
size_t file_size = sb.st_size;
|
|
|
|
int dst_fd = open(dst_path, O_RDWR | O_CREAT | O_TRUNC, 0644);
|
|
if (dst_fd == -1) {
|
|
perror("Erreur lors de l'ouverture du fichier de destination");
|
|
close(src_fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (ftruncate(dst_fd, file_size) == -1) {
|
|
perror("Erreur lors de ftruncate");
|
|
close(src_fd);
|
|
close(dst_fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
void *src_map = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, src_fd, 0);
|
|
if (src_map == MAP_FAILED) {
|
|
perror("Erreur lors de mmap (source)");
|
|
close(src_fd);
|
|
close(dst_fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
void *dst_map = mmap(NULL, file_size, PROT_WRITE, MAP_SHARED, dst_fd, 0);
|
|
if (dst_map == MAP_FAILED) {
|
|
perror("Erreur lors de mmap (destination)");
|
|
munmap(src_map, file_size);
|
|
close(src_fd);
|
|
close(dst_fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
memcpy(dst_map, src_map, file_size);
|
|
|
|
munmap(src_map, file_size);
|
|
munmap(dst_map, file_size);
|
|
close(src_fd);
|
|
close(dst_fd);
|
|
|
|
printf("Copie réussie de '%s' vers '%s'\n", src_path, dst_path);
|
|
|
|
return 0;
|
|
}
|