From a74edeb738198cbe7945fa3682da6e7030c13ce9 Mon Sep 17 00:00:00 2001 From: akiyamn Date: Tue, 8 Sep 2020 13:24:38 +1000 Subject: Added proper error handling --- fileutil.c | 10 ++++++---- 1 file 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 #include #include -#include +#include #include #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 { -- cgit v1.2.3