From fe22f07134f71176c084cdd833cafbc2ce6e2819 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 15 Dec 2023 22:24:22 +0300 Subject: [PATCH] os parser fix --- include/fetch.h | 4 +++- src/fetch.c | 27 ++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/fetch.h b/include/fetch.h index 7feb02a..7abf5c0 100644 --- a/include/fetch.h +++ b/include/fetch.h @@ -14,7 +14,9 @@ #define OS_SIZE 128 typedef struct { - char os_name[OS_SIZE + 1]; + char os_buf[OS_SIZE + 1]; + char *os_name; + struct utsname uts; char *pkg_cmd; diff --git a/src/fetch.c b/src/fetch.c index 2a823c0..ac7cba8 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -18,24 +18,29 @@ FETCH Init(void) { exit(1); } - memset(fetch.os_name, '\0', sizeof(fetch.os_name)); - if (GetOs(fetch.os_name, sizeof(fetch.os_name))) - snprintf(fetch.os_name, sizeof(fetch.os_name), "%s", fetch.uts.sysname); + memset(fetch.os_buf, '\0', sizeof(fetch.os_buf)); + int ret = GetOs(fetch.os_buf, sizeof(fetch.os_buf)); + fetch.os_name = fetch.os_buf; + + if (ret) + snprintf(fetch.os_buf, sizeof(fetch.os_buf), "%s", fetch.uts.sysname); + + else + fetch.os_name += strlen("PRETTY_NAME=\""); fetch.font_color = FONT_COLOR; GetArt(&fetch); return fetch; } -int GetOs(char *os_name, size_t len) { +int GetOs(char *buf, size_t len) { FILE *fp = fopen("/etc/os-release", "r"); if (fp == NULL) return 1; - while (fgets(os_name, len, fp)) { - if (!strncmp(os_name, "PRETTY_NAME=\"", 5)) { - os_name[strlen(os_name) - 2] = '\0'; - snprintf(os_name, len, "%s", os_name + strlen("PRETTY_NAME=\"")); + while (fgets(buf, len, fp)) { + if (!strncmp(buf, "PRETTY_NAME=\"", 5)) { + buf[strlen(buf) - 2] = '\0'; fclose(fp); return 0; @@ -185,12 +190,12 @@ int GetMem(const char *title, const FETCH fetch) { if (fp == NULL) return 1; - unsigned int free = 0, cache = 0, total = 0, unused; - if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\nBuffers: %u kB\nCached: %u kB", &total, &free, &unused, &unused, &cache) < 0) + unsigned int unused = 0, total = 0, available = 0; + if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\n", &total, &unused, &available) < 0) return 1; UNUSED(unused); - printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (free + cache) / 1024, total / 1024); + printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (total - available) / 1024, total / 1024); fclose(fp); return 0;