From 224a6a9881473fbf89bfa3de90dd2ec40977ebf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 15 Dec 2023 14:29:51 +0800 Subject: [PATCH] TerminalShell: code cleanup --- src/detection/terminalshell/terminalshell.c | 26 +++++++++++++++- .../terminalshell/terminalshell_linux.c | 30 ++----------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index d54e1cc615..68b963e07d 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -4,6 +4,8 @@ #include "common/properties.h" #include "util/stringUtils.h" +#include + #ifdef _WIN32 #include "util/mallocHelper.h" @@ -162,6 +164,28 @@ static bool getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version) NULL }) == NULL; } +#else +static bool getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version) +{ + FF_STRBUF_AUTO_DESTROY command = ffStrbufCreate(); + ffStrbufAppendS(&command, "printf \"%s\" \"$"); + ffStrbufAppendTransformS(&command, exeName, toupper); + ffStrbufAppendS(&command, "_VERSION\""); + + if (ffProcessAppendStdOut(version, (char* const[]) { + "env", + "-i", + exe->chars, + "-c", + command.chars, + NULL + }) != NULL) + return false; + + ffStrbufSubstrBeforeFirstC(version, '('); + ffStrbufRemoveStrings(version, 2, (const char*[]) { "-release", "release" }); + return true; +} #endif bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) @@ -198,7 +222,7 @@ bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) return getFileVersion(exe->chars, version); #else - return false; + return getShellVersionGeneric(exe, exeName, version); #endif } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index a974ecc1bc..3fde133c4d 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -5,7 +5,6 @@ #include "common/thread.h" #include "util/stringUtils.h" -#include #include #include #include @@ -284,34 +283,8 @@ static void getUserShellFromEnv(FFTerminalShellResult* result) } } -static void getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version) -{ - FF_STRBUF_AUTO_DESTROY command = ffStrbufCreate(); - ffStrbufAppendS(&command, "printf \"%s\" \"$"); - ffStrbufAppendTransformS(&command, exeName, toupper); - ffStrbufAppendS(&command, "_VERSION\""); - - ffProcessAppendStdOut(version, (char* const[]) { - "env", - "-i", - exe->chars, - "-c", - command.chars, - NULL - }); - ffStrbufSubstrBeforeFirstC(version, '('); - ffStrbufRemoveStrings(version, 2, (const char*[]) { "-release", "release" }); -} - bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); -static void getShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) -{ - ffStrbufClear(version); - if(!fftsGetShellVersion(exe, exeName, version)) - getShellVersionGeneric(exe, exeName, version); -} - bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); const FFTerminalShellResult* ffDetectTerminalShell() @@ -348,7 +321,8 @@ const FFTerminalShellResult* ffDetectTerminalShell() getTerminalFromEnv(&result); getUserShellFromEnv(&result); - getShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion); + ffStrbufClear(&result.shellVersion); + fftsGetShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion); if(ffStrbufEqualS(&result.shellProcessName, "pwsh")) ffStrbufInitStatic(&result.shellPrettyName, "PowerShell");