aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-04-17 18:40:17 -0400
committerShav Kinderlehrer <[email protected]>2023-04-17 18:40:17 -0400
commitf853dabe7f3585ec856669c6711df08087b65cd8 (patch)
treeb8b89665b23fe807a88810d89805e29ae5ffd946 /src
parentab10a9c2176faa2f339d4e39ace58b811a6c05be (diff)
downloadlat-f853dabe7f3585ec856669c6711df08087b65cd8.tar.gz
lat-f853dabe7f3585ec856669c6711df08087b65cd8.zip
Fix stdin supportv0.7.4
Diffstat (limited to 'src')
-rw-r--r--src/lib/file.c34
-rw-r--r--src/main.c14
2 files changed, 41 insertions, 7 deletions
diff --git a/src/lib/file.c b/src/lib/file.c
index 93591ab..865bc98 100644
--- a/src/lib/file.c
+++ b/src/lib/file.c
@@ -5,7 +5,7 @@
#include "types.h"
#include "util.h"
-struct filedata readfile(FILE *fp) {
+struct filedata readfile(FILE *fp, bool isstdin) {
struct filedata f;
f.lc = 0;
@@ -15,6 +15,38 @@ struct filedata readfile(FILE *fp) {
f.buf = NULL;
f.lines = NULL;
+ if (isstdin) {
+ size_t bufsize = 1024;
+ f.buf = malloc(bufsize);
+ if (f.buf == NULL)
+ die("malloc");
+
+ char c;
+ while (fread(&c, 1, 1, fp) > 0) {
+ if (f.buflen == bufsize - 1) {
+ bufsize *= 2;
+
+ char *new_buf = realloc(f.buf, bufsize);
+ if (new_buf == NULL)
+ die("realloc");
+
+ f.buf = new_buf;
+ }
+ f.buf[f.buflen++] = c;
+ }
+
+ if (f.buflen < bufsize - 1) {
+ char *new_buf = realloc(f.buf, f.buflen + 1);
+ if (new_buf == NULL)
+ die("realloc");
+
+ f.buf = new_buf;
+ }
+ f.buf[f.buflen] = '\0';
+
+ return f;
+ }
+
// expects to be at beginning of file
fseek(fp, 0, SEEK_END);
f.buflen = ftell(fp);
diff --git a/src/main.c b/src/main.c
index 7d0afa2..3478b71 100644
--- a/src/main.c
+++ b/src/main.c
@@ -21,7 +21,7 @@ void run(FILE *fp, char *filename, bool tty) {
const char *reset = conf.color ? RESET : "";
struct filedata f;
- f = readfile(fp);
+ f = readfile(fp, conf.stdin);
if (tty) {
char *addon = f.binary ? "<binary>" : "";
@@ -40,7 +40,7 @@ void run(FILE *fp, char *filename, bool tty) {
for (int i = 0; i < f.lc; i++) {
if (conf.lines) {
char *padding = linepad(linecount, f.lc);
- printf("%s%s%d:%s %s\n", grey, padding, i, reset, f.lines[i].buf);
+ printf("%s%s%d:%s %s\n", grey, padding, i + 1, reset, f.lines[i].buf);
free(padding);
linecount++;
} else {
@@ -60,14 +60,12 @@ void run(FILE *fp, char *filename, bool tty) {
float rounded;
char *format = formatbytes(f.buflen, &rounded);
- // char *cnewline = c == '\n' ? "" : "\n";
- char *cnewline = "";
- fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format,
- reset);
+ fprintf(stderr, "\r%s%.2f %s%s\r\n", invert_t, rounded, format, reset);
}
}
void initconf(void) {
+ conf.stdin = false;
conf.process = true;
conf.color = true;
conf.lines = true;
@@ -98,10 +96,12 @@ int main(int argc, char *argv[]) {
if (conf.has_read_stdin)
clearstdin();
conf.has_read_stdin = true;
+ conf.stdin = true;
run(stdin, "stdin", tty);
continue;
}
+ conf.stdin = false;
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
@@ -114,9 +114,11 @@ int main(int argc, char *argv[]) {
}
if (offset == argc) {
+ conf.stdin = true;
run(stdin, "stdin", tty);
}
} else {
+ conf.stdin = true;
run(stdin, "stdin", tty); // for piped-input or repl-like behavior
}