From 635be7a4fa2c172a8aeffa788e8cf172f10831dc Mon Sep 17 00:00:00 2001 From: akiyamn Date: Fri, 4 Sep 2020 17:22:54 +1000 Subject: Fixed creation of file --- fileutil.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'fileutil.c') diff --git a/fileutil.c b/fileutil.c index ec446b7..f530dfa 100644 --- a/fileutil.c +++ b/fileutil.c @@ -16,10 +16,10 @@ #include #include #include -#include #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. @@ -43,7 +43,7 @@ void to_stderr(char *string) { int open_file(char *path, int flags){ // Read the input file int fd; - if ((fd = open(path, flags)) <= 2){ + if ((fd = open(path, flags, NEW_FILE_PERMS)) <= 2){ to_stderr(path); to_stderr(" could not be opened.\n"); exit(1); // Exit if an error occurs @@ -67,11 +67,13 @@ void print_file(char *read_path) { close(read_fd); } -void copy_file(char *read_path, char *write_path) { +void copy_file(char *read_path, char *write_path, int overwrite) { int read_fd, write_fd; + int write_flags = O_WRONLY | O_CREAT; + if (overwrite) write_flags |= O_EXCL; read_fd = open_file(read_path, O_RDONLY); - write_fd = open_file(write_path, O_WRONLY); + write_fd = open_file(write_path, write_flags); file_into_file(read_fd, write_fd); @@ -100,8 +102,7 @@ void copy_into_dir(char *file_path, char *dir_path){ strcpy(new_path, dir_path); strcat(new_path, "/"); strcat(new_path, filename); - printf("%s", new_path); - copy_file(file_path, new_path); + copy_file(file_path, new_path, 0); free(new_path); } @@ -123,7 +124,12 @@ int main(int argc, char *argv[]) { exit(1); break; default: - copy_into_dir(argv[1], argv[3]); + if (strcmp(argv[2], "-d") == 0){ + copy_into_dir(argv[1], argv[3]); + } else { + to_stderr("Invalid argument(s) sepcified\n"); + exit(1); + } } return 0; } -- cgit v1.2.3