summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fileutil.c37
-rw-r--r--fileutil.h2
2 files changed, 30 insertions, 9 deletions
diff --git a/fileutil.c b/fileutil.c
index c0fc7a8..24a532b 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -68,7 +68,7 @@ void print_file(char *read_path) {
void copy_file(char *read_path, char *write_path, bool overwrite) {
int read_fd, write_fd;
- int write_flags = O_WRONLY | O_CREAT;
+ int write_flags = O_WRONLY | O_CREAT | O_TRUNC;
if (!overwrite) write_flags |= O_EXCL;
read_fd = open_file(read_path, O_RDONLY);
@@ -89,7 +89,7 @@ char* get_filename(char *full_path){
arguments_t parse_arguments(int argc, char **argv){
arguments_t args = {
- .src_path_index = 1,
+ .src_path_index = -1,
.dir_path_index = -1,
.valid = false,
.move_flag = false,
@@ -97,6 +97,8 @@ arguments_t parse_arguments(int argc, char **argv){
.dir_flag = false
};
+ if (argc > 1) args.src_path_index = 1;
+
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
args.dir_flag = true;
@@ -119,8 +121,27 @@ arguments_t parse_arguments(int argc, char **argv){
return args;
}
+void execute_subprogram(arguments_t args, char *argv[]) {
+ if (!args.valid) {
+ to_stderr("Invalid arguments given.\n");
+ exit(1);
+ }
+
+ char *source_path = (args.src_path_index == -1 ) ? DEFAULT_READ_PATH : argv[args.src_path_index];
+ char *dir_path;
+
+ if (args.dir_flag){
+ dir_path = argv[args.dir_path_index];
+ copy_into_dir(source_path, dir_path, args.move_flag, args.force_flag);
+ } else {
+ print_file(source_path);
+ }
+
+
+
+}
-void copy_into_dir(char *file_path, char *dir_path){
+void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite){
char* filename;
char* new_path;
size_t new_path_len;
@@ -134,7 +155,8 @@ void copy_into_dir(char *file_path, char *dir_path){
strcpy(new_path, dir_path);
strcat(new_path, "/");
strcat(new_path, filename);
- copy_file(file_path, new_path, 0);
+ copy_file(file_path, new_path, overwrite);
+ if (move) unlink(file_path);
to_stdout("Copy successful\n");
free(new_path);
@@ -169,11 +191,10 @@ int main(int argc, char *argv[]) {
arguments_t args = parse_arguments(argc, argv);
if (!args.valid) {
to_stderr("nah");
- return -1;
+ return 1;
} else {
- to_stdout("yeah");
- return 0;
+ execute_subprogram(args, argv);
}
- // return 0;
+ return 0;
}
diff --git a/fileutil.h b/fileutil.h
index 676d3f0..d944854 100644
--- a/fileutil.h
+++ b/fileutil.h
@@ -24,7 +24,7 @@ void file_into_file(int source_fd, int dest_fd);
void print_file(char *read_path);
void copy_file(char *read_path, char *write_path, bool overwrite);
char* get_filename(char *full_path);
-void copy_into_dir(char *file_path, char *dir_path);
+void copy_into_dir(char *file_path, char *dir_path, bool move, bool overwrite);
int main(int argc, char *argv[]);
#endif \ No newline at end of file