From fcf5680e72b218d41b66619eede39d3bb187ad59 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Tue, 28 Oct 2025 10:50:38 +0100 Subject: [PATCH] [bugfix] removed double definition of structs --- source/lib.c | 47 ++++++++++++++++++++--------------------------- source/main.c | 29 ++++++++++++++++++++++------- source/types.h | 8 ++++---- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/source/lib.c b/source/lib.c index 67e0eed..a52b2a1 100644 --- a/source/lib.c +++ b/source/lib.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "types.h" #include "lib.h" @@ -40,11 +41,6 @@ void device_os_version(); void device_hostname(); void device_model(); -cpu_s *u_cpu ; -ram_s *u_ram ; -disk_s *u_disk ; -device_s *u_device ; - #ifdef __gnu_linux__ #include @@ -183,48 +179,51 @@ void device_mdoel() { void cpu_name() { - char *name; + char *name = NULL; size_t len = 0; - if (sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0) < 0) - perror("errorn in assigning the size for the cpu name variable\n"); + if (sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0) < 0){ + + perror("error in assigning the size for the cpu name variable\n"); + return; + } name = malloc(len); + if (name == NULL) { + perror("malloc failed"); + return; + } + if(sysctlbyname("machdep.cpu.brand_string", name, &len, NULL, 0) < 0){ perror("error in assigning the value to the cpu name variable\n"); - free(name); return; } u_cpu->name = name; - return; } void cpu_threads() { int count; size_t len = sizeof(count); - if (sysctlbyname("machdep.cpu.thread_count", &count, &len, NULL, 0) < 0) + if (sysctlbyname("machdep.cpu.thread_count", &count, &len, NULL, 0) < 0){ perror("error in retrieving the cpu threads count\n"); + return; + } u_cpu->threads = count; - return; } void cpu_frequency() { - uint64_t freq = 0; - size_t size = sizeof(freq); + // assigning a value of 0, value not available on macos + u_cpu->frequency = 0; - if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0) - { - perror("sysctl"); - } - return; } void cpu_temperature() { + // assigning a value of 0, value not available on macos u_cpu->temperature = 0; } @@ -247,14 +246,12 @@ void mem_av_size() { } void get_total() { - - + } void get_usage() { - } void device_hostname(){ @@ -306,13 +303,12 @@ void device_model(){ } u_device->name = model_name; - return; } void device_os_version() { - char *os_version; + char *os_version = NULL; size_t size = 0; if (sysctlbyname("kern.ostype", NULL, &size, NULL, 0) < 0) @@ -327,9 +323,6 @@ void device_os_version() { } u_device->os_version = os_version; - return; - - } #endif diff --git a/source/main.c b/source/main.c index 12edf48..5f679b8 100644 --- a/source/main.c +++ b/source/main.c @@ -11,24 +11,27 @@ device_s *u_device; void init() { + u_cpu = malloc( sizeof(cpu_s) ); + u_ram = malloc(sizeof(ram_s)); + u_disk = malloc(sizeof(disk_s)); + u_device = malloc(sizeof(device_s)); + cpu_name(); cpu_frequency(); cpu_threads(); cpu_temperature(); + mem_size(); mem_av_size(); + get_total(); get_usage(); + device_os_version(); device_model(); device_up_time(); device_model(); - u_cpu = malloc(sizeof(cpu_s)); - u_ram = malloc(sizeof(disk_s)); - u_disk = malloc(sizeof(disk_s)); - u_cpu = malloc(sizeof(device_s)); - } void end() { @@ -44,8 +47,20 @@ int main() { init(); - printf("%d\n%s\n%s\n%d\n", u_cpu->temperature, u_cpu->frequency, u_cpu->name, u_cpu->threads); - printf("%s\n%s\n%s\n%d", u_device->name, u_device->hostname, u_device->os_version, u_device->uptime); + printf("temperature: %d\nfrequency: %s\nname: %s\nthreads: %d\n", u_cpu->temperature, u_cpu->frequency, u_cpu->name, u_cpu->threads); + printf("total: %ld\navailable: %ld\n", u_ram->total, u_ram->available); + printf("device name:%s\ndevice hostname:%s\nos version: %s\nuptime: %d\n", u_device->name, u_device->hostname, u_device->os_version, u_device->uptime); + + while(1) { + + char *input = malloc(sizeof(char *)); + + if(scanf("%s\n", input)) { + + + } + } + end(); return 0; } diff --git a/source/types.h b/source/types.h index 48d1781..bf720b6 100644 --- a/source/types.h +++ b/source/types.h @@ -11,13 +11,13 @@ typedef struct { } cpu_s; typedef struct { - unsigned long total; - unsigned long available; + long int total; + long int available; } ram_s; typedef struct { - long long size; - short name; + long size; + char *name; } disk_s; typedef struct {