tkt
This commit is contained in:
BIN
SCR3.1/TP2/Ex4/Comparaison
Executable file
BIN
SCR3.1/TP2/Ex4/Comparaison
Executable file
Binary file not shown.
74
SCR3.1/TP2/Ex4/Comparaison.c
Normal file
74
SCR3.1/TP2/Ex4/Comparaison.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#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;
|
||||
}
|
||||
BIN
SCR3.1/TP2/Ex4/test.data
Normal file
BIN
SCR3.1/TP2/Ex4/test.data
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/Ex4/test.data2
Normal file
BIN
SCR3.1/TP2/Ex4/test.data2
Normal file
Binary file not shown.
Reference in New Issue
Block a user