diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/file.c | 17 | ||||
-rw-r--r-- | src/lib/util.c | 2 |
2 files changed, 17 insertions, 2 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; } diff --git a/src/lib/util.c b/src/lib/util.c index 6561af8..d2d4f6a 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -7,7 +7,7 @@ void die(const char *message) { exit(1); } -char *formatBytes(size_t bytes, float *rounded) { +char *formatbytes(size_t bytes, float *rounded) { char *SIZES[] = {"bytes", "kB", "MB", "GB"}; size_t size = bytes; |