diff options
| author | akiyamn | 2020-09-10 18:13:12 +1000 |
|---|---|---|
| committer | akiyamn | 2020-09-10 18:13:12 +1000 |
| commit | 03e396142909f633ddc201e7c948bfc71319a0d2 (patch) | |
| tree | 816ff170bb70044908fd69e99296cea2922b213b | |
| parent | cc006cf8239efd4f30925a1cefb0e408db3ffe0d (diff) | |
| download | fit2100_ass1-03e396142909f633ddc201e7c948bfc71319a0d2.tar.gz fit2100_ass1-03e396142909f633ddc201e7c948bfc71319a0d2.zip | |
Split into two files1.0.0-rc1
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | fileutil.c | 63 | ||||
| -rw-r--r-- | fileutil.h | 8 | ||||
| -rw-r--r-- | iohelper.c | 69 | ||||
| -rw-r--r-- | iohelper.h | 14 | ||||
| -rw-r--r-- | manual.txt | 23 |
6 files changed, 119 insertions, 63 deletions
@@ -1,5 +1,5 @@ OUTPUT = fileutil -OBJS = fileutil.o +OBJS = fileutil.o iohelper.o CC = gcc CFLAGS = -Wall -c @@ -12,5 +12,8 @@ debug: $(OBJS) fileutil.o: fileutil.c fileutil.h $(CC) $(CFLAGS) fileutil.c +iohelper.o: iohelper.c iohelper.h + $(CC) $(CFLAGS) iohelper.c + clean: rm $(OUTPUT) $(OBJS)
\ No newline at end of file @@ -1,11 +1,12 @@ /* * fileutil.c * - * Alexander Occhipinti + * Alexander Occhipinti * Student ID: 29994705 * Created: 3 Sep 2020 - * Last Modified: 8 Sep 2020 + * Last Modified: 10 Sep 2020 * + * This is the main source file for the fileutil program. * 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. * It also allows you to delete the original (i.e. mv) if you please. @@ -15,61 +16,12 @@ #include <unistd.h> #include <sys/file.h> #include <stdlib.h> -#include <errno.h> #include <string.h> #include "fileutil.h" +#include "iohelper.h" /* - * Prints a given string to stdout. Returns nothing. -*/ -void to_stdout(char *string) { - write(1, string, strlen(string)); -} - -/* - * Prints a given string to stderr. Returns nothing. -*/ -void to_stderr(char *string) { - write(2, string, strlen(string)); -} - - -/* - * Given a file path and open() flags, open a file - * Catches openning errors, prints an error to stderr when an error occurs and exits - * Returns a file descriptor of the open file. Needs to be closed afterwards. -*/ -int open_file(char *path, int flags){ - // Read the input file - int fd; - if ((fd = open(path, flags, NEW_FILE_PERMS)) < 0){ - int error = errno; - to_stderr(path); - to_stderr(" could not be opened: "); - to_stderr(strerror(error)); - // Exit (with 1) if an error occurs, with error 2 if the file exists and can not be overwriten - if (errno == EEXIST) exit(2) : exit(1); - } - return fd; -} - -/* - * Given a source file descriptor open for reading, - * put all of its contents in the destination file descriptor open for writing. - * - * Useful for transferring data between files or stdin/stdout/stderr -*/ -void file_into_file(int source_fd, int dest_fd){ - int bytes_read; - char buffer[FILE_BUF_SIZE]; - while ((bytes_read = read(source_fd, buffer, FILE_BUF_SIZE))) { - write(dest_fd, buffer, bytes_read); - } - -} - -/* * Prints the contents of a given file (provided a path) to stdout. * Returns nothing. */ @@ -113,10 +65,11 @@ char* get_filename(char *full_path){ } /* -* Returns true if the provided string is a UNIX style absolute path (starts with '/'), false otherwise +* Returns true if the provided string is a UNIX style absolute path (starts with '/' or '~'), false otherwise */ bool is_a_path(char* string) { - return (strlen(string) == 0) ? false : string[0] == '/'; + bool empty_string = strlen(string) == 0; + return (empty_string) ? false : string[0] == '/' || string[0] == '~'; } /* @@ -221,7 +174,7 @@ void execute_subprogram(arguments_t args, char *argv[]) { } -// Main +// Main function of the whole program int main(int argc, char *argv[]) { arguments_t args = parse_arguments(argc, argv); execute_subprogram(args, argv); @@ -1,9 +1,9 @@ +// fileutil.h + #ifndef _FILEUTIL_H #define _FILEUTIL_H -#define FILE_BUF_SIZE 1024 #define DEFAULT_READ_PATH "logfile.txt" // Default source path if one isn't specified -#define NEW_FILE_PERMS 0664 // -rw-r--r-- when files are created #define MIN_NUM_OF_ARGS 1 #define MAX_NUM_OF_ARGS 6 @@ -19,10 +19,6 @@ typedef struct arguments_t { int dir_index; // Index in the provided arguments (argv) of the directory path string } 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, bool overwrite); char* get_filename(char *full_path); diff --git a/iohelper.c b/iohelper.c new file mode 100644 index 0000000..de5ad34 --- /dev/null +++ b/iohelper.c @@ -0,0 +1,69 @@ +/* + * iohelper.c + * + * Alexander Occhipinti + * Student ID: 29994705 + * Created: 10 Sep 2020 + * Last Modified: 10 Sep 2020 + * + * iohelper is a small, simple library containing light wrapper functions to Linux system calls. + * The functions in iohelper are similar to what would normally be provided in C's stdio.h library. + * +*/ + +#include <unistd.h> +#include <sys/file.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include "iohelper.h" + +/* + * Prints a given string to stdout. Returns nothing. +*/ +void to_stdout(char *string) { + write(1, string, strlen(string)); +} + +/* + * Prints a given string to stderr. Returns nothing. +*/ +void to_stderr(char *string) { + write(2, string, strlen(string)); +} + +/* + * Given a file path and open() flags, open a file + * Catches openning errors, prints an error to stderr when an error occurs and exits + * Returns a file descriptor of the open file. Needs to be closed afterwards. +*/ +int open_file(char *path, int flags){ + // Read the input file + int fd; + if ((fd = open(path, flags, NEW_FILE_PERMS)) < 0){ + int error = errno; + to_stderr(path); + to_stderr(" could not be opened: "); + to_stderr(strerror(error)); + to_stderr("\n"); + // Exit as error 2 if the file exists and can not be overwriten + if (errno == EEXIST) exit(2); + exit(1); // Exit with 1 otherwise + } + return fd; +} + +/* + * Given a source file descriptor open for reading, + * put all of its contents in the destination file descriptor open for writing. + * + * Useful for transferring data between files or stdin/stdout/stderr +*/ +void file_into_file(int source_fd, int dest_fd){ + int bytes_read; + char buffer[FILE_BUF_SIZE]; + while ((bytes_read = read(source_fd, buffer, FILE_BUF_SIZE))) { + write(dest_fd, buffer, bytes_read); + } + +}
\ No newline at end of file diff --git a/iohelper.h b/iohelper.h new file mode 100644 index 0000000..d4c2355 --- /dev/null +++ b/iohelper.h @@ -0,0 +1,14 @@ +// iohelper.h + +#ifndef _IOHELPER_H +#define _IOHELPER_H + +#define FILE_BUF_SIZE 1024 +#define NEW_FILE_PERMS 0664 // -rw-r--r-- when files are created + +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); + +#endif
\ No newline at end of file @@ -4,16 +4,37 @@ Fileutil is a program which is a utility for files written in C. It combines the It allows you to copy or move the contents of a given file to stdout or to a another file. You can also choose whether or not to overwrite the destination file if it exists. +Please note that all paths given to this program must be absolute, UNIX style file paths. +(i.e. start with '/' or '~') + +COMPILATION: + + This program can be easily compiled using the make command. Simply run the command: + + make + + in the main directory and the executable fileutil will be created. You need to have GNU Make on your system to compile in this way. + + SYNOPSIS: + fileutil fileutil SOURCE fileutil SOURCE [OPTIONS] -d DEST + +PARAMETERS: + SOURCE The absolute path to the source file to be moved or copied. + If one isn't provided, then it is set to "logfile.txt" by default. + DEST The path to the destination directory of the copy/move operation. + If one isn't provided, it prints to stdout (i.e. the terminal output) instead. + + OPTIONS: OPTION ARGUMENT(S) DESCRIPTION -d DEST Specifies the path (DEST) of the output directory -M Move the file from its destination rather than copying it - -F Allows the program to overwrite a file if it already exists (must be used with -d)
\ No newline at end of file + -F Allows the program to overwrite a file if it already exists (must be used with -d) |
