Skip to content

Commit

Permalink
apply change to host util
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-coding committed Sep 14, 2024
1 parent 1716008 commit 72a4615
Showing 1 changed file with 23 additions and 56 deletions.
79 changes: 23 additions & 56 deletions code/logic/hostsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
#include "fossil/lib/hostsys.h"
#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
Expand All @@ -27,32 +28,28 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/mach.h>
#endif

static bool fossil_hostsys_get_endian(fossil_hostsystem_t *info) {
unsigned int num = 1;
char *endian_check = (char*)&num;

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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -128,8 +127,6 @@ static bool fossil_hostsys_get_linux(fossil_hostsystem_t *info) {
}

#elif defined(__APPLE__)
#include <mach/mach.h>

static bool fossil_hostsys_get_macos(fossil_hostsystem_t *info) {
struct utsname unameData;
memset(info, 0, sizeof(fossil_hostsystem_t));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -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));
}

0 comments on commit 72a4615

Please sign in to comment.