summaryrefslogtreecommitdiff
path: root/fileutil.c
diff options
context:
space:
mode:
authorakiyamn2020-09-10 18:48:02 +1000
committerakiyamn2020-09-10 18:52:13 +1000
commit5d66cc376fd5c96fe4357a8f9eca3b5727a6eb9f (patch)
treee75c68d48057214f5a290825c8276df20ecaa5bd /fileutil.c
parent03e396142909f633ddc201e7c948bfc71319a0d2 (diff)
downloadfit2100_ass1-5d66cc376fd5c96fe4357a8f9eca3b5727a6eb9f.tar.gz
fit2100_ass1-5d66cc376fd5c96fe4357a8f9eca3b5727a6eb9f.zip
Refactored arg checking and fixed source missing bug1.0.0-rc2
Diffstat (limited to 'fileutil.c')
-rw-r--r--fileutil.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/fileutil.c b/fileutil.c
index 6906a24..92eb81f 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -73,7 +73,7 @@ bool is_a_path(char* string) {
}
/*
- * Copies a file given the path `file_path`, into the directory of path string `dir_path`
+ * Copies or moves a file given the path `file_path`, into the directory of path string `dir_path`
* Moves the file rather than copies it (i.e. remove the original file) if `move` is true
* Overwrites the target file if it exists if `overwrite` is true
*/
@@ -98,6 +98,29 @@ void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite){
free(new_path);
}
+void validate_arguments(arguments_t *args, int argc, char **argv) {
+ // Determine if the source path is either not provided or the first argument
+ bool valid_source = is_a_path(argv[args->src_index]);
+
+ // Is a valid combination of flags provided?
+ bool correct_flags = (args->move_flag || args->force_flag) ? args->dir_flag : true;
+
+ // Is the -d tag followed by a path?
+ bool correct_directory = false;
+ if (args->dir_index < argc) {
+ correct_directory = (args->dir_flag) ? is_a_path(argv[args->dir_index]) : true;
+ }
+
+ // Combine all conditions for a valid set of arguments
+ args->valid =
+ valid_source &&
+ correct_flags &&
+ correct_directory &&
+ (argc >= MIN_NUM_OF_ARGS) &&
+ (argc <= MAX_NUM_OF_ARGS);
+}
+
+
/*
* Given argc and argv (number of program arguments and the program arguments) provided from main,
* create an `arguments_t` type struct containing all of the information about the arguments.
@@ -125,28 +148,13 @@ arguments_t parse_arguments(int argc, char **argv){
args.force_flag = true;
}
}
+ args.src_index = (argc > 1) ? 1 : -1;
- // Determine if the source path is either not provided or the first argument
- if (argc > 1 && is_a_path(argv[1])) args.src_index = 1;
-
- // Is a valid combination of flags provided?
- bool correct_flags = (args.move_flag || args.force_flag) ? args.dir_flag : true;
-
- // Is the -d tag followed by a path?
- bool correct_directory = false;
- if (args.dir_index < argc) {
- correct_directory = (args.dir_flag) ? is_a_path(argv[args.dir_index]) : true;
- }
-
- // Combine all conditions for a valid set of arguments
- args.valid =
- correct_flags &&
- correct_directory &&
- (argc >= MIN_NUM_OF_ARGS) &&
- (argc <= MAX_NUM_OF_ARGS);
+ validate_arguments(&args, argc, argv);
return args;
}
+
/*
* Given an arguments_t struct of the program's arguments, execute the correct suprogram
* (i.e. move, copy or print to stdout)
@@ -169,9 +177,6 @@ void execute_subprogram(arguments_t args, char *argv[]) {
} else {
print_file(source_path); // Print a file
}
-
-
-
}
// Main function of the whole program