From dadcccdf26a1ae5ec8cd55af73f8ad6e63236352 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Thu, 28 May 2026 10:31:35 +0800 Subject: [PATCH] TerminalShell: code refactor --- src/detection/terminalshell/terminalshell.c | 8 ------ src/detection/terminalshell/terminalshell.h | 3 +++ .../terminalshell/terminalshell_linux.c | 25 +++++++++++-------- .../terminalshell/terminalshell_windows.c | 12 ++++----- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index afb4a1e27..c6733b2c3 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -235,10 +235,6 @@ static bool getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version) { #endif bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) { - if (!instance.config.general.detectVersion) { - return false; - } - if (ffStrEqualsIgnCase(exeName, "sh")) { // #849 return false; } @@ -727,10 +723,6 @@ static bool getTerminalVersionConEmu(FFstrbuf* exe, FFstrbuf* version) { #endif bool fftsGetTerminalVersion(FFstrbuf* processName, FF_A_UNUSED FFstrbuf* exe, FFstrbuf* version) { - if (!instance.config.general.detectVersion) { - return false; - } - #ifdef __ANDROID__ if (ffStrbufEqualS(processName, "com.termux")) { diff --git a/src/detection/terminalshell/terminalshell.h b/src/detection/terminalshell/terminalshell.h index 85083fa12..008e30c40 100644 --- a/src/detection/terminalshell/terminalshell.h +++ b/src/detection/terminalshell/terminalshell.h @@ -30,3 +30,6 @@ typedef struct FFTerminalResult { const FFShellResult* ffDetectShell(); const FFTerminalResult* ffDetectTerminal(); + +bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); +bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 75f88c4e9..9c99c8be9 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -280,14 +280,7 @@ static void getUserShellFromEnv(FFShellResult* result) { } } -bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); - -bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); - static void setShellInfoDetails(FFShellResult* result) { - ffStrbufClear(&result->version); - fftsGetShellVersion(result->exePath.length > 0 ? &result->exePath : &result->exe, result->exeName, &result->version); - if (ffStrbufEqualS(&result->processName, "pwsh")) { ffStrbufInitStatic(&result->prettyName, "PowerShell"); } else if (ffStrbufEqualS(&result->processName, "nu")) { @@ -369,8 +362,6 @@ static void setTerminalInfoDetails(FFTerminalResult* result) { } else { ffStrbufInitCopy(&result->prettyName, &result->processName); } - - fftsGetTerminalVersion(&result->processName, result->exePath.length > 0 ? &result->exePath : &result->exe, &result->version); } #if defined(MAXPATH) @@ -408,7 +399,13 @@ const FFShellResult* ffDetectShell() { ppid = getShellInfo(&result, ppid); getUserShellFromEnv(&result); - setShellInfoDetails(&result); + + if (result.processName.length > 0) { + setShellInfoDetails(&result); + if (instance.config.general.detectVersion) { + fftsGetShellVersion(result.exePath.length > 0 ? &result.exePath : &result.exe, result.exeName, &result.version); + } + } return &result; } @@ -436,7 +433,13 @@ const FFTerminalResult* ffDetectTerminal() { ppid = getTerminalInfo(&result, ppid); } getTerminalFromEnv(&result); - setTerminalInfoDetails(&result); + + if (result.processName.length > 0) { + setTerminalInfoDetails(&result); + if (instance.config.general.detectVersion) { + fftsGetTerminalVersion(&result.processName, result.exePath.length > 0 ? &result.exePath : &result.exe, &result.version); + } + } return &result; } diff --git a/src/detection/terminalshell/terminalshell_windows.c b/src/detection/terminalshell/terminalshell_windows.c index cd687e892..263692996 100644 --- a/src/detection/terminalshell/terminalshell_windows.c +++ b/src/detection/terminalshell/terminalshell_windows.c @@ -17,8 +17,6 @@ #include #include -bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); - static uint32_t getShellInfo(FFShellResult* result, uint32_t pid) { uint32_t ppid = 0; bool gui = false; @@ -292,8 +290,6 @@ static void setTerminalInfoDetails(FFTerminalResult* result) { } } -bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); - const FFShellResult* ffDetectShell(void) { static FFShellResult result; static bool init = false; @@ -332,7 +328,9 @@ const FFShellResult* ffDetectShell(void) { if (ext) { *ext = '\0'; } - fftsGetShellVersion(result.exePath.length > 0 ? &result.exePath : &result.exe, tmp, &result.version); + if (instance.config.general.detectVersion) { + fftsGetShellVersion(result.exePath.length > 0 ? &result.exePath : &result.exe, tmp, &result.version); + } } return &result; @@ -370,7 +368,9 @@ const FFTerminalResult* ffDetectTerminal(void) { if (result.processName.length > 0) { setTerminalInfoDetails(&result); - fftsGetTerminalVersion(&result.processName, result.exePath.length > 0 ? &result.exePath : &result.exe, &result.version); + if (instance.config.general.detectVersion) { + fftsGetTerminalVersion(&result.processName, result.exePath.length > 0 ? &result.exePath : &result.exe, &result.version); + } } return &result;