From a55512edbe175e96a2120be569232274b27e582f Mon Sep 17 00:00:00 2001 From: akiyamn Date: Thu, 3 Sep 2020 23:33:45 +1000 Subject: Functionality of Task 1 done --- Makefile | 6 +++++- a.out | Bin 16672 -> 0 bytes fileutil | Bin 16672 -> 0 bytes fileutil.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ fileutil.h | 11 +++++++++-- fileutil.o | Bin 1848 -> 0 bytes logfile.txt | 7 ++----- 7 files changed, 65 insertions(+), 14 deletions(-) delete mode 100755 a.out delete mode 100755 fileutil delete mode 100644 fileutil.o diff --git a/Makefile b/Makefile index 4ebec02..d28e100 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,13 @@ +OUTPUT = fileutil OBJS = fileutil.o CC = gcc CFLAGS = -Wall -c all: $(OBJS) - $(CC) $(OBJS) -o fileutil + $(CC) $(OBJS) -o $(OUTPUT) fileutil.o: fileutil.c fileutil.h $(CC) $(CFLAGS) fileutil.c + +clean: + rm $(OUTPUT) $(OBJS) \ No newline at end of file diff --git a/a.out b/a.out deleted file mode 100755 index 5186573..0000000 Binary files a/a.out and /dev/null differ diff --git a/fileutil b/fileutil deleted file mode 100755 index 5186573..0000000 Binary files a/fileutil and /dev/null differ diff --git a/fileutil.c b/fileutil.c index af98a1b..266569f 100644 --- a/fileutil.c +++ b/fileutil.c @@ -1,17 +1,60 @@ +/* + * fileutil.c + * + * Alexander Occhipinti + * Student ID: 29994705 + * Created: 3 Sep 2020 + * Last Modified: 3 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. + * It also allows you to delete the original (i.e. mv) if you please. + * +*/ + #include +#include +#include #include "fileutil.h" +#define FILE_BUF_SIZE 1024 +/* + * Prints a given string to stdout. Returns nothing. +*/ +void to_stdout(char *string) { + write(1, string, sizeof(string)); +} -void to_stdout(char *string, ssize_t size) { - write(0, string, size); +/* + * Prints a given string to stderr. Returns nothing. +*/ +void to_stderr(char *string) { + write(2, string, sizeof(string)); } -void to_stderr(char *string, ssize_t size) { - write(1, string, size); +/* + * 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); + exit(1); // Exit if an error occurs + } + + // Write contents to stdout using a buffer + char buffer[FILE_BUF_SIZE]; + while ((bytes_read = read(infile, buffer, FILE_BUF_SIZE))) { + write(1, buffer, bytes_read); + } + + close(infile); } int main(int argc, char const *argv[]) { - /* code */ - to_stdout("Testing\n", 9); + print_file("logfile.txt"); return 0; } diff --git a/fileutil.h b/fileutil.h index 23658ba..88e59ae 100644 --- a/fileutil.h +++ b/fileutil.h @@ -1,2 +1,9 @@ -void to_stdout(char *string, ssize_t size); -int main(int argc, char const *argv[]); \ No newline at end of file +#ifndef _FILEUTIL_H +#define _FILEUTIL_H + +void to_stdout(char *string); +void to_stderr(char *string); +void print_file(char *read_path); +int main(int argc, char const *argv[]); + +#endif \ No newline at end of file diff --git a/fileutil.o b/fileutil.o deleted file mode 100644 index df6344a..0000000 Binary files a/fileutil.o and /dev/null differ diff --git a/logfile.txt b/logfile.txt index 49f9c6f..05aae31 100644 --- a/logfile.txt +++ b/logfile.txt @@ -1,5 +1,2 @@ -aisoyudg iasd -'asdj haksjd -a DJK AHS GH - ASKDJHA SD - sds s s sf s s s s \ No newline at end of file +12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ +nsdfggdfdgfd dfgdfgd dfgdfg dfgdgfdfg dgf dgf dfg dfg \ No newline at end of file -- cgit v1.2.3