Files
SCR/SCR3.1/TP2/Ex4/Comparaison.c
2025-09-18 14:06:06 +02:00

75 lines
1.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#define BUFFER_SIZE 4096
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]);
return 1;
}
const char *file1 = argv[1];
const char *file2 = argv[2];
struct stat stat1, stat2;
if (stat(file1, &stat1) == -1) {
perror("Erreur stat file1");
return 1;
}
if (stat(file2, &stat2) == -1) {
perror("Erreur stat file2");
return 1;
}
if (stat1.st_size != stat2.st_size) {
return 1;
}
int fd1 = open(file1, O_RDONLY);
if (fd1 == -1) {
perror("Erreur open file1");
return 1;
}
int fd2 = open(file2, O_RDONLY);
if (fd2 == -1) {
perror("Erreur open file2");
close(fd1);
return 1;
}
char buffer1[BUFFER_SIZE];
char buffer2[BUFFER_SIZE];
ssize_t bytes_read1, bytes_read2;
while ((bytes_read1 = read(fd1, buffer1, BUFFER_SIZE)) > 0) {
bytes_read2 = read(fd2, buffer2, BUFFER_SIZE);
if (bytes_read2 != bytes_read1 || memcmp(buffer1, buffer2, bytes_read1) != 0) {
close(fd1);
close(fd2);
return 1;
}
}
if (bytes_read1 == -1) {
perror("Erreur lecture file1");
close(fd1);
close(fd2);
return 1;
}
close(fd1);
close(fd2);
return 0;
}