Update dircomp.c

Fixes #1
This commit is contained in:
Alessandro Ferro 2023-04-16 16:35:03 +02:00
parent 0f7d9b3e86
commit 4adc7435a5
1 changed files with 14 additions and 3 deletions

View File

@ -84,8 +84,8 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
bool is_directory_equal = true; bool is_directory_equal = true;
int stat_result, file_equality_result; int stat_result, file_equality_result;
char fullpath_file_helper[512]; char* fullpath_file_helper;
char fullpath_file_helper2[512]; char* fullpath_file_helper2;
char* subdirectory1; char* subdirectory1;
char* subdirectory2; char* subdirectory2;
struct dirent *element; struct dirent *element;
@ -100,6 +100,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
if (element->d_type == DT_REG) if (element->d_type == DT_REG)
{ {
// Check wether it exists in directory2 // Check wether it exists in directory2
fullpath_file_helper = malloc(sizeof(char) * (strlen(directory_to_analyze_2) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper, directory_to_analyze_2); strcpy(fullpath_file_helper, directory_to_analyze_2);
strcat(fullpath_file_helper, "/"); strcat(fullpath_file_helper, "/");
strcat(fullpath_file_helper, element->d_name); strcat(fullpath_file_helper, element->d_name);
@ -115,9 +116,11 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
// Check if the files are the same // Check if the files are the same
else else
{ {
fullpath_file_helper = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper, directory_to_analyze_1); strcpy(fullpath_file_helper, directory_to_analyze_1);
strcat(fullpath_file_helper, "/"); strcat(fullpath_file_helper, "/");
strcat(fullpath_file_helper, element->d_name); strcat(fullpath_file_helper, element->d_name);
fullpath_file_helper2 = malloc(sizeof(char) * (strlen(directory_to_analyze_2) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper2, directory_to_analyze_2); strcpy(fullpath_file_helper2, directory_to_analyze_2);
strcat(fullpath_file_helper2, "/"); strcat(fullpath_file_helper2, "/");
strcat(fullpath_file_helper2, element->d_name); strcat(fullpath_file_helper2, element->d_name);
@ -139,6 +142,8 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
} }
} }
} }
free(fullpath_file_helper);
free(fullpath_file_helper2);
} }
// Is directory // Is directory
@ -149,6 +154,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
continue; continue;
} }
// Check wether a folder with the same name exists in directory2 // Check wether a folder with the same name exists in directory2
fullpath_file_helper = malloc(sizeof(char) * (strlen(directory_to_analyze_2) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper, directory_to_analyze_2); strcpy(fullpath_file_helper, directory_to_analyze_2);
strcat(fullpath_file_helper, "/"); strcat(fullpath_file_helper, "/");
strcat(fullpath_file_helper, element->d_name); strcat(fullpath_file_helper, element->d_name);
@ -156,6 +162,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
struct stat *dummy_structure = malloc(sizeof(struct stat)); struct stat *dummy_structure = malloc(sizeof(struct stat));
stat_result = stat(fullpath_file_helper, dummy_structure); stat_result = stat(fullpath_file_helper, dummy_structure);
free(dummy_structure); free(dummy_structure);
free(fullpath_file_helper);
if (stat_result == -1) // directory does not exist if (stat_result == -1) // directory does not exist
{ {
is_directory_equal = false; is_directory_equal = false;
@ -195,6 +202,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
if (element->d_type == DT_REG) if (element->d_type == DT_REG)
{ {
// Check wether it exists in directory1 // Check wether it exists in directory1
fullpath_file_helper = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper, directory_to_analyze_1); strcpy(fullpath_file_helper, directory_to_analyze_1);
strcat(fullpath_file_helper, "/"); strcat(fullpath_file_helper, "/");
strcat(fullpath_file_helper, element->d_name); strcat(fullpath_file_helper, element->d_name);
@ -206,12 +214,14 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
, directory_to_analyze_2 , directory_to_analyze_2
, directory_to_analyze_1); , directory_to_analyze_1);
} }
free(fullpath_file_helper);
} }
// Is directory // Is directory
else if (element->d_type == DT_DIR) else if (element->d_type == DT_DIR)
{ {
// Check wether a folder with the same name exists in directory1 // Check wether a folder with the same name exists in directory1
fullpath_file_helper = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2) );
strcpy(fullpath_file_helper, directory_to_analyze_1); strcpy(fullpath_file_helper, directory_to_analyze_1);
strcat(fullpath_file_helper, "/"); strcat(fullpath_file_helper, "/");
strcat(fullpath_file_helper, element->d_name); strcat(fullpath_file_helper, element->d_name);
@ -224,6 +234,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
, directory_to_analyze_2 , directory_to_analyze_2
, directory_to_analyze_1); , directory_to_analyze_1);
} }
free(fullpath_file_helper);
} }
} }
closedir(directory); closedir(directory);