From 7a1eef12299f4b030bfb77d9bbb133d0ba2ee674 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Fri, 28 Apr 2023 15:13:10 -0400 Subject: add tail --- src/shared.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/shared.c') 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 #include #include +#include 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); -- cgit v1.2.3-freya