Clean up the code around isatty() a bit

This commit is contained in:
Konstantin Nazarov 2024-08-18 20:14:35 +01:00
parent dbf52344ae
commit 4bd21e0e17
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 8 additions and 4 deletions

View file

@ -1,5 +1,7 @@
#include "fio.hpp" #include "fio.hpp"
#include <unistd.h>
#include <cstdlib> #include <cstdlib>
Result<String> read_fh(FILE* file) { Result<String> read_fh(FILE* file) {
@ -37,3 +39,7 @@ Result<String> read_file(const char* filename) {
} }
Result<String> read_stdin() { return read_fh(stdin); } Result<String> 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); }

View file

@ -5,3 +5,4 @@
Result<String> read_file(const char* filename); Result<String> read_file(const char* filename);
Result<String> read_stdin(); Result<String> read_stdin();
bool stdin_isatty();

View file

@ -1,6 +1,3 @@
#include <unistd.h>
#include <cstdio>
#include <iostream> #include <iostream>
#include "arena.hpp" #include "arena.hpp"
@ -17,7 +14,7 @@ StaticArena<64 * 1024 * 1024> arena;
Result<void> run(int argc, const char* argv[]) { Result<void> run(int argc, const char* argv[]) {
String src = DIEX(String::create("")); String src = DIEX(String::create(""));
if (argc == 1) { if (argc == 1) {
if (isatty(STDIN_FILENO)) { if (stdin_isatty()) {
die("Code expected at stdin, not a tty.\n"); die("Code expected at stdin, not a tty.\n");
} }
src = TRY(read_stdin()); src = TRY(read_stdin());