From f1cc94207efd389277c60947bc1cef9e04aeb310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 22:35:40 +0800 Subject: [PATCH] Host: detect WSL version --- src/detection/host/host.h | 3 --- src/detection/host/host_linux.c | 24 ++++++++++++++++++- .../terminalshell/terminalshell_linux.c | 7 ++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/detection/host/host.h b/src/detection/host/host.h index e66af41fb..e49e8faad 100644 --- a/src/detection/host/host.h +++ b/src/detection/host/host.h @@ -5,9 +5,6 @@ #include "fastfetch.h" -#define FF_HOST_PRODUCT_NAME_WSL "Windows Subsystem for Linux" -#define FF_HOST_PRODUCT_NAME_MSYS "Windows on MSYS" - typedef struct FFHostResult { FFstrbuf productFamily; diff --git a/src/detection/host/host_linux.c b/src/detection/host/host_linux.c index ef9cfbf5e..483313103 100644 --- a/src/detection/host/host_linux.c +++ b/src/detection/host/host_linux.c @@ -1,5 +1,6 @@ #include "host.h" #include "common/io.h" +#include "common/processing.h" #include @@ -94,6 +95,27 @@ void ffDetectHostImpl(FFHostResult* host) { //On WSL, the real host can't be detected. Instead use WSL as host. if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL) - ffStrbufAppendS(&host->productName, FF_HOST_PRODUCT_NAME_WSL); + { + ffStrbufAppendS(&host->productName, "Windows Subsystem for Linux"); + + FFstrbuf wslVer; //Wide charactors + ffStrbufInit(&wslVer); + if(!ffProcessAppendStdOut(&wslVer, (char* const[]){ + "wsl.exe", + "--version", + NULL + }) && wslVer.length > 0) + { + ffStrbufSubstrBeforeFirstC(&wslVer, '\r'); //CRLF + ffStrbufSubstrAfterLastC(&wslVer, ' '); + ffStrbufAppendS(&host->productName, " ("); + for(uint32_t i = 0; i < wslVer.length; ++i) { + if(wslVer.chars[i]) //don't append \0 + ffStrbufAppendC(&host->productName, wslVer.chars[i]); + } + ffStrbufAppendC(&host->productName, ')'); + } + ffStrbufDestroy(&wslVer); + } } } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 0dfc40a10..2c3e7319d 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -1,5 +1,4 @@ #include "fastfetch.h" -#include "detection/host/host.h" #include "common/io.h" #include "common/parsing.h" #include "common/processing.h" @@ -239,10 +238,8 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) if(!ffStrSet(term)) { //We are in WSL but not in Windows Terminal - const FFHostResult* host = ffDetectHost(); - if(ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_WSL) == 0 || - ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_MSYS) == 0) //TODO better WSL or MSYS detection - term = "conhost"; + if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL) + term = "conhost"; } #endif