diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-07-06 14:14:54 -0600 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-07-07 20:01:15 +0430 |
commit | a11658737a292d0dbbbb13feb9c83f99c5773546 (patch) | |
tree | 8d3e154678b5a832a9cfa2e3f55a4f76ed5222b4 /Userland/Utilities | |
parent | 1ec061d6669201c6191e71f3d3c999f6021e77c1 (diff) | |
download | serenity-a11658737a292d0dbbbb13feb9c83f99c5773546.zip |
Userland: Less: emulate cat when stdout is not a tty
This is the most logical behavior when less is used in a pipe.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/less.cpp | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp index 3662ba59a2..f89be6c5f4 100644 --- a/Userland/Utilities/less.cpp +++ b/Userland/Utilities/less.cpp @@ -286,10 +286,26 @@ static String get_key_sequence() return String(buff, n); } -int main(int argc, char** argv) +static void cat_file(FILE* file) { - VERIFY(isatty(STDOUT_FILENO)); + ByteBuffer buffer = ByteBuffer::create_uninitialized(4096); + while (!feof(file)) { + size_t n = fread(buffer.data(), 1, buffer.size(), file); + if (n == 0 && ferror(file)) { + perror("fread"); + exit(1); + } + + n = fwrite(buffer.data(), 1, n, stdout); + if (n == 0 && ferror(stdout)) { + perror("fwrite"); + exit(1); + } + } +} +int main(int argc, char** argv) +{ char const* filename = "-"; char const* prompt = "?f%f :.(line %l)?e (END):."; bool dont_switch_buffer = false; @@ -321,6 +337,11 @@ int main(int argc, char** argv) prompt = "--More--"; } + if (!isatty(STDOUT_FILENO)) { + cat_file(file); + return 0; + } + setup_tty(!dont_switch_buffer); Pager pager(file, stdout, g_wsize.ws_col, g_wsize.ws_row); |