From 819475ff1de22dcec1073275ad7e094e0ffa94d2 Mon Sep 17 00:00:00 2001 From: Jekyll Wu Date: Fri, 13 Jan 2012 01:38:09 +0800 Subject: [PATCH] Improve the method of checking whether konsole is started from terminal The old way of calling isatty() is problmatic, because the checked file descriptor might be redirected by users. For example, 'konsole < /dev/null > /dev/null 2> /dev/null' will make that method fail. The better way is trying to open /dev/tty to see whether we have controlling terminal. Thanks to Askar Safin for pointing this out. --- src/main.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 560f00d5..dfa876fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,8 @@ // Unix #include +#include +#include // KDE #include @@ -31,9 +33,6 @@ #define KONSOLE_VERSION "2.8.999" -// standard input file descriptor -static const int STDIN = 0; - using Konsole::Application; // fill the KAboutData structure with information about contributors to Konsole. @@ -93,7 +92,13 @@ bool shouldUseNewProcess() // Konsole and any debug output or warnings from Konsole are written to // the current terminal KCmdLineArgs* args = KCmdLineArgs::parsedArgs(); - return isatty(STDIN) && !args->isSet("new-tab"); + + bool hasControllingTTY = false ; + if ( open("/dev/tty", O_RDONLY) != -1 ) { + hasControllingTTY = true ; + } + + return hasControllingTTY && !args->isSet("new-tab"); } void fillCommandLineOptions(KCmdLineOptions& options)