From 7b15c85ff5ef8cad674bd64871cb8435a255cb8f Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Wed, 5 Aug 2020 10:00:01 +0430 Subject: [PATCH] Shell: Do not assume that stdin/stdout is a TTY This closes #2989. --- Shell/Builtin.cpp | 3 ++- Shell/Shell.cpp | 16 ++++++++++++---- Shell/Shell.h | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index c3184eefa4..33a2d91178 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -267,7 +267,8 @@ int Shell::builtin_exit(int argc, const char** argv) } stop_all_jobs(); save_history(); - printf("Good-bye!\n"); + if (m_is_interactive) + printf("Good-bye!\n"); exit(exit_code); return 0; } diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index de8a103a91..3d7dd49030 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -58,7 +58,7 @@ extern char** environ; void Shell::print_path(const String& path) { - if (s_disable_hyperlinks) { + if (s_disable_hyperlinks || !m_is_interactive) { printf("%s", path.characters()); return; } @@ -1092,9 +1092,17 @@ Shell::Shell() int rc = gethostname(hostname, Shell::HostNameSize); if (rc < 0) perror("gethostname"); - rc = ttyname_r(0, ttyname, Shell::TTYNameSize); - if (rc < 0) - perror("ttyname_r"); + + auto istty = isatty(STDIN_FILENO); + m_is_interactive = istty; + + if (istty) { + rc = ttyname_r(0, ttyname, Shell::TTYNameSize); + if (rc < 0) + perror("ttyname_r"); + } else { + ttyname[0] = 0; + } { auto* cwd = getcwd(nullptr, 0); diff --git a/Shell/Shell.h b/Shell/Shell.h index 0da1fd1c6a..a0b29d0bdd 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -218,6 +218,7 @@ private: Vector m_local_frames; HashMap m_aliases; + bool m_is_interactive { true }; }; static constexpr bool is_word_character(char c)