diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-07-14 11:58:58 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-07-14 11:58:58 -0400 |
commit | fda3ed8bb7bcce4cbac96c8e3280e40a6b4d89d2 (patch) | |
tree | 3d1f2096487c5767a8818ea59766790e0b84b887 | |
parent | 14cd3f396fdf11272b0c7c1101b9c48f8d68f83d (diff) |
add fast path for when stdout is not a terminal
-rw-r--r-- | more.c | 58 |
1 files changed, 56 insertions, 2 deletions
@@ -365,6 +365,55 @@ static void adjust_args(int *argc, char ***argv) } } +static void cat_loop(FILE *f) +{ + int c = 0; + while ((c = fgetc(f)) != EOF) { + putchar(c); + } +} + +static void compress_loop(FILE *f) +{ + int c = 0; + int nl = 0; + while ((c = fgetc(f)) != EOF) { + if (c == '\n') { + if (nl) { + continue; + } + nl = 1; + } else { + nl = 0; + } + putchar(c); + } +} + +static int more_cat(const char *path, int compressempty) +{ + FILE *f = stdin; + if (path != NULL && strcmp(path, "-") != 0) { + f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "more: %s: %s\n", path, strerror(errno)); + return 1; + } + } + + if (compressempty) { + compress_loop(f); + } else { + cat_loop(f); + } + + if (f != stdin) { + fclose(f); + } + + return 0; +} + int main(int argc, char *argv[]) { int c; @@ -418,10 +467,15 @@ int main(int argc, char *argv[]) } } - if (isatty(STDOUT_FILENO)) { - openrawtty(); + if (!isatty(STDOUT_FILENO)) { + int ret = 0; + do { + ret |= more_cat(argv[optind++], compressempty); + } while (optind < argc); + return ret; } + openrawtty(); global.lines--; if (optind >= argc) { |