From f853dabe7f3585ec856669c6711df08087b65cd8 Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Mon, 17 Apr 2023 18:40:17 -0400 Subject: [PATCH] Fix stdin support --- Makefile | 1 + include/arg.h | 3 ++- include/file.h | 3 ++- src/lib/file.c | 34 +++++++++++++++++++++++++++++++++- src/main.c | 14 ++++++++------ 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 149c2ac..4f80f3e 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ $(ODIR)/%.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) $(LIB) $(NAME): $(OBJ) + @echo "--don't forget to set $(NAME)'s version--" $(CC) -o $(BINDIR)/$@ $^ $(CFLAGS) $(LIB) .PHONY: prep diff --git a/include/arg.h b/include/arg.h index 1bf23f4..dc2e242 100644 --- a/include/arg.h +++ b/include/arg.h @@ -2,9 +2,10 @@ #define ARG_H #include -#define LAT_VERSION "0.4.0" +#define LAT_VERSION "0.7.3" struct config { + bool stdin; bool process; bool color; bool lines; diff --git a/include/file.h b/include/file.h index 5518d17..8612857 100644 --- a/include/file.h +++ b/include/file.h @@ -1,7 +1,8 @@ #ifndef FILE_H #define FILE_H +#include #include -struct filedata readfile(FILE *fp); +struct filedata readfile(FILE *fp, bool isstdin); #endif 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 ? "" : ""; @@ -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 }