From 72a46151e861c4b6f1b6852f9c0cfb7d78a4e695 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sat, 14 Sep 2024 11:11:58 -0600 Subject: [PATCH] apply change to host util --- code/logic/hostsys.c | 79 +++++++++++++------------------------------- 1 file changed, 23 insertions(+), 56 deletions(-) diff --git a/code/logic/hostsys.c b/code/logic/hostsys.c index 14b82e8..84a77b8 100644 --- a/code/logic/hostsys.c +++ b/code/logic/hostsys.c @@ -13,6 +13,7 @@ */ #include "fossil/lib/hostsys.h" #include +#include #ifdef _WIN32 #include @@ -27,32 +28,28 @@ #include #include #include + #include #endif static bool fossil_hostsys_get_endian(fossil_hostsystem_t *info) { unsigned int num = 1; char *endian_check = (char*)# - if (*endian_check == 1) { - info->is_big_endian = false; - } else { - info->is_big_endian = true; - } - + info->is_big_endian = (*endian_check == 0); return true; } #ifdef _WIN32 static bool fossil_hostsys_get_windows(fossil_hostsystem_t *info) { - OSVERSIONINFO osvi; + OSVERSIONINFOEX osvi; SYSTEM_INFO si; MEMORYSTATUSEX memInfo; memset(info, 0, sizeof(fossil_hostsystem_t)); - ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - - if (!GetVersionEx(&osvi)) { + ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); + + if (!GetVersionEx((OSVERSIONINFO*)&osvi)) { fprintf(stderr, "Error getting Windows version information\n"); return false; } @@ -62,10 +59,11 @@ static bool fossil_hostsys_get_windows(fossil_hostsystem_t *info) { osvi.dwMajorVersion, osvi.dwMinorVersion); GetNativeSystemInfo(&si); - snprintf(info->cpu_model, sizeof(info->cpu_model), "Intel"); + snprintf(info->cpu_model, sizeof(info->cpu_model), "Intel"); // Replace with actual model if available info->cpu_cores = si.dwNumberOfProcessors; + memInfo.dwLength = sizeof(MEMORYSTATUSEX); if (!GlobalMemoryStatusEx(&memInfo)) { fprintf(stderr, "Error getting memory information\n"); return false; @@ -99,7 +97,8 @@ static bool fossil_hostsys_get_linux(fossil_hostsystem_t *info) { if (strstr(line, "model name")) { char *pos = strchr(line, ':'); if (pos) { - strncpy(info->cpu_model, pos + 2, sizeof(info->cpu_model)); + strncpy(info->cpu_model, pos + 2, sizeof(info->cpu_model) - 1); + info->cpu_model[sizeof(info->cpu_model) - 1] = '\0'; // Ensure null-termination break; } } @@ -128,8 +127,6 @@ static bool fossil_hostsys_get_linux(fossil_hostsystem_t *info) { } #elif defined(__APPLE__) -#include - static bool fossil_hostsys_get_macos(fossil_hostsystem_t *info) { struct utsname unameData; memset(info, 0, sizeof(fossil_hostsystem_t)); @@ -171,8 +168,9 @@ static bool fossil_hostsys_get_macos(fossil_hostsystem_t *info) { mach_port_t host_port = mach_host_self(); mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_statistics_data_t vm_stats; - - if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count) != KERN_SUCCESS) { + kern_return_t kr = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count); + + if (kr != KERN_SUCCESS) { fprintf(stderr, "Error getting free memory information using mach API\n"); return false; } @@ -182,16 +180,6 @@ static bool fossil_hostsys_get_macos(fossil_hostsystem_t *info) { } #endif -/** - * @brief Retrieves the system information and stores it in the provided fossil_hostsystem_t structure. - * - * This function retrieves various system information such as the operating system name, - * version, CPU model, number of CPU cores, total memory, and free memory. The information - * is stored in the provided fossil_hostsystem_t structure. - * - * @param info Pointer to the fossil_hostsystem_t structure where the system information will be stored. - * @return Returns true if the system information was successfully retrieved, otherwise false. - */ bool fossil_hostsys_get(fossil_hostsystem_t *info) { bool result = false; @@ -212,37 +200,16 @@ bool fossil_hostsys_get(fossil_hostsystem_t *info) { } } -/** - * @brief Returns a string indicating the endianness of the system. - * - * This function checks the endianness of the system and returns a string indicating - * whether the system is big endian or little endian. - * - * @param info Pointer to the fossil_hostsystem_t structure containing the system information. - * @return Returns a string "Big Endian" if the system is big endian, otherwise "Little Endian". - */ const char* fossil_hostsys_endian(fossil_hostsystem_t *info) { - if (info->is_big_endian) { - return "Big Endian"; - } else { - return "Little Endian"; - } + return info->is_big_endian ? "Big Endian" : "Little Endian"; } -/** - * @brief Prints the system information to the standard output. - * - * This function prints the retrieved system information to the standard output, - * including the operating system name, version, CPU model, number of CPU cores, - * total memory, free memory, and endianness. - * - * @param info Pointer to the fossil_hostsystem_t structure containing the system information. - */ void fossil_hostsys_print(fossil_hostsystem_t *info) { - printf("Operating System: %s %s\n", info->os_name, info->os_version); - printf("CPU: %s\n", info->cpu_model); - printf("Number of Cores: %d\n", info->cpu_cores); - printf("Total Memory: %d MB\n", (int)info->total_memory); - printf("Free Memory: %d MB\n", (int)info->free_memory); - printf("Endian: %s\n", fossil_hostsys_endian(info)); + printf("Operating System: %s\n", info->os_name); + printf("Version: %s\n", info->os_version); + printf("CPU Model: %s\n", info->cpu_model); + printf("CPU Cores: %d\n", info->cpu_cores); + printf("Total Memory: %d MB\n", info->total_memory); + printf("Free Memory: %d MB\n", info->free_memory); + printf("Endianness: %s\n", fossil_hostsys_endian(info)); }