From 49a1f0456f00d10b19f0ff97dc42e1c0e11d772a Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 22 Jul 2019 13:29:53 -0700 Subject: [PATCH] Output error on directory --- src/roflcat.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 4ae6476..176e923 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -34,6 +34,8 @@ #include //std::chrono::* #include //srand #include //char_traits +#include //struct stat +#include //struct stat constexpr const char version_string[] = "roflcat version 1.0.4"; @@ -135,11 +137,23 @@ void nonprinting_notation(int in, char* dest){ } } +bool is_directory(const char* file){ + struct stat st; + if(stat(file, &st) != 0){ + return true; + } + return S_ISDIR(st.st_mode); +} FILE* open_file(const char* file, const char* mode){ + if(is_directory(file)){ + fflush(stdout); + fprintf(stderr, "Unable to open file \"%s\": Is a directory \n", file); + return nullptr; + } FILE* fp = fopen(file, mode); if(!fp){ fflush(stdout); - fprintf(stderr, "Unable to open file %s\n", file); + fprintf(stderr, "Unable to open file for reading: \"%s\"\n", file); } return fp; }