summaryrefslogtreecommitdiff
path: root/fileutil.c
diff options
context:
space:
mode:
authorakiyamn2020-09-08 13:24:38 +1000
committerakiyamn2020-09-08 13:24:38 +1000
commita74edeb738198cbe7945fa3682da6e7030c13ce9 (patch)
treed2fa2cce447608962522465a0bc3e83174f79b5c /fileutil.c
parentfe6110a87d782de40c5f256228ca1edcb848e4ab (diff)
downloadfit2100_ass1-a74edeb738198cbe7945fa3682da6e7030c13ce9.tar.gz
fit2100_ass1-a74edeb738198cbe7945fa3682da6e7030c13ce9.zip
Added proper error handling
Diffstat (limited to 'fileutil.c')
-rw-r--r--fileutil.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/fileutil.c b/fileutil.c
index 86c4cfc..fa1ccac 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -15,7 +15,7 @@
#include <unistd.h>
#include <sys/file.h>
#include <stdlib.h>
-#include <stdio.h>
+#include <errno.h>
#include <string.h>
#include "fileutil.h"
@@ -42,9 +42,11 @@ void to_stderr(char *string) {
int open_file(char *path, int flags){
// Read the input file
int fd;
- if ((fd = open(path, flags, NEW_FILE_PERMS)) <= 2){
+ if ((fd = open(path, flags, NEW_FILE_PERMS)) < 0){
+ int error = errno;
to_stderr(path);
- to_stderr(" could not be opened.\n");
+ to_stderr(" could not be opened: ");
+ to_stderr(strerror(error));
exit(1); // Exit if an error occurs
}
return fd;
@@ -176,7 +178,7 @@ void execute_subprogram(arguments_t args, char *argv[]) {
int main(int argc, char *argv[]) {
arguments_t args = parse_arguments(argc, argv);
- if (!args.valid)
+ if (!args.valid) {
to_stderr("Invalid arguments provided.");
return 1;
} else {