diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-28 15:13:10 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-28 15:13:10 -0400 |
commit | 7a1eef12299f4b030bfb77d9bbb133d0ba2ee674 (patch) | |
tree | 61d7008b25e37799318db4030b86f85fc0059275 /src/shared.c | |
parent | fix missing newline on single row output (diff) | |
download | lazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.tar.gz lazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.tar.bz2 lazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.zip |
add tail
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); |