diff options
Diffstat (limited to '')
-rw-r--r-- | src/shared.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/shared.c b/src/shared.c index b167e6f..8d0dcca 100644 --- a/src/shared.c +++ b/src/shared.c @@ -5,6 +5,7 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <sys/stat.h> void error(const char* format, ...) { va_list list; @@ -15,14 +16,29 @@ void error(const char* format, ...) { exit(EXIT_FAILURE); } -FILE* get_file(const char* path, const char* type) { +FILE* get_file_s(const char* path, const char* type) { + struct stat s; + if (stat(path, &s) < 0) { + fprintf(stderr, "error: failed to read %s: %s\n", path, strerror(errno)); + return NULL; + } + if (!S_ISREG(s.st_mode)) { + fprintf(stderr, "error: %s is not a file\n", path); + return NULL; + } FILE* file = fopen(path, type); if (file == NULL) { - error("error: failed to open file %s: %s", path, strerror(errno)); + fprintf(stderr, "error: failed to open file %s: %s\n", path, strerror(errno)); } return file; } +FILE* get_file(const char* path, const char* type) { + FILE* file = get_file_s(path, type); + if (file == NULL) exit(EXIT_FAILURE); + return file; +} + long int get_number(const char* text) { char* end; long int n = strtol(text, &end, 10); |