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
typedef struct {
char os_name[OS_SIZE + 1];
char os_buf[OS_SIZE + 1];
char *os_name;
struct utsname uts;
char *pkg_cmd;

View File

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