summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakiyamn2020-09-04 17:22:54 +1000
committerakiyamn2020-09-04 17:22:54 +1000
commit635be7a4fa2c172a8aeffa788e8cf172f10831dc (patch)
treee1d19bab5b718da2b748a7478d50407e4e54e921
parent6da84c22429d4334dec575f6dd302594c33f303d (diff)
downloadfit2100_ass1-635be7a4fa2c172a8aeffa788e8cf172f10831dc.tar.gz
fit2100_ass1-635be7a4fa2c172a8aeffa788e8cf172f10831dc.zip
Fixed creation of file
-rwxr-xr-xfileutilbin0 -> 17512 bytes
-rw-r--r--fileutil.c20
-rw-r--r--fileutil.h5
3 files changed, 18 insertions, 7 deletions
diff --git a/fileutil b/fileutil
new file mode 100755
index 0000000..0cab2a8
--- /dev/null
+++ b/fileutil
Binary files differ
diff --git a/fileutil.c b/fileutil.c
index ec446b7..f530dfa 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -16,10 +16,10 @@
#include <sys/file.h>
#include <stdlib.h>
#include <string.h>
-#include <stdio.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.
@@ -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;
}
diff --git a/fileutil.h b/fileutil.h
index 06b43f8..138f01d 100644
--- a/fileutil.h
+++ b/fileutil.h
@@ -3,7 +3,12 @@
void to_stdout(char *string);
void to_stderr(char *string);
+int open_file(char *path, int flags);
+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, int overwrite);
+char* get_filename(char *full_path);
+void copy_into_dir(char *file_path, char *dir_path);
int main(int argc, char *argv[]);
#endif \ No newline at end of file