From 4bd21e0e17215147b7cab008157762fccd087d49 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 18 Aug 2024 20:14:35 +0100 Subject: [PATCH] Clean up the code around isatty() a bit --- src/fio.cpp | 6 ++++++ src/fio.hpp | 1 + src/vli.cpp | 5 +---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/fio.cpp b/src/fio.cpp index a343eea..d3006c6 100644 --- a/src/fio.cpp +++ b/src/fio.cpp @@ -1,5 +1,7 @@ #include "fio.hpp" +#include + #include Result read_fh(FILE* file) { @@ -37,3 +39,7 @@ Result read_file(const char* filename) { } Result read_stdin() { return read_fh(stdin); } + +// TODO: though unlikely, but isatty can possibly set an error. Need to check it +// someday. +bool stdin_isatty() { return isatty(STDIN_FILENO); } diff --git a/src/fio.hpp b/src/fio.hpp index 9129193..91f17bf 100644 --- a/src/fio.hpp +++ b/src/fio.hpp @@ -5,3 +5,4 @@ Result read_file(const char* filename); Result read_stdin(); +bool stdin_isatty(); diff --git a/src/vli.cpp b/src/vli.cpp index 3aaee7e..e77b41a 100644 --- a/src/vli.cpp +++ b/src/vli.cpp @@ -1,6 +1,3 @@ -#include - -#include #include #include "arena.hpp" @@ -17,7 +14,7 @@ StaticArena<64 * 1024 * 1024> arena; Result run(int argc, const char* argv[]) { String src = DIEX(String::create("")); if (argc == 1) { - if (isatty(STDIN_FILENO)) { + if (stdin_isatty()) { die("Code expected at stdin, not a tty.\n"); } src = TRY(read_stdin());