summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xfileutilbin17512 -> 0 bytes
-rw-r--r--fileutil.c98
-rw-r--r--fileutil.h18
3 files changed, 88 insertions, 28 deletions
diff --git a/fileutil b/fileutil
deleted file mode 100755
index 0cab2a8..0000000
--- a/fileutil
+++ /dev/null
Binary files differ
diff --git a/fileutil.c b/fileutil.c
index f530dfa..c0fc7a8 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -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;
}
diff --git a/fileutil.h b/fileutil.h
index 138f01d..676d3f0 100644
--- a/fileutil.h
+++ b/fileutil.h
@@ -1,12 +1,28 @@
#ifndef _FILEUTIL_H
#define _FILEUTIL_H
+#define FILE_BUF_SIZE 1024
+#define DEFAULT_READ_PATH "logfile.txt"
+#define NEW_FILE_PERMS 0664
+#define MIN_NUM_OF_ARGS 1
+#define MAX_NUM_OF_ARGS 6
+
+typedef enum {false, true} bool;
+typedef struct arguments_t {
+ bool valid;
+ bool move_flag;
+ bool force_flag;
+ bool dir_flag;
+ int src_path_index;
+ int dir_path_index;
+} arguments_t;
+
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);
+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);
int main(int argc, char *argv[]);