summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-23 00:23:06 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-02 22:38:00 +0200
commitc4ccd4e0798a7ed9af6f86d9819b309575d418a0 (patch)
treef76183914b89d801b2853e78d4886cc00281dad0 /src
parent420e28845d5068d0c3b432ffc0d5fc80b7b7d7e2 (diff)
file_to_filebuf: isdir output parameter
Diffstat (limited to 'src')
-rw-r--r--src/io/read_file.c12
-rw-r--r--src/io/read_file.h3
-rw-r--r--src/tak.c12
3 files changed, 18 insertions, 9 deletions
diff --git a/src/io/read_file.c b/src/io/read_file.c
index 219a395..22e0286 100644
--- a/src/io/read_file.c
+++ b/src/io/read_file.c
@@ -60,18 +60,20 @@ static void *fd_to_mmap(const int fd, struct stat sb) {
return addr;
}
-struct filebuf *file_to_filebuf(char *fname, int openOptions) {
+struct filebuf *file_to_filebuf(char *fname, int openOptions, bool *isdir) {
const int fd = open(fname, O_RDONLY);
if (fd == -1) {
return NULL;
}
- if (openOptions & O_NOALLOWMAP) {
- goto sponge;
- }
-
struct stat sb;
if (fstat(fd, &sb) == -1) {
+ return NULL;
+ }
+
+ if (isdir != NULL) *isdir = S_ISDIR(sb.st_mode);
+
+ if (openOptions & O_NOALLOWMAP) {
goto sponge;
}
diff --git a/src/io/read_file.h b/src/io/read_file.h
index 89f738b..d097c05 100644
--- a/src/io/read_file.h
+++ b/src/io/read_file.h
@@ -1,5 +1,6 @@
#pragma once
+#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@@ -20,5 +21,5 @@ struct filebuf {
};
struct filebuf *stream_to_filebuf(FILE *restrict stream, int openOptions);
-struct filebuf *file_to_filebuf(char *fname, int openOptions);
+struct filebuf *file_to_filebuf(char *fname, int openOptions, bool *isdir);
void free_filebuf(struct filebuf *filebuf);
diff --git a/src/tak.c b/src/tak.c
index 8ae6ca5..e0c501f 100644
--- a/src/tak.c
+++ b/src/tak.c
@@ -82,11 +82,13 @@ int entry_tak(int argc, char **argv) {
if (!strcmp(*args, "-")) {
fb = stream_to_filebuf(stdin, 0);
if (fb == NULL) goto err_stdin;
+ } else {
+ bool isdir = false;
+ fb = file_to_filebuf(*args, 0, &isdir);
+ if (isdir) goto err_isdir;
+ else if (fb == NULL) goto err_file;
}
- if (fb == NULL) fb = file_to_filebuf(*args, 0);
-
- if (fb == NULL) goto err_file;
process(fb);
args++;
@@ -101,4 +103,8 @@ err_stdin:
err_file:
fprintf(stderr, "tak: fout bij lezen van bestand\n");
return 1;
+
+err_isdir:
+ fprintf(stderr, "tak: bestand '%s' is een mapje\n", *args);
+ return 1;
}