From 729746e50ebc8e4eb4c98f7556a241aec111e07d Mon Sep 17 00:00:00 2001 From: Anser Date: Tue, 11 Aug 2026 13:11:39 +0200 Subject: some more rework in readme.md and src file structure --- src/sysinfo.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/sysinfo.c (limited to 'src/sysinfo.c') diff --git a/src/sysinfo.c b/src/sysinfo.c new file mode 100644 index 0000000..0a91eca --- /dev/null +++ b/src/sysinfo.c @@ -0,0 +1,85 @@ +#include "../include/cfetch.h" +#include + +void get_os(system_info_t *info) { +#ifdef __linux__ + struct utsname u; + + if (uname(&u) == 0) { + size_t length = printf("OS: %s %s %s %s\n", u.sysname, u.release, u.version, u.machine); + printf("line length: %ld\n", length); + } else { + perror("Error while accessing uname"); + return; + } +#endif + return; +} + +void get_cpu(system_info_t *info) { + FILE* file = fopen("/proc/cpuinfo", "r"); + + //check file existence + if (!file) { + perror("/proc/cpuinfo not found"); + return; + } + + char line[256]; + char *model_name = NULL; + + //read per line + while (fgets(line, sizeof(line), file)) { + char* p = strstr(line, "model name"); + + if (p != NULL) { + model_name = strchr(p, ':') + 1; //skip : + break; + } + } + + if (model_name != NULL) { + char *string = string_format(model_name); + + if (string == NULL) { + printf("CPU: Error string not found.\n"); + free(string); + model_name = NULL; + } else { + printf("CPU: %s\n", string); + free(string); + model_name = NULL; + } + + } else { + printf("CPU: Error model_name not found.\n"); + } + + fclose(file); + return; +} + +void get_memory(system_info_t *info) { + FILE *file = fopen("/proc/meminfo", "r"); + + if (!file) { + perror("Error fopen meminfo"); + return; + } + + char line[256]; + long total = 0; + long available = 0; + + // read per line + while (fgets(line, sizeof(line), file)) { + if (sscanf(line, "MemTotal: %ld kB", &total) == 1) continue; + if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) continue; + } + + fclose(file); + + printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); + + return; +} -- cgit v1.2.3