summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/io/read_file.c21
-rw-r--r--src/io/read_file.h9
-rw-r--r--src/tak.c6
3 files changed, 26 insertions, 10 deletions
diff --git a/src/io/read_file.c b/src/io/read_file.c
index c344f5b..414a6d8 100644
--- a/src/io/read_file.c
+++ b/src/io/read_file.c
@@ -13,7 +13,11 @@
return NULL; \
}
-struct filebuf *stream_to_filebuf(FILE *restrict stream) {
+struct filebuf *stream_to_filebuf(FILE *restrict stream, int openOptions) {
+ if (openOptions & O_NOALLOWSPONGE) {
+ return NULL;
+ }
+
size_t sz = 0;
size_t cap = 4096;
char *buf = malloc(cap);
@@ -56,12 +60,19 @@ static void *fd_to_mmap(const int fd, struct stat sb) {
return addr;
}
-struct filebuf *file_to_filebuf(char *fname) {
+struct filebuf *file_to_filebuf(char *fname, int openOptions) {
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) {
- goto stream;
+ goto sponge;
}
void *addr;
@@ -73,9 +84,9 @@ struct filebuf *file_to_filebuf(char *fname) {
return res;
}
-stream:
+sponge:
FILE *stream = fdopen(fd, "rb");
- struct filebuf *res = stream_to_filebuf(stream);
+ struct filebuf *res = stream_to_filebuf(stream, openOptions);
fclose(stream);
return res;
}
diff --git a/src/io/read_file.h b/src/io/read_file.h
index 201ae5b..89f738b 100644
--- a/src/io/read_file.h
+++ b/src/io/read_file.h
@@ -3,6 +3,11 @@
#include <stddef.h>
#include <stdio.h>
+enum allow_flags {
+ O_NOALLOWMAP = 1 << 0,
+ O_NOALLOWSPONGE = 1 << 1,
+};
+
enum mapping_type {
MT_MMAP,
MT_OWNED,
@@ -14,6 +19,6 @@ struct filebuf {
enum mapping_type mapping_type;
};
-struct filebuf *stream_to_filebuf(FILE *restrict stream);
-struct filebuf *file_to_filebuf(char *fname);
+struct filebuf *stream_to_filebuf(FILE *restrict stream, int openOptions);
+struct filebuf *file_to_filebuf(char *fname, int openOptions);
void free_filebuf(struct filebuf *filebuf);
diff --git a/src/tak.c b/src/tak.c
index c6b6143..cbe5a3b 100644
--- a/src/tak.c
+++ b/src/tak.c
@@ -68,7 +68,7 @@ int entry_tak(int argc, char **argv) {
char **args = parse_options(argc, argv);
if (*args == NULL) {
- struct filebuf *fb = stream_to_filebuf(stdin);
+ struct filebuf *fb = stream_to_filebuf(stdin, 0);
if (fb == NULL) goto err_stdin;
process(fb);
@@ -79,11 +79,11 @@ int entry_tak(int argc, char **argv) {
struct filebuf *fb = NULL;
if (!strcmp(*args, "-")) {
- fb = stream_to_filebuf(stdin);
+ fb = stream_to_filebuf(stdin, 0);
if (fb == NULL) goto err_stdin;
}
- if (fb == NULL) fb = file_to_filebuf(*args);
+ if (fb == NULL) fb = file_to_filebuf(*args, 0);
if (fb == NULL) goto err_file;
process(fb);