diff options
Diffstat (limited to 'fileutil.c')
| -rw-r--r-- | fileutil.c | 98 |
1 files changed, 71 insertions, 27 deletions
@@ -4,7 +4,7 @@ * Alexander Occhipinti * Student ID: 29994705 * Created: 3 Sep 2020 - * Last Modified: 4 Sep 2020 + * Last Modified: 7 Sep 2020 * * fileutil is a program which is a utility for files. It combines the functionality of cat cp and mv. * This program allows you to copy the contents of a given file to stdout or to a another file. @@ -15,11 +15,10 @@ #include <unistd.h> #include <sys/file.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> #include "fileutil.h" -#define FILE_BUF_SIZE 1024 -#define DEFAULT_READ_PATH "logfile.txt" -#define NEW_FILE_PERMS 0664 + /* * Prints a given string to stdout. Returns nothing. @@ -67,10 +66,10 @@ void print_file(char *read_path) { close(read_fd); } -void copy_file(char *read_path, char *write_path, int overwrite) { +void copy_file(char *read_path, char *write_path, bool overwrite) { int read_fd, write_fd; int write_flags = O_WRONLY | O_CREAT; - if (overwrite) write_flags |= O_EXCL; + if (!overwrite) write_flags |= O_EXCL; read_fd = open_file(read_path, O_RDONLY); write_fd = open_file(write_path, write_flags); @@ -88,6 +87,39 @@ char* get_filename(char *full_path){ } +arguments_t parse_arguments(int argc, char **argv){ + arguments_t args = { + .src_path_index = 1, + .dir_path_index = -1, + .valid = false, + .move_flag = false, + .force_flag = false, + .dir_flag = false + }; + + for (int i = 2; i < argc; i++) { + if (strcmp(argv[i], "-d") == 0) { + args.dir_flag = true; + args.dir_path_index = i+1; + } else if (strcmp(argv[i], "-M") == 0) { + args.move_flag = true; + } else if (strcmp(argv[i], "-F") == 0) { + args.force_flag = true; + } + } + + bool correct_flags = (args.move_flag || args.force_flag) ? args.dir_flag : true; + + args.valid = + correct_flags && + (args.dir_path_index < argc) && + (argc >= MIN_NUM_OF_ARGS) && + (argc <= MAX_NUM_OF_ARGS); + + return args; +} + + void copy_into_dir(char *file_path, char *dir_path){ char* filename; char* new_path; @@ -103,33 +135,45 @@ void copy_into_dir(char *file_path, char *dir_path){ strcat(new_path, "/"); strcat(new_path, filename); copy_file(file_path, new_path, 0); + to_stdout("Copy successful\n"); free(new_path); } + int main(int argc, char *argv[]) { - switch (argc){ - case 0: - exit(1); - case 1: - print_file(DEFAULT_READ_PATH); - break; - case 2: - print_file(argv[1]); - break; - case 3: - to_stderr("Invalid number of arguments sepcified\n"); - exit(1); - break; - default: - if (strcmp(argv[2], "-d") == 0){ - copy_into_dir(argv[1], argv[3]); - } else { - to_stderr("Invalid argument(s) sepcified\n"); - exit(1); - } + // switch (argc){ + // case 0: + // exit(1); + // case 1: + // print_file(DEFAULT_READ_PATH); + // break; + // case 2: + // print_file(argv[1]); + // break; + // case 3: + // to_stderr("Invalid number of arguments sepcified\n"); + // exit(1); + // break; + // default: + // if (strcmp(argv[2], "-d") == 0){ + // copy_into_dir(argv[1], argv[3]); + // } else { + // to_stderr("Invalid argument(s) sepcified\n"); + // exit(1); + // } + // } + + arguments_t args = parse_arguments(argc, argv); + if (!args.valid) { + to_stderr("nah"); + return -1; + } else { + to_stdout("yeah"); + return 0; } - return 0; + + // return 0; } |
