diff options
Diffstat (limited to '')
-rw-r--r-- | src/util/shared.c (renamed from src/shared.c) | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/shared.c b/src/util/shared.c index 240e364..c5c22e9 100644 --- a/src/shared.c +++ b/src/util/shared.c @@ -18,15 +18,19 @@ void error(const char* format, ...) { FILE* get_file_s(const char* path, const char* type) { struct stat s; - if (stat(path, &s) < 0) { + if (lstat(path, &s) < 0) { + if (type[0] != 'r') goto read; 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); + if (S_ISDIR(s.st_mode)) { + fprintf(stderr, "error: %s is a directory\n", path); return NULL; } - FILE* file = fopen(path, type); + + FILE* file; +read: + file = fopen(path, type); if (file == NULL) { fprintf(stderr, "error: failed to open file %s: %s\n", path, strerror(errno)); } @@ -34,7 +38,10 @@ FILE* get_file_s(const char* path, const char* type) { } FILE* get_file(const char* path, const char* type) { - if (streql("-", path)) return stdin; + if (streql("-", path) && type[0] == 'r') { + clearerr(stdin); + return stdin; + } FILE* file = get_file_s(path, type); if (file == NULL) exit(EXIT_FAILURE); return file; |