From a749014e7c7769b49578e49f2f8c50f8ed7780ff Mon Sep 17 00:00:00 2001 From: Carter Li Date: Tue, 2 Jul 2024 10:12:26 +0800 Subject: [PATCH] Kernel: code refactor --- src/detection/memory/memory_apple.c | 2 +- src/detection/memory/memory_bsd.c | 2 +- src/detection/memory/memory_sunos.c | 4 +- src/detection/swap/swap_bsd.c | 4 +- src/detection/swap/swap_sunos.c | 4 +- src/detection/swap/swap_windows.c | 2 +- src/detection/terminalshell/terminalshell.c | 2 +- src/detection/version/version.c | 72 +++++++++++++-------- src/detection/version/version.h | 3 +- src/fastfetch.c | 5 +- src/logo/logo.c | 2 +- src/modules/kernel/kernel.c | 37 ++++++----- src/modules/os/os.c | 14 ++-- src/modules/title/title.c | 1 - src/modules/version/version.c | 45 ++++++------- src/util/platform/FFPlatform.c | 42 +++++------- src/util/platform/FFPlatform.h | 21 +++--- src/util/platform/FFPlatform_unix.c | 22 +++---- src/util/platform/FFPlatform_windows.c | 53 +++++++-------- 19 files changed, 180 insertions(+), 157 deletions(-) diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 19e67f1a4..cabdab267 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -25,7 +25,7 @@ const char* ffDetectMemory(FFMemoryResult* ram) + vmstat.compressor_page_count - vmstat.purgeable_count - vmstat.external_page_count - ) * instance.state.platform.pageSize; + ) * instance.state.platform.sysinfo.pageSize; return NULL; } diff --git a/src/detection/memory/memory_bsd.c b/src/detection/memory/memory_bsd.c index 9af77cb70..0db20527a 100644 --- a/src/detection/memory/memory_bsd.c +++ b/src/detection/memory/memory_bsd.c @@ -12,7 +12,7 @@ const char* ffDetectMemory(FFMemoryResult* ram) + ffSysctlGetInt("vm.stats.vm.v_inactive_count", 0) + ffSysctlGetInt("vm.stats.vm.v_cache_count", 0); - ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * instance.state.platform.pageSize; + ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * instance.state.platform.sysinfo.pageSize; return NULL; } diff --git a/src/detection/memory/memory_sunos.c b/src/detection/memory/memory_sunos.c index 9e9f0f11e..4633eb6c0 100644 --- a/src/detection/memory/memory_sunos.c +++ b/src/detection/memory/memory_sunos.c @@ -23,11 +23,11 @@ const char* ffDetectMemory(FFMemoryResult* ram) { kstat_named_t* kn = kstat_data_lookup(ks, "pagestotal"); - ram->bytesTotal = kn->value.ui64 * instance.state.platform.pageSize; + ram->bytesTotal = kn->value.ui64 * instance.state.platform.sysinfo.pageSize; } { kstat_named_t* kn = kstat_data_lookup(ks, "pagesfree"); - ram->bytesUsed = ram->bytesTotal - kn->value.ui64 * instance.state.platform.pageSize; + ram->bytesUsed = ram->bytesTotal - kn->value.ui64 * instance.state.platform.sysinfo.pageSize; } return NULL; diff --git a/src/detection/swap/swap_bsd.c b/src/detection/swap/swap_bsd.c index 6fa8fb3ea..7cbb2a1fe 100644 --- a/src/detection/swap/swap_bsd.c +++ b/src/detection/swap/swap_bsd.c @@ -25,8 +25,8 @@ const char* ffDetectSwap(FFSwapResult* swap) swap->bytesTotal += (uint64_t) xsw.xsw_nblks; } - swap->bytesUsed *= instance.state.platform.pageSize; - swap->bytesTotal *= instance.state.platform.pageSize; + swap->bytesUsed *= instance.state.platform.sysinfo.pageSize; + swap->bytesTotal *= instance.state.platform.sysinfo.pageSize; return NULL; } diff --git a/src/detection/swap/swap_sunos.c b/src/detection/swap/swap_sunos.c index f564e9929..1866cdcd9 100644 --- a/src/detection/swap/swap_sunos.c +++ b/src/detection/swap/swap_sunos.c @@ -26,8 +26,8 @@ const char* ffDetectSwap(FFSwapResult* swap) swap->bytesUsed += (uint64_t) table->swt_ent[i].ste_free; } swap->bytesUsed = swap->bytesTotal - swap->bytesUsed; - swap->bytesTotal *= instance.state.platform.pageSize; - swap->bytesUsed *= instance.state.platform.pageSize; + swap->bytesTotal *= instance.state.platform.sysinfo.pageSize; + swap->bytesUsed *= instance.state.platform.sysinfo.pageSize; return NULL; } diff --git a/src/detection/swap/swap_windows.c b/src/detection/swap/swap_windows.c index 5b3a15457..322516fdb 100644 --- a/src/detection/swap/swap_windows.c +++ b/src/detection/swap/swap_windows.c @@ -13,7 +13,7 @@ const char* ffDetectSwap(FFSwapResult* swap) if(!NT_SUCCESS(NtQuerySystemInformation(SystemPagefileInformation, pstart, size, &size))) return "NtQuerySystemInformation(SystemPagefileInformation, size) failed"; - uint32_t pageSize = instance.state.platform.pageSize; + uint32_t pageSize = instance.state.platform.sysinfo.pageSize; swap->bytesUsed = (uint64_t)pstart->TotalUsed * pageSize; swap->bytesTotal = (uint64_t)pstart->CurrentSize * pageSize; diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index f4e4ccdef..57ccb6a89 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -500,7 +500,7 @@ static bool getTerminalVersionKitty(FFstrbuf* exe, FFstrbuf* version) return getExeVersionGeneral(exe, version); } -static bool getTerminalVersionPtyxis(FF_MAYBE_UNUSED FFstrbuf* exe, FFstrbuf* version) +FF_MAYBE_UNUSED static bool getTerminalVersionPtyxis(FF_MAYBE_UNUSED FFstrbuf* exe, FFstrbuf* version) { if(ffProcessAppendStdOut(version, (char* const[]) { "ptyxis", diff --git a/src/detection/version/version.c b/src/detection/version/version.c index 3bb4f2f8a..b6d6e7d77 100644 --- a/src/detection/version/version.c +++ b/src/detection/version/version.c @@ -20,43 +20,61 @@ #define FF_ARCHITECTURE "unknown" #endif + +#if defined(__linux__) + #define FF_SYSNAME "Linux" +#elif defined(__FreeBSD__) + #define FF_SYSNAME "FreeBSD" +#elif defined(__APPLE__) + #define FF_SYSNAME "Darwin" +#elif defined(_WIN32) + #define FF_SYSNAME "WIN32" +#elif defined(__sun) + #define FF_SYSNAME "SunOS" +#else + #define FF_SYSNAME "unknown" +#endif + #define FF_STR_INDIR(x) #x #define FF_STR(x) FF_STR_INDIR(x) -void ffDetectVersion(FFVersionResult* version) -{ - version->projectName = FASTFETCH_PROJECT_NAME; - version->architecture = FF_ARCHITECTURE; - version->version = FASTFETCH_PROJECT_VERSION; - version->versionTweak = FASTFETCH_PROJECT_VERSION_TWEAK; - version->cmakeBuiltType = FASTFETCH_PROJECT_CMAKE_BUILD_TYPE; - version->compileTime = __DATE__ ", " __TIME__; +FFVersionResult ffVersionResult = { + .projectName = FASTFETCH_PROJECT_NAME, + .sysName = FF_SYSNAME, + .architecture = FF_ARCHITECTURE, + .version = FASTFETCH_PROJECT_VERSION, + .versionTweak = FASTFETCH_PROJECT_VERSION_TWEAK, + .cmakeBuiltType = FASTFETCH_PROJECT_CMAKE_BUILD_TYPE, + .compileTime = __DATE__ ", " __TIME__, + .compiler = + #ifdef __clang__ - version->compiler = - #ifdef _MSC_VER - "clang-cl " ; - #elif defined(__APPLE__) && defined(__apple_build_version__) - "Apple clang " - #else - "clang " - #endif + #ifdef _MSC_VER + "clang-cl " ; + #elif defined(__APPLE__) && defined(__apple_build_version__) + "Apple clang " + #else + "clang " + #endif - FF_STR(__clang_major__) "." FF_STR(__clang_minor__) "." FF_STR(__clang_patchlevel__) + FF_STR(__clang_major__) "." FF_STR(__clang_minor__) "." FF_STR(__clang_patchlevel__) - #if defined(__APPLE__) && defined(__apple_build_version__) - " (" FF_STR(__apple_build_version__) ")" - #endif - ; + #if defined(__APPLE__) && defined(__apple_build_version__) + " (" FF_STR(__apple_build_version__) ")" + #endif + , #elif defined(__GNUC__) - version->compiler = "gcc " FF_STR(__GNUC__) "." FF_STR(__GNUC_MINOR__) "." FF_STR(__GNUC_PATCHLEVEL__); + "gcc " FF_STR(__GNUC__) "." FF_STR(__GNUC_MINOR__) "." FF_STR(__GNUC_PATCHLEVEL__), #elif defined(_MSC_VER) - version->compiler = "msvc " FF_STR(_MSC_VER); + "msvc " FF_STR(_MSC_VER), #else - version->compiler = "unknown"; + "unknown", #endif + + .debugMode = #ifndef NDEBUG - version->debugMode = true; + true, #else - version->debugMode = false; + false, #endif -} +}; diff --git a/src/detection/version/version.h b/src/detection/version/version.h index 5227561e0..7bb9bd56d 100644 --- a/src/detection/version/version.h +++ b/src/detection/version/version.h @@ -5,6 +5,7 @@ typedef struct FFVersionResult { const char* projectName; + const char* sysName; const char* architecture; const char* version; const char* versionTweak; @@ -14,4 +15,4 @@ typedef struct FFVersionResult bool debugMode; } FFVersionResult; -void ffDetectVersion(FFVersionResult* version); +extern FFVersionResult ffVersionResult; diff --git a/src/fastfetch.c b/src/fastfetch.c index 9d11507fa..12de044e5 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -501,9 +501,8 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val static void printVersion() { - FFVersionResult result = {}; - ffDetectVersion(&result); - printf("%s %s%s%s (%s)\n", result.projectName, result.version, result.versionTweak, result.debugMode ? "-debug" : "", result.architecture); + FFVersionResult* result = &ffVersionResult; + printf("%s %s%s%s (%s)\n", result->projectName, result->version, result->versionTweak, result->debugMode ? "-debug" : "", result->architecture); } static void parseCommand(FFdata* data, char* key, char* value) diff --git a/src/logo/logo.c b/src/logo/logo.c index 51b990aab..9770fb0c1 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -340,7 +340,7 @@ static const FFlogo* logoGetBuiltinDetected(FFLogoSize size) if(logo != NULL) return logo; - logo = logoGetBuiltin(&instance.state.platform.systemName, size); + logo = logoGetBuiltin(&instance.state.platform.sysinfo.name, size); if(logo != NULL) return logo; diff --git a/src/modules/kernel/kernel.c b/src/modules/kernel/kernel.c index 862ee1989..5e171faf5 100644 --- a/src/modules/kernel/kernel.c +++ b/src/modules/kernel/kernel.c @@ -3,29 +3,32 @@ #include "modules/kernel/kernel.h" #include "util/stringUtils.h" -#define FF_KERNEL_NUM_FORMAT_ARGS 5 +#define FF_KERNEL_NUM_FORMAT_ARGS 6 void ffPrintKernel(FFKernelOptions* options) { - const FFPlatform* platform = &instance.state.platform; + const FFPlatformSysinfo* info = &instance.state.platform.sysinfo; if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_KERNEL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - printf("%s %s", platform->systemName.chars, platform->systemRelease.chars); + printf("%s %s", info->name.chars, info->release.chars); - if(platform->systemDisplayVersion.length > 0) - printf(" (%s)\n", platform->systemDisplayVersion.chars); + if(info->displayVersion.length > 0) + printf(" (%s)\n", info->displayVersion.chars); else putchar('\n'); } else { + FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); + ffParseSize(info->pageSize, &str); FF_PRINT_FORMAT_CHECKED(FF_KERNEL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_KERNEL_NUM_FORMAT_ARGS, ((FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &platform->systemName, "sysname"}, - {FF_FORMAT_ARG_TYPE_STRBUF, &platform->systemRelease, "release"}, - {FF_FORMAT_ARG_TYPE_STRBUF, &platform->systemVersion, "version"}, - {FF_FORMAT_ARG_TYPE_STRBUF, &platform->systemArchitecture, "arch"}, - {FF_FORMAT_ARG_TYPE_STRBUF, &platform->systemDisplayVersion, "display-version"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &info->name, "sysname"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &info->release, "release"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &info->version, "version"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &info->architecture, "arch"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &info->displayVersion, "display-version"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &str, "page-size"}, })); } } @@ -67,12 +70,15 @@ void ffGenerateKernelJsonConfig(FFKernelOptions* options, yyjson_mut_doc* doc, y void ffGenerateKernelJsonResult(FF_MAYBE_UNUSED FFKernelOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + const FFPlatformSysinfo* info = &instance.state.platform.sysinfo; + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); - yyjson_mut_obj_add_strbuf(doc, obj, "architecture", &instance.state.platform.systemArchitecture); - yyjson_mut_obj_add_strbuf(doc, obj, "name", &instance.state.platform.systemName); - yyjson_mut_obj_add_strbuf(doc, obj, "release", &instance.state.platform.systemRelease); - yyjson_mut_obj_add_strbuf(doc, obj, "version", &instance.state.platform.systemVersion); - yyjson_mut_obj_add_strbuf(doc, obj, "displayVersion", &instance.state.platform.systemDisplayVersion); + yyjson_mut_obj_add_strbuf(doc, obj, "architecture", &info->architecture); + yyjson_mut_obj_add_strbuf(doc, obj, "name", &info->name); + yyjson_mut_obj_add_strbuf(doc, obj, "release", &info->release); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &info->version); + yyjson_mut_obj_add_strbuf(doc, obj, "displayVersion", &info->displayVersion); + yyjson_mut_obj_add_uint(doc, obj, "pageSize", info->pageSize); } void ffPrintKernelHelpFormat(void) @@ -83,6 +89,7 @@ void ffPrintKernelHelpFormat(void) "Version - version", "Architecture - arch", "Display version - display-version", + "Page size - page-size", })); } diff --git a/src/modules/os/os.c b/src/modules/os/os.c index 1e0d6842c..3d1aadebd 100644 --- a/src/modules/os/os.c +++ b/src/modules/os/os.c @@ -19,7 +19,7 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result) else if(os->id.length > 0) ffStrbufAppend(result, &os->id); else - ffStrbufAppend(result, &instance.state.platform.systemName); + ffStrbufAppend(result, &instance.state.platform.sysinfo.name); //Append code name if it is missing if(os->codename.length > 0 && !ffStrbufContainIgnCase(result, &os->codename)) @@ -55,10 +55,10 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result) } //Append architecture if it is missing - if(!ffStrbufContainIgnCase(result, &instance.state.platform.systemArchitecture)) + if(!ffStrbufContainIgnCase(result, &instance.state.platform.sysinfo.architecture)) { ffStrbufAppendC(result, ' '); - ffStrbufAppend(result, &instance.state.platform.systemArchitecture); + ffStrbufAppend(result, &instance.state.platform.sysinfo.architecture); } } @@ -80,10 +80,10 @@ static void buildOutputNixOS(const FFOSResult* os, FFstrbuf* result) ffStrbufAppendC(result, ')'); } - if(instance.state.platform.systemArchitecture.length > 0) + if(instance.state.platform.sysinfo.architecture.length > 0) { ffStrbufAppendC(result, ' '); - ffStrbufAppend(result, &instance.state.platform.systemArchitecture); + ffStrbufAppend(result, &instance.state.platform.sysinfo.architecture); } } @@ -112,7 +112,7 @@ void ffPrintOS(FFOSOptions* options) else { FF_PRINT_FORMAT_CHECKED(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_OS_NUM_FORMAT_ARGS, ((FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemName, "sysname"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.sysinfo.name, "sysname"}, {FF_FORMAT_ARG_TYPE_STRBUF, &os->name, "name"}, {FF_FORMAT_ARG_TYPE_STRBUF, &os->prettyName, "pretty-name"}, {FF_FORMAT_ARG_TYPE_STRBUF, &os->id, "id"}, @@ -123,7 +123,7 @@ void ffPrintOS(FFOSOptions* options) {FF_FORMAT_ARG_TYPE_STRBUF, &os->versionID, "version-id"}, {FF_FORMAT_ARG_TYPE_STRBUF, &os->codename, "codename"}, {FF_FORMAT_ARG_TYPE_STRBUF, &os->buildID, "build-id"}, - {FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemArchitecture, "arch"} + {FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.sysinfo.architecture, "arch"} })); } } diff --git a/src/modules/title/title.c b/src/modules/title/title.c index 115cd61e5..bc5706749 100644 --- a/src/modules/title/title.c +++ b/src/modules/title/title.c @@ -176,7 +176,6 @@ void ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "homeDir", &instance.state.platform.homeDir); yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &instance.state.platform.exePath); yyjson_mut_obj_add_strbuf(doc, obj, "userShell", &instance.state.platform.userShell); - yyjson_mut_obj_add_uint(doc, obj, "pageSize", instance.state.platform.pageSize); } void ffPrintTitleHelpFormat(void) diff --git a/src/modules/version/version.c b/src/modules/version/version.c index 66a4f0fb7..c7c729606 100644 --- a/src/modules/version/version.c +++ b/src/modules/version/version.c @@ -5,17 +5,16 @@ #include "modules/version/version.h" #include "util/stringUtils.h" -#define FF_VERSION_NUM_FORMAT_ARGS 9 +#define FF_VERSION_NUM_FORMAT_ARGS 10 void ffPrintVersion(FFVersionOptions* options) { - FFVersionResult result; - ffDetectVersion(&result); + FFVersionResult* result = &ffVersionResult; if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - printf("%s %s%s%s (%s)\n", result.projectName, result.version, result.versionTweak, result.debugMode ? "-debug" : "", result.architecture); + printf("%s %s%s%s (%s)\n", result->projectName, result->version, result->versionTweak, result->debugMode ? "-debug" : "", result->architecture); } else { @@ -32,14 +31,15 @@ void ffPrintVersion(FFVersionOptions* options) } FF_PRINT_FORMAT_CHECKED(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_VERSION_NUM_FORMAT_ARGS, ((FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRING, result.projectName, "project-name"}, - {FF_FORMAT_ARG_TYPE_STRING, result.version, "version"}, - {FF_FORMAT_ARG_TYPE_STRING, result.versionTweak, "version-tweak"}, - {FF_FORMAT_ARG_TYPE_STRING, result.debugMode ? "debug" : "release", "build-type"}, - {FF_FORMAT_ARG_TYPE_STRING, result.architecture, "arch"}, - {FF_FORMAT_ARG_TYPE_STRING, result.cmakeBuiltType, "cmake-built-type"}, - {FF_FORMAT_ARG_TYPE_STRING, result.compileTime, "compile-time"}, - {FF_FORMAT_ARG_TYPE_STRING, result.compiler, "compiler"}, + {FF_FORMAT_ARG_TYPE_STRING, result->projectName, "project-name"}, + {FF_FORMAT_ARG_TYPE_STRING, result->version, "version"}, + {FF_FORMAT_ARG_TYPE_STRING, result->versionTweak, "version-tweak"}, + {FF_FORMAT_ARG_TYPE_STRING, result->debugMode ? "debug" : "release", "build-type"}, + {FF_FORMAT_ARG_TYPE_STRING, result->sysName, "sysname"}, + {FF_FORMAT_ARG_TYPE_STRING, result->architecture, "arch"}, + {FF_FORMAT_ARG_TYPE_STRING, result->cmakeBuiltType, "cmake-built-type"}, + {FF_FORMAT_ARG_TYPE_STRING, result->compileTime, "compile-time"}, + {FF_FORMAT_ARG_TYPE_STRING, result->compiler, "compiler"}, {FF_FORMAT_ARG_TYPE_STRBUF, &buf, "libc-used"}, })); } @@ -82,18 +82,18 @@ void ffGenerateVersionJsonConfig(FFVersionOptions* options, yyjson_mut_doc* doc, void ffGenerateVersionJsonResult(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FFVersionResult result; - ffDetectVersion(&result); + FFVersionResult* result = &ffVersionResult; yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); - yyjson_mut_obj_add_str(doc, obj, "projectName", result.projectName); - yyjson_mut_obj_add_str(doc, obj, "architecture", result.architecture); - yyjson_mut_obj_add_str(doc, obj, "version", result.version); - yyjson_mut_obj_add_str(doc, obj, "versionTweak", result.versionTweak); - yyjson_mut_obj_add_str(doc, obj, "cmakeBuiltType", result.cmakeBuiltType); - yyjson_mut_obj_add_str(doc, obj, "compileTime", result.compileTime); - yyjson_mut_obj_add_str(doc, obj, "compiler", result.compiler); - yyjson_mut_obj_add_bool(doc, obj, "debugMode", result.debugMode); + yyjson_mut_obj_add_str(doc, obj, "projectName", result->projectName); + yyjson_mut_obj_add_str(doc, obj, "sysName", result->sysName); + yyjson_mut_obj_add_str(doc, obj, "architecture", result->architecture); + yyjson_mut_obj_add_str(doc, obj, "version", result->version); + yyjson_mut_obj_add_str(doc, obj, "versionTweak", result->versionTweak); + yyjson_mut_obj_add_str(doc, obj, "cmakeBuiltType", result->cmakeBuiltType); + yyjson_mut_obj_add_str(doc, obj, "compileTime", result->compileTime); + yyjson_mut_obj_add_str(doc, obj, "compiler", result->compiler); + yyjson_mut_obj_add_bool(doc, obj, "debugMode", result->debugMode); FFLibcResult libcResult; if (ffDetectLibc(&libcResult)) @@ -119,6 +119,7 @@ void ffPrintVersionHelpFormat(void) "Version - version", "Version tweak - version-tweak", "Build type (debug or release) - build-type", + "System name - sysname", "Architecture - arch", "CMake build type when compiling (Debug, Release, RelWithDebInfo, MinSizeRel) - cmake-built-type", "Date time when compiling - compile-time", diff --git a/src/util/platform/FFPlatform.c b/src/util/platform/FFPlatform.c index 8ce979044..cb044f750 100644 --- a/src/util/platform/FFPlatform.c +++ b/src/util/platform/FFPlatform.c @@ -1,6 +1,7 @@ #include "FFPlatform_private.h" #include "util/stringUtils.h" #include "common/io/io.h" +#include "detection/version/version.h" void ffPlatformInit(FFPlatform* platform) { @@ -14,30 +15,20 @@ void ffPlatformInit(FFPlatform* platform) ffStrbufInit(&platform->hostName); ffStrbufInit(&platform->userShell); - ffStrbufInit(&platform->systemName); - ffStrbufInit(&platform->systemRelease); - ffStrbufInit(&platform->systemVersion); - ffStrbufInit(&platform->systemArchitecture); + FFPlatformSysinfo* info = &platform->sysinfo; + + ffStrbufInit(&info->name); + ffStrbufInit(&info->release); + ffStrbufInit(&info->version); + ffStrbufInit(&info->architecture); ffPlatformInitImpl(platform); - if(platform->systemName.length == 0) - { - #if defined(__linux__) - ffStrbufAppendS(&platform->systemName, "Linux"); - #elif defined(__FreeBSD__) - ffStrbufAppendS(&platform->systemName, "FreeBSD"); - #elif defined(__APPLE__) - ffStrbufAppendS(&platform->systemName, "Darwin"); - #elif defined(_WIN32) - ffStrbufAppendS(&platform->systemName, "Windows_NT"); - #else - ffStrbufAppendS(&platform->systemName, "Unknown"); - #endif - } + if(info->name.length == 0) + ffStrbufSetStatic(&info->name, ffVersionResult.sysName); - if(platform->systemArchitecture.length == 0) - ffStrbufAppendS(&platform->systemArchitecture, "Unknown"); + if(info->architecture.length == 0) + ffStrbufSetStatic(&info->architecture, ffVersionResult.architecture); } void ffPlatformDestroy(FFPlatform* platform) @@ -58,11 +49,12 @@ void ffPlatformDestroy(FFPlatform* platform) ffStrbufDestroy(&platform->hostName); ffStrbufDestroy(&platform->userShell); - ffStrbufDestroy(&platform->systemArchitecture); - ffStrbufDestroy(&platform->systemName); - ffStrbufDestroy(&platform->systemRelease); - ffStrbufDestroy(&platform->systemVersion); - ffStrbufDestroy(&platform->systemDisplayVersion); + FFPlatformSysinfo* info = &platform->sysinfo; + ffStrbufDestroy(&info->architecture); + ffStrbufDestroy(&info->name); + ffStrbufDestroy(&info->release); + ffStrbufDestroy(&info->version); + ffStrbufDestroy(&info->displayVersion); } void ffPlatformPathAddAbsolute(FFlist* dirs, const char* path) diff --git a/src/util/platform/FFPlatform.h b/src/util/platform/FFPlatform.h index 8594e2039..5dd6c0efa 100644 --- a/src/util/platform/FFPlatform.h +++ b/src/util/platform/FFPlatform.h @@ -3,7 +3,18 @@ #include "util/FFstrbuf.h" #include "util/FFlist.h" -typedef struct FFPlatform { +typedef struct FFPlatformSysinfo +{ + FFstrbuf name; + FFstrbuf release; + FFstrbuf version; + FFstrbuf architecture; + FFstrbuf displayVersion; + uint32_t pageSize; +} FFPlatformSysinfo; + +typedef struct FFPlatform +{ FFstrbuf homeDir; // Trailing slash included FFstrbuf cacheDir; // Trailing slash included FFlist configDirs; // List of FFstrbuf, trailing slash included @@ -14,13 +25,7 @@ typedef struct FFPlatform { FFstrbuf hostName; FFstrbuf userShell; - FFstrbuf systemName; - FFstrbuf systemRelease; - FFstrbuf systemVersion; - FFstrbuf systemArchitecture; - FFstrbuf systemDisplayVersion; - - uint32_t pageSize; + FFPlatformSysinfo sysinfo; } FFPlatform; void ffPlatformInit(FFPlatform* platform); diff --git a/src/util/platform/FFPlatform_unix.c b/src/util/platform/FFPlatform_unix.c index 4ba4d2a5f..b442eed54 100644 --- a/src/util/platform/FFPlatform_unix.c +++ b/src/util/platform/FFPlatform_unix.c @@ -169,13 +169,19 @@ static void getUserShell(FFPlatform* platform, const struct passwd* pwd) ffStrbufAppendS(&platform->userShell, shell); } -static void getPageSize(FFPlatform* platform) +static void getSysinfo(FFPlatformSysinfo* info, const struct utsname* uts) { + ffStrbufAppendS(&info->name, uts->sysname); + ffStrbufAppendS(&info->release, uts->release); + ffStrbufAppendS(&info->version, uts->version); + ffStrbufAppendS(&info->architecture, uts->machine); + ffStrbufInit(&info->displayVersion); + #if defined(__FreeBSD__) || defined(__APPLE__) - size_t length = sizeof(platform->pageSize); - sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &platform->pageSize, &length, NULL, 0); + size_t length = sizeof(info->pageSize); + sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &info->pageSize, &length, NULL, 0); #else - platform->pageSize = (uint32_t) sysconf(_SC_PAGESIZE); + info->pageSize = (uint32_t) sysconf(_SC_PAGESIZE); #endif } @@ -197,11 +203,5 @@ void ffPlatformInitImpl(FFPlatform* platform) getHostName(platform, &uts); getUserShell(platform, pwd); - ffStrbufAppendS(&platform->systemName, uts.sysname); - ffStrbufAppendS(&platform->systemRelease, uts.release); - ffStrbufAppendS(&platform->systemVersion, uts.version); - ffStrbufAppendS(&platform->systemArchitecture, uts.machine); - ffStrbufInit(&platform->systemDisplayVersion); - - getPageSize(platform); + getSysinfo(&platform->sysinfo, &uts); } diff --git a/src/util/platform/FFPlatform_windows.c b/src/util/platform/FFPlatform_windows.c index d5c690c63..961f88237 100644 --- a/src/util/platform/FFPlatform_windows.c +++ b/src/util/platform/FFPlatform_windows.c @@ -159,7 +159,7 @@ static void getUserShell(FFPlatform* platform) ffStrbufReplaceAllC(&platform->userShell, '\\', '/'); } -static void getSystemReleaseAndVersion(FFPlatform* platform) +static void getSystemReleaseAndVersion(FFPlatformSysinfo* info) { RTL_OSVERSIONINFOW osVersion = { .dwOSVersionInfoSize = sizeof(osVersion) }; if (!NT_SUCCESS(RtlGetVersion(&osVersion))) @@ -172,39 +172,39 @@ static void getSystemReleaseAndVersion(FFPlatform* platform) uint32_t ubr = 0; ffRegReadUint(hKey, L"UBR", &ubr, NULL); - ffStrbufAppendF(&platform->systemRelease, + ffStrbufAppendF(&info->release, "%u.%u.%u.%u", (unsigned) osVersion.dwMajorVersion, (unsigned) osVersion.dwMinorVersion, (unsigned) osVersion.dwBuildNumber, (unsigned) ubr); - ffStrbufInit(&platform->systemDisplayVersion); - if(!ffRegReadStrbuf(hKey, L"DisplayVersion", &platform->systemDisplayVersion, NULL)) + ffStrbufInit(&info->displayVersion); + if(!ffRegReadStrbuf(hKey, L"DisplayVersion", &info->displayVersion, NULL)) { if (osVersion.szCSDVersion[0]) - ffStrbufSetWS(&platform->systemDisplayVersion, osVersion.szCSDVersion); + ffStrbufSetWS(&info->displayVersion, osVersion.szCSDVersion); else - ffRegReadStrbuf(hKey, L"ReleaseId", &platform->systemDisplayVersion, NULL); // For old Windows 10 + ffRegReadStrbuf(hKey, L"ReleaseId", &info->displayVersion, NULL); // For old Windows 10 } - ffRegReadStrbuf(hKey, L"BuildLabEx", &platform->systemVersion, NULL); + ffRegReadStrbuf(hKey, L"BuildLabEx", &info->version, NULL); switch (osVersion.dwPlatformId) { case VER_PLATFORM_WIN32s: - ffStrbufSetStatic(&platform->systemName, "WIN32s"); + ffStrbufSetStatic(&info->name, "WIN32s"); break; case VER_PLATFORM_WIN32_WINDOWS: - ffStrbufSetStatic(&platform->systemName, "WIN32_WINDOWS"); + ffStrbufSetStatic(&info->name, "WIN32_WINDOWS"); break; case VER_PLATFORM_WIN32_NT: - ffStrbufSetStatic(&platform->systemName, "WIN32_NT"); + ffStrbufSetStatic(&info->name, "WIN32_NT"); break; } } -static void getSystemArchitectureAndPageSize(FFPlatform* platform) +static void getSystemArchitectureAndPageSize(FFPlatformSysinfo* info) { SYSTEM_INFO sysInfo; GetNativeSystemInfo(&sysInfo); @@ -212,52 +212,53 @@ static void getSystemArchitectureAndPageSize(FFPlatform* platform) switch(sysInfo.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_AMD64: - ffStrbufSetStatic(&platform->systemArchitecture, "x86_64"); + ffStrbufSetStatic(&info->architecture, "x86_64"); break; case PROCESSOR_ARCHITECTURE_IA64: - ffStrbufSetStatic(&platform->systemArchitecture, "ia64"); + ffStrbufSetStatic(&info->architecture, "ia64"); break; case PROCESSOR_ARCHITECTURE_INTEL: switch (sysInfo.wProcessorLevel) { case 4: - ffStrbufSetStatic(&platform->systemArchitecture, "i486"); + ffStrbufSetStatic(&info->architecture, "i486"); break; case 5: - ffStrbufSetStatic(&platform->systemArchitecture, "i586"); + ffStrbufSetStatic(&info->architecture, "i586"); break; case 6: - ffStrbufSetStatic(&platform->systemArchitecture, "i686"); + ffStrbufSetStatic(&info->architecture, "i686"); break; default: - ffStrbufSetStatic(&platform->systemArchitecture, "i386"); + ffStrbufSetStatic(&info->architecture, "i386"); break; } break; case PROCESSOR_ARCHITECTURE_ARM64: - ffStrbufSetStatic(&platform->systemArchitecture, "aarch64"); + ffStrbufSetStatic(&info->architecture, "aarch64"); break; case PROCESSOR_ARCHITECTURE_ARM: - ffStrbufSetStatic(&platform->systemArchitecture, "arm"); + ffStrbufSetStatic(&info->architecture, "arm"); break; case PROCESSOR_ARCHITECTURE_PPC: - ffStrbufSetStatic(&platform->systemArchitecture, "ppc"); + ffStrbufSetStatic(&info->architecture, "ppc"); break; case PROCESSOR_ARCHITECTURE_MIPS: - ffStrbufSetStatic(&platform->systemArchitecture, "mips"); + ffStrbufSetStatic(&info->architecture, "mips"); break; case PROCESSOR_ARCHITECTURE_ALPHA: - ffStrbufSetStatic(&platform->systemArchitecture, "alpha"); + ffStrbufSetStatic(&info->architecture, "alpha"); break; case PROCESSOR_ARCHITECTURE_ALPHA64: - ffStrbufSetStatic(&platform->systemArchitecture, "alpha64"); + ffStrbufSetStatic(&info->architecture, "alpha64"); break; case PROCESSOR_ARCHITECTURE_UNKNOWN: default: + ffStrbufSetStatic(&info->architecture, "unknown"); break; } - platform->pageSize = sysInfo.dwPageSize; + info->pageSize = sysInfo.dwPageSize; } void ffPlatformInitImpl(FFPlatform* platform) @@ -272,6 +273,6 @@ void ffPlatformInitImpl(FFPlatform* platform) getHostName(platform); getUserShell(platform); - getSystemReleaseAndVersion(platform); - getSystemArchitectureAndPageSize(platform); + getSystemReleaseAndVersion(&platform->sysinfo); + getSystemArchitectureAndPageSize(&platform->sysinfo); }