Get directories' name from argv

This commit is contained in:
Alessandro Ferro 2023-04-13 23:48:16 +02:00
parent 0159a89394
commit a92726f026
3 changed files with 65 additions and 35 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
dircomp

2
build.sh Normal file
View File

@ -0,0 +1,2 @@
#! /bin/bash
gcc dircomp.c -lssl -lcrypto -o dircomp

View File

@ -11,6 +11,8 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#include <unistd.h> #include <unistd.h>
#include <stdbool.h> #include <stdbool.h>
#include <dirent.h> #include <dirent.h>
#include <openssl/sha.h>
#include <string.h>
struct arguments{ struct arguments{
char* directory1; char* directory1;
@ -24,7 +26,7 @@ struct arguments{
struct arguments get_arguments(int, char**); struct arguments get_arguments(int, char**);
void print_help(void); void print_help(void);
void print_files_in_directory(struct arguments); void print_files_in_directory(char*, char*, struct arguments*);
int main(int argc, char* argv[]){ int main(int argc, char* argv[]){
struct arguments arguments = get_arguments(argc, argv); struct arguments arguments = get_arguments(argc, argv);
@ -34,9 +36,7 @@ int main(int argc, char* argv[]){
return 0; return 0;
} }
arguments.directory1 = "/home/user"; print_files_in_directory(arguments.directory1, "", &arguments); // pass pointer to reduce memory usage
arguments.directory2 = "/home/user";
print_files_in_directory(arguments);
return 0; return 0;
} }
@ -63,9 +63,66 @@ struct arguments get_arguments(int argc, char** argv){
break; break;
} }
} }
// Get directories
if( (argc - optind) < 2){
fprintf (stderr, "Not enough directories.\n");
exit(-1);
}
else if( (argc - optind) > 2){
fprintf (stderr, "Too many directories.\n");
exit(-1);
}
provided_arguments.directory1 = malloc( (strlen(argv[optind]) * sizeof(char)) + 1);
strcpy(provided_arguments.directory1, argv[optind]);
provided_arguments.directory2 = malloc( (strlen(argv[optind + 1]) * sizeof(char)) + 1);
strcpy(provided_arguments.directory2, argv[optind + 1]);
return provided_arguments; return provided_arguments;
} }
/*
* References:
* https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
*/
void print_files_in_directory(char* directory1, char* directory2, struct arguments* arguments){ // testing only
printf("\nAnalyzing directory %s\n", directory1);
DIR *d;
struct dirent *dir;
d = opendir(directory1);
if (d) {
while ((dir = readdir(d)) != NULL) {
// is file
if (dir -> d_type == DT_REG)
{
if(arguments -> v == true)
printf("Analyzing file: %s\n", dir -> d_name);
}
// is directory
if (dir -> d_type == DT_DIR)
{
if(arguments -> r == true)
print_files_in_directory(dir -> d_name, "", arguments);
}
}
closedir(d);
}
}
void sha1(char* string){
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(string, strlen(string), hash);
int i;
for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
printf("%02x", hash[i]);
putchar('\n');
}
void print_help(void){ void print_help(void){
printf("Usage: dircomp directory1 directory2 [-r] [-n] [-s] [-v] [-h]\n\n"); printf("Usage: dircomp directory1 directory2 [-r] [-n] [-s] [-v] [-h]\n\n");
printf(" -r \t\t Recursive\n"); printf(" -r \t\t Recursive\n");
@ -74,34 +131,3 @@ void print_help(void){
printf(" -v \t\t Verbose\n"); printf(" -v \t\t Verbose\n");
printf(" -h \t\t Print this help and quit\n\n"); printf(" -h \t\t Print this help and quit\n\n");
} }
/*
* References:
* https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
*/
void print_files_in_directory(struct arguments arguments){ // testing only
printf("\nAnalyzing directory %s\n", arguments.directory1);
DIR *d;
struct dirent *dir;
d = opendir(arguments.directory1);
if (d) {
while ((dir = readdir(d)) != NULL) {
// is file
if (dir -> d_type == DT_REG)
{
if(arguments.v == true)
printf("Analyzing file: %s\n", dir -> d_name);
}
// is directory
if (dir -> d_type == DT_DIR)
{
if(arguments.r == true)
print_files_in_directory(dir -> d_name, arguments); // todo, pass the pointer to reduce memory usage
}
}
closedir(d);
}
}