os parser fix

This commit is contained in:
Your Name 2023-12-15 22:24:22 +03:00
parent e6dc62953b
commit fe22f07134
2 changed files with 19 additions and 12 deletions

View File

@ -14,7 +14,9 @@
#define OS_SIZE 128 #define OS_SIZE 128
typedef struct { typedef struct {
char os_name[OS_SIZE + 1]; char os_buf[OS_SIZE + 1];
char *os_name;
struct utsname uts; struct utsname uts;
char *pkg_cmd; char *pkg_cmd;

View File

@ -18,24 +18,29 @@ FETCH Init(void) {
exit(1); exit(1);
} }
memset(fetch.os_name, '\0', sizeof(fetch.os_name)); memset(fetch.os_buf, '\0', sizeof(fetch.os_buf));
if (GetOs(fetch.os_name, sizeof(fetch.os_name))) int ret = GetOs(fetch.os_buf, sizeof(fetch.os_buf));
snprintf(fetch.os_name, sizeof(fetch.os_name), "%s", fetch.uts.sysname); 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; fetch.font_color = FONT_COLOR;
GetArt(&fetch); GetArt(&fetch);
return 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"); FILE *fp = fopen("/etc/os-release", "r");
if (fp == NULL) if (fp == NULL)
return 1; return 1;
while (fgets(os_name, len, fp)) { while (fgets(buf, len, fp)) {
if (!strncmp(os_name, "PRETTY_NAME=\"", 5)) { if (!strncmp(buf, "PRETTY_NAME=\"", 5)) {
os_name[strlen(os_name) - 2] = '\0'; buf[strlen(buf) - 2] = '\0';
snprintf(os_name, len, "%s", os_name + strlen("PRETTY_NAME=\""));
fclose(fp); fclose(fp);
return 0; return 0;
@ -185,12 +190,12 @@ int GetMem(const char *title, const FETCH fetch) {
if (fp == NULL) if (fp == NULL)
return 1; return 1;
unsigned int free = 0, cache = 0, total = 0, unused; unsigned int unused = 0, total = 0, available = 0;
if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\nBuffers: %u kB\nCached: %u kB", &total, &free, &unused, &unused, &cache) < 0) if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\n", &total, &unused, &available) < 0)
return 1; return 1;
UNUSED(unused); 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); fclose(fp);
return 0; return 0;