aboutsummaryrefslogtreecommitdiff
path: root/src/lib/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/file.c')
-rw-r--r--src/lib/file.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/lib/file.c b/src/lib/file.c
index e2c7c5d..494e740 100644
--- a/src/lib/file.c
+++ b/src/lib/file.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "file.h"
#include "util.h"
@@ -9,9 +10,9 @@ struct filedata readfile(FILE *fp) {
f.lc = 0;
f.len = 0;
+ f.binary = 0;
size_t bufsize = 4;
-
f.buf = malloc(bufsize);
if (f.buf == NULL)
die("malloc");
@@ -37,5 +38,19 @@ struct filedata readfile(FILE *fp) {
f.buf[f.len++] = c;
}
+ // guess if printable
+ // from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs
+ int testlen = 64;
+ char *testbuf[testlen];
+ memcpy(testbuf, f.buf, testlen);
+
+ char *result = memchr(testbuf, 0x00, testlen);
+
+ if (result) {
+ f.binary = 1;
+ } else {
+ f.binary = 0;
+ }
+
return f;
}