diff options
| -rwxr-xr-x | fileutil | bin | 0 -> 17512 bytes | |||
| -rw-r--r-- | fileutil.c | 20 | ||||
| -rw-r--r-- | fileutil.h | 5 |
3 files changed, 18 insertions, 7 deletions
diff --git a/fileutil b/fileutil Binary files differnew file mode 100755 index 0000000..0cab2a8 --- /dev/null +++ b/fileutil @@ -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; } @@ -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 |
