summaryrefslogtreecommitdiff
path: root/fileutil.c
diff options
context:
space:
mode:
Diffstat (limited to 'fileutil.c')
-rw-r--r--fileutil.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/fileutil.c b/fileutil.c
index b35fd76..346f3c5 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <sys/file.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include "fileutil.h"
#include "iohelper.h"
@@ -103,8 +104,6 @@ void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite){
}
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;
@@ -117,7 +116,6 @@ void validate_arguments(arguments_t *args, int argc, char **argv) {
// Combine all conditions for a valid set of arguments
args->valid =
- valid_source &&
correct_flags &&
correct_directory &&
(argc >= MIN_NUM_OF_ARGS) &&
@@ -143,16 +141,24 @@ arguments_t parse_arguments(int argc, char **argv){
// Go through each argument, find the defined flags and store then in the struct
for (int i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-d") == 0) {
+ if (strcmp(argv[i], "-d") == 0) { // Set -d flag
args.dir_flag = true;
args.dir_index = i+1;
- } else if (strcmp(argv[i], "-M") == 0) {
+ } else if (strcmp(argv[i], "-M") == 0) { // Set -M flag
args.move_flag = true;
- } else if (strcmp(argv[i], "-F") == 0) {
+ } else if (strcmp(argv[i], "-F") == 0) { // Set -F flag
args.force_flag = true;
+ } else if (is_a_path(argv[i])) {
+ if (i == 1) args.src_index = 1; // Source path is provided
+ } else {
+ // Exit with error if unknown flag or invalid path found
+ to_stderr("Unrecognised argument (not a valid flag or path) provided.\n");
+ exit(1);
}
}
- args.src_index = (argc > 1) ? 1 : -1;
+
+
+ // if (argc > 1 && is_a_path(argv[1])) args.src_index = 1;
validate_arguments(&args, argc, argv);
return args;