|
13 | 13 | #include "../include/tuwi.h"
|
14 | 14 |
|
15 | 15 | #include <netlibc/fs.h>
|
| 16 | +#include <netlibc/log.h> |
16 | 17 |
|
17 | 18 | #include <fcntl.h>
|
18 | 19 | #include <pthread.h>
|
|
25 | 26 | #include <unistd.h>
|
26 | 27 |
|
27 | 28 | bool __files_equal(char *first_file, char *second_file) {
|
28 |
| - char command1[1024]; // Buffer for the first checksum command |
29 |
| - char command2[1024]; // Buffer for the second checksum command |
| 29 | + FILE *file1 = fopen(first_file, "rb"); |
| 30 | + FILE *file2 = fopen(second_file, "rb"); |
30 | 31 |
|
31 |
| - // Create the shell command to calculate the checksum of file1 |
32 |
| - snprintf(command1, sizeof(command1), "md5sum \"%s\" 2>/dev/null", first_file); |
| 32 | + if (file1 == NULL || file2 == NULL) { |
| 33 | + perror("Error opening file"); |
| 34 | + PANIC("could not open files"); |
| 35 | + } |
| 36 | + |
| 37 | + int byte1; |
| 38 | + int byte2; |
| 39 | + bool equal = true; |
33 | 40 |
|
34 |
| - // Create the shell command to calculate the checksum of file2 |
35 |
| - snprintf(command2, sizeof(command2), "md5sum \"%s\" 2>/dev/null", |
36 |
| - second_file); |
| 41 | + while (1) { |
| 42 | + byte1 = fgetc(file1); |
| 43 | + byte2 = fgetc(file2); |
37 | 44 |
|
38 |
| - // Execute the shell commands using popen and capture their output |
39 |
| - FILE *fp1 = popen(command1, "r"); |
40 |
| - FILE *fp2 = popen(command2, "r"); |
| 45 | + if (byte1 == EOF || byte2 == EOF) { |
| 46 | + break; |
| 47 | + } |
41 | 48 |
|
42 |
| - if (fp1 == NULL || fp2 == NULL) { |
43 |
| - perror("popen"); |
44 |
| - exit(EXIT_FAILURE); |
| 49 | + if (byte1 != byte2) { |
| 50 | + equal = false; |
| 51 | + break; |
| 52 | + } |
45 | 53 | }
|
46 | 54 |
|
47 |
| - char checksum1[32], checksum2[32]; // Buffer to store checksums |
48 |
| - fgets(checksum1, sizeof(checksum1), fp1); |
49 |
| - fgets(checksum2, sizeof(checksum2), fp2); |
| 55 | + // Check if both files have reached EOF at the same time |
| 56 | + if (byte1 != EOF || byte2 != EOF) { |
| 57 | + LOG(INFO, "THIS!"); |
| 58 | + equal = false; |
| 59 | + } |
50 | 60 |
|
51 |
| - // Close the file pointers |
52 |
| - pclose(fp1); |
53 |
| - pclose(fp2); |
| 61 | + fclose(file1); |
| 62 | + fclose(file2); |
54 | 63 |
|
55 |
| - // Compare the checksums |
56 |
| - if (strcmp(checksum1, checksum2) == 0) { |
57 |
| - return true; // Checksums match |
58 |
| - } else { |
59 |
| - return false; // Checksums do not match |
60 |
| - } |
| 64 | + return equal; |
61 | 65 | }
|
62 | 66 |
|
63 | 67 | char *__read_file(char *path) {
|
|
0 commit comments