diff options
Diffstat (limited to 'fileutil.c')
| -rw-r--r-- | fileutil.c | 63 |
1 files changed, 8 insertions, 55 deletions
@@ -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); |
