summaryrefslogtreecommitdiff
path: root/fileutil.c
diff options
context:
space:
mode:
authorakiyamn2020-09-03 23:33:45 +1000
committerakiyamn2020-09-03 23:33:45 +1000
commita55512edbe175e96a2120be569232274b27e582f (patch)
tree584378000e0adae1ea7490fa6e95cea02d1a76a0 /fileutil.c
parent4444a375ac7f24ada2932291661d9102791604d9 (diff)
downloadfit2100_ass1-a55512edbe175e96a2120be569232274b27e582f.tar.gz
fit2100_ass1-a55512edbe175e96a2120be569232274b27e582f.zip
Functionality of Task 1 done
Diffstat (limited to 'fileutil.c')
-rw-r--r--fileutil.c55
1 files changed, 49 insertions, 6 deletions
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 <unistd.h>
+#include <sys/file.h>
+#include <stdlib.h>
#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;
}