aboutsummaryrefslogtreecommitdiff
path: root/src/lib/file.c
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-04-19 13:28:06 -0400
committerShav Kinderlehrer <[email protected]>2023-04-19 13:28:06 -0400
commitc713312b2281787030602e6d1e10d5a65cd2dbce (patch)
tree451fd848013461ba1da9abf39b9dd2d0817b47c2 /src/lib/file.c
parent219075499f0c7307cbf9fb3a6c3561cf6c99aeb0 (diff)
downloadlat-c713312b2281787030602e6d1e10d5a65cd2dbce.tar.gz
lat-c713312b2281787030602e6d1e10d5a65cd2dbce.zip
Check stdin for binary toov0.10.2
Diffstat (limited to 'src/lib/file.c')
-rw-r--r--src/lib/file.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/lib/file.c b/src/lib/file.c
index 865bc98..73b76ce 100644
--- a/src/lib/file.c
+++ b/src/lib/file.c
@@ -5,6 +5,23 @@
#include "types.h"
#include "util.h"
+bool isbinary(struct filedata *f) {
+
+ // guess if printable
+ // from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs
+ int testlen = f->buflen >= 64 ? 64 : f->buflen;
+ char *testbuf[testlen];
+ memcpy(testbuf, f->buf, testlen);
+
+ char *result = memchr(testbuf, 0x00, testlen);
+
+ if (result) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
struct filedata readfile(FILE *fp, bool isstdin) {
struct filedata f;
@@ -44,6 +61,8 @@ struct filedata readfile(FILE *fp, bool isstdin) {
}
f.buf[f.buflen] = '\0';
+ f.binary = isbinary(&f);
+
return f;
}
@@ -60,19 +79,7 @@ struct filedata readfile(FILE *fp, bool isstdin) {
die("fread");
}
- // guess if printable
- // from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs
- int testlen = f.buflen >= 64 ? 64 : f.buflen;
- char *testbuf[testlen];
- memcpy(testbuf, f.buf, testlen);
-
- char *result = memchr(testbuf, 0x00, testlen);
-
- if (result) {
- f.binary = 1;
- } else {
- f.binary = 0;
- }
+ f.binary = isbinary(&f);
return f;
}