fix os parser

This commit is contained in:
Your Name 2023-12-16 18:58:30 +03:00
parent fe22f07134
commit d4571a3f14
2 changed files with 15 additions and 16 deletions

View File

@ -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;

View File

@ -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;