Added option -f (fast)

This commit is contained in:
Alessandro Ferro 2023-04-16 17:02:23 +02:00
parent 4adc7435a5
commit 2eefe61e18
2 changed files with 52 additions and 10 deletions

View File

@ -37,13 +37,13 @@ int main(int argc, char *argv[])
struct arguments get_arguments(int argc, char **argv) struct arguments get_arguments(int argc, char **argv)
{ {
struct arguments provided_arguments = {"", "", false, false, false}; struct arguments provided_arguments = {"", "", false, false, false, false};
if(argc == 1){ if(argc == 1){
provided_arguments.h = true; provided_arguments.h = true;
return provided_arguments; return provided_arguments;
} }
char option; char option;
while ((option = getopt(argc, argv, "rnsvh")) != -1) while ((option = getopt(argc, argv, "rvhf")) != -1)
{ {
switch (option) switch (option)
{ {
@ -56,6 +56,9 @@ struct arguments get_arguments(int argc, char **argv)
case 'h': case 'h':
provided_arguments.h = true; provided_arguments.h = true;
break; break;
case 'f':
provided_arguments.f = true;
break;
} }
} }
@ -79,9 +82,6 @@ struct arguments get_arguments(int argc, char **argv)
bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyze_2, struct arguments *arguments) bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyze_2, struct arguments *arguments)
{ {
if (arguments->v == true)
printf("\nAnalyzing directories %s %s\n", directory_to_analyze_1, directory_to_analyze_2);
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; char* fullpath_file_helper;
@ -111,6 +111,12 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
printf("File %s exists in %s but does not in %s\n" , element->d_name printf("File %s exists in %s but does not in %s\n" , element->d_name
, directory_to_analyze_1 , directory_to_analyze_1
, directory_to_analyze_2); , directory_to_analyze_2);
if( arguments->f == true ){
free(fullpath_file_helper);
free(directory_to_analyze_1);
free(directory_to_analyze_2);
return false;
}
} }
// Check if the files are the same // Check if the files are the same
@ -136,10 +142,19 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
} }
else if(file_equality_result == -1) else if(file_equality_result == -1)
{ {
printf("Error while comparing file %s in the directories %s, %s\n", element->d_name printf("ERROR while comparing file %s in the directories %s, %s\n", element->d_name
, directory_to_analyze_1 , directory_to_analyze_1
, directory_to_analyze_2); , directory_to_analyze_2);
} }
if( arguments->f == true )
{
free( fullpath_file_helper );
free( fullpath_file_helper2 );
free( directory_to_analyze_1 );
free( directory_to_analyze_2 );
return false;
}
} }
} }
free(fullpath_file_helper); free(fullpath_file_helper);
@ -170,13 +185,19 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name
, directory_to_analyze_1 , directory_to_analyze_1
, directory_to_analyze_2); , directory_to_analyze_2);
if( arguments->f == true )
{
free( fullpath_file_helper );
free( directory_to_analyze_1 );
free( directory_to_analyze_2 );
return false;
}
} }
// Analyze recursively // Analyze recursively
else else
{ {
if (arguments->r == true) if (arguments->r == true)
{ {
subdirectory1 = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2)); subdirectory1 = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2));
strcpy(subdirectory1, directory_to_analyze_1); strcpy(subdirectory1, directory_to_analyze_1);
strcat(subdirectory1, "/"); strcat(subdirectory1, "/");
@ -186,6 +207,11 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
strcat(subdirectory2, "/"); strcat(subdirectory2, "/");
strcat(subdirectory2, element->d_name); strcat(subdirectory2, element->d_name);
is_directory_equal = analyze_directories(subdirectory1, subdirectory2, arguments) && is_directory_equal; is_directory_equal = analyze_directories(subdirectory1, subdirectory2, arguments) && is_directory_equal;
// Interrupt recursion if -f option is set
if(arguments->f == true && is_directory_equal == false){
break;
}
} }
} }
} }
@ -213,6 +239,13 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
printf("File %s exists in %s but does not in %s\n" , element->d_name printf("File %s exists in %s but does not in %s\n" , element->d_name
, directory_to_analyze_2 , directory_to_analyze_2
, directory_to_analyze_1); , directory_to_analyze_1);
if( arguments->f == true )
{
free( fullpath_file_helper );
free( directory_to_analyze_1 );
free( directory_to_analyze_2 );
return false;
}
} }
free(fullpath_file_helper); free(fullpath_file_helper);
} }
@ -233,6 +266,13 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name
, directory_to_analyze_2 , directory_to_analyze_2
, directory_to_analyze_1); , directory_to_analyze_1);
if( arguments->f == true )
{
free( fullpath_file_helper );
free( directory_to_analyze_1 );
free( directory_to_analyze_2 );
return false;
}
} }
free(fullpath_file_helper); free(fullpath_file_helper);
} }
@ -287,8 +327,9 @@ int are_files_equal(char* filename1, char* filename2){
void print_help(void) void print_help(void)
{ {
printf("usage: dircomp directory1 directory2 [-r] [-v] [-h]\n"); printf("usage: dircomp directory1 directory2 [-rvfh]\n");
printf(" -r \t\t Recursive\n"); printf(" -r \t\t Recursive\n");
printf(" -v \t\t Verbose\n"); printf(" -v \t\t Verbose\n");
printf(" -f \t\t Fast. Halt as soon as the directories are found to be not equal\n");
printf(" -h \t\t Print this help and quit\n"); printf(" -h \t\t Print this help and quit\n");
} }

View File

@ -25,6 +25,7 @@ struct arguments{
bool r; // recursive bool r; // recursive
bool v; // verbose bool v; // verbose
bool h; // help bool h; // help
bool f; // fast
}; };
struct arguments get_arguments(int, char**); struct arguments get_arguments(int, char**);