From 76684e49cadf95bf926298313ea7f881d905ab64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 15 Oct 2022 22:35:04 +0800 Subject: [PATCH] TerminalShell: speed up PowerShell version detection by querying the version of pwsh.exe file Also add cmd version detection --- CMakeLists.txt | 1 + src/detection/terminalshell/terminalshell.c | 66 ++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 880839700..1d0cf1b4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -525,6 +525,7 @@ elseif(WIN32) PRIVATE "wbemuuid" PRIVATE "ws2_32" PRIVATE "ntdll" + PRIVATE "version" ) if(USE_WIN_FAST_PPID_DETECTION) target_compile_definitions(libfastfetch PRIVATE FF_USE_WIN_FAST_PPID_DETECTION) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index 260b28faa..f4141a6b2 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -1,6 +1,41 @@ #include "fastfetch.h" #include "common/processing.h" +#ifdef _WIN32 + +#include + +static bool getFileVersion(const char* exePath, FFstrbuf* version) +{ + DWORD handle; + DWORD size = GetFileVersionInfoSizeA(exePath, &handle); + if(size > 0) + { + void* versionData = malloc(size); + if(GetFileVersionInfoA(exePath, handle, size, versionData)) + { + VS_FIXEDFILEINFO* verInfo; + UINT len; + if(VerQueryValueW(versionData, L"\\", (void**)&verInfo, &len) && len && verInfo->dwSignature == 0xFEEF04BD) + { + ffStrbufAppendF(version, "%u.%u.%u.%u", + (unsigned)(( verInfo->dwFileVersionMS >> 16 ) & 0xffff), + (unsigned)(( verInfo->dwFileVersionMS >> 0 ) & 0xffff), + (unsigned)(( verInfo->dwFileVersionLS >> 16 ) & 0xffff), + (unsigned)(( verInfo->dwFileVersionLS >> 0 ) & 0xffff) + ); + free(versionData); + return true; + } + } + free(versionData); + } + + return false; +} + +#endif + static void getShellVersionBash(FFstrbuf* exe, FFstrbuf* version) { ffProcessAppendStdOut(version, (char* const[]) { @@ -25,7 +60,7 @@ static void getShellVersionZsh(FFstrbuf* exe, FFstrbuf* version) ffStrbufSubstrAfterFirstC(version, ' '); } -static void getShellVersionFishPwsh(FFstrbuf* exe, FFstrbuf* version) +static void getShellVersionFish(FFstrbuf* exe, FFstrbuf* version) { ffProcessAppendStdOut(version, (char* const[]) { exe->chars, @@ -36,6 +71,22 @@ static void getShellVersionFishPwsh(FFstrbuf* exe, FFstrbuf* version) ffStrbufSubstrAfterLastC(version, ' '); } +static void getShellVersionPwsh(FFstrbuf* exe, FFstrbuf* version) +{ + #ifdef _WIN32 + if(getFileVersion(exe->chars, version)) + return; + #endif + + ffProcessAppendStdOut(version, (char* const[]) { + exe->chars, + "--version", + NULL + }); + ffStrbufTrimRight(version, '\n'); + ffStrbufSubstrAfterLastC(version, ' '); +} + #ifdef _WIN32 static void getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version) { @@ -50,6 +101,11 @@ static void getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version) ffStrbufTrimRight(version, '\n'); ffStrbufSubstrAfterLastC(version, ' '); } + +static void getShellVersionCmd(FFstrbuf* exe, FFstrbuf* version) +{ + getFileVersion(exe->chars, version); +} #endif static void getShellVersionNu(FFstrbuf* exe, FFstrbuf* version) @@ -68,14 +124,18 @@ bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) getShellVersionBash(exe, version); else if(strcasecmp(exeName, "zsh") == 0) getShellVersionZsh(exe, version); - else if(strcasecmp(exeName, "fish") == 0 || strcasecmp(exeName, "pwsh") == 0) - getShellVersionFishPwsh(exe, version); + else if(strcasecmp(exeName, "fish") == 0) + getShellVersionFish(exe, version); + else if(strcasecmp(exeName, "pwsh") == 0) + getShellVersionPwsh(exe, version); else if(strcasecmp(exeName, "nu") == 0) getShellVersionNu(exe, version); #ifdef _WIN32 else if(strcasecmp(exeName, "powershell") == 0 || strcasecmp(exeName, "powershell_ise") == 0) getShellVersionWinPowerShell(exe, version); + else if(strcasecmp(exeName, "cmd") == 0) + getShellVersionCmd(exe, version); #endif else