diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | fileutil.c | 95 |
2 files changed, 82 insertions, 16 deletions
@@ -6,6 +6,9 @@ CFLAGS = -Wall -c all: $(OBJS) $(CC) $(OBJS) -o $(OUTPUT) +debug: $(OBJS) + $(CC) -g $(OBJS) -o $(OUTPUT) + fileutil.o: fileutil.c fileutil.h $(CC) $(CFLAGS) fileutil.c @@ -4,7 +4,7 @@ * Alexander Occhipinti * Student ID: 29994705 * Created: 3 Sep 2020 - * Last Modified: 3 Sep 2020 + * Last Modified: 4 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. @@ -16,8 +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" /* * Prints a given string to stdout. Returns nothing. @@ -37,30 +39,91 @@ void to_stderr(char *string) { * Prints the contents of a given file (provided a path) to stdout. * Returns nothing. */ -void print_file(char *read_path) { - int infile, bytes_read; - // Read the input file - if ((infile = open(read_path, O_RDONLY)) <= 2){ - to_stderr(read_path); - to_stderr(" could not be opened for reading.\n"); +int open_file(char *path, int flags){ + // Read the input file + int fd; + if ((fd = open(path, flags)) <= 2){ + to_stderr(path); + to_stderr(" could not be opened.\n"); exit(1); // Exit if an error occurs } - - // Write contents to stdout using a buffer + return fd; +} + +void file_into_file(int source_fd, int dest_fd){ + int bytes_read; char buffer[FILE_BUF_SIZE]; - while ((bytes_read = read(infile, buffer, FILE_BUF_SIZE))) { - write(1, buffer, bytes_read); + while ((bytes_read = read(source_fd, buffer, FILE_BUF_SIZE))) { + write(dest_fd, buffer, bytes_read); } - close(infile); } +void print_file(char *read_path) { + int read_fd; + read_fd = open_file(read_path, O_RDONLY); + file_into_file(read_fd, 1); + close(read_fd); +} + +void copy_file(char *read_path, char *write_path) { + int read_fd, write_fd; + + read_fd = open_file(read_path, O_RDONLY); + write_fd = open_file(write_path, O_WRONLY); + + file_into_file(read_fd, write_fd); + + close(read_fd); + close(write_fd); +} + +char* get_filename(char *full_path){ + char* filename = strrchr(full_path, '/'); // Find the string after the last occurence of a '/' + if (!filename) return full_path; // If there are no slashes, the whole path is already a filename + return ++filename; // Return the filename without a leading slash + +} + +void copy_into_dir(char *file_path, char *dir_path){ + char* filename; + char* new_path; + size_t new_path_len; + + // filename = strrchr(dir_path, '/'); + filename = get_filename(file_path); + // printf("%s\n", filename); + new_path_len = strlen(dir_path) + strlen(filename) + 1; + new_path = (char *) malloc(new_path_len); + + strcpy(new_path, dir_path); + strcat(new_path, "/"); + strcat(new_path, filename); + printf("%s", new_path); + copy_file(file_path, new_path); + + free(new_path); +} + + + int main(int argc, char *argv[]) { - if (argc >= 2) { - print_file(argv[1]); - } else { - print_file("logfile.txt"); + 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: + copy_into_dir(argv[1], argv[3]); } return 0; } |
