From d4571a3f14892a5e39bb85c6dc82b71e0fe84623 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 16 Dec 2023 18:58:30 +0300 Subject: [PATCH] fix os parser --- include/fetch.h | 4 +--- src/fetch.c | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/include/fetch.h b/include/fetch.h index 7abf5c0..7feb02a 100644 --- a/include/fetch.h +++ b/include/fetch.h @@ -14,9 +14,7 @@ #define OS_SIZE 128 typedef struct { - char os_buf[OS_SIZE + 1]; - char *os_name; - + char os_name[OS_SIZE + 1]; struct utsname uts; char *pkg_cmd; diff --git a/src/fetch.c b/src/fetch.c index ac7cba8..edc5551 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -18,15 +18,9 @@ FETCH Init(void) { exit(1); } - 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=\""); + 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); fetch.font_color = FONT_COLOR; GetArt(&fetch); @@ -41,8 +35,15 @@ int GetOs(char *buf, size_t len) { while (fgets(buf, len, fp)) { if (!strncmp(buf, "PRETTY_NAME=\"", 5)) { buf[strlen(buf) - 2] = '\0'; - fclose(fp); + + char *tmp = strdup(buf); + if (tmp == NULL) + return 1; + + snprintf(buf, len, "%s", tmp + strlen("PRETTY_NAME=\"")); + free(tmp); + return 0; } } @@ -190,12 +191,12 @@ int GetMem(const char *title, const FETCH fetch) { if (fp == NULL) return 1; - 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) + unsigned int unused = 0, total = 0, available = 0, buffer = 0, cached = 0; + if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\nBuffers: %u kB\nCached: %u kB", &total, &unused, &available, &buffer, &cached) < 0) return 1; UNUSED(unused); - printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (total - available) / 1024, total / 1024); + printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (total - available - buffer - cached) / 1024, total / 1024); fclose(fp); return 0;