diff options
| author | Anser <rostgans@tutamail.com> | 2026-08-17 11:51:54 +0200 |
|---|---|---|
| committer | Anser <rostgans@tutamail.com> | 2026-08-17 11:54:00 +0200 |
| commit | ab497e439d9c8c41d7f43998a7b782fe4720d856 (patch) | |
| tree | 6d1589c0e950b52125c6d516fa31152173d62ff9 /src/sysinfo.c | |
| parent | 5f068ebcc05703ac7e4f1051fbe5eaf21f0d7f75 (diff) | |
added OS/distro name
Diffstat (limited to 'src/sysinfo.c')
| -rw-r--r-- | src/sysinfo.c | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/src/sysinfo.c b/src/sysinfo.c index 7d72e02..5142197 100644 --- a/src/sysinfo.c +++ b/src/sysinfo.c @@ -17,8 +17,9 @@ #include "../include/cfetch.h" -#include <stdio.h> +#include <stdbool.h> #include <sys/utsname.h> +#include <unistd.h> void get_kernel(struct system_info_t *info) { @@ -92,3 +93,72 @@ void get_memory(struct system_info_t *info) return; } + +bool get_os_release_value(char *line, const char *key, char *buffer, size_t size_out) +{ + if (!line || !key || !buffer || size_out == 0) return false; + + size_t key_len = strlen(key); + if (key_len == 0) return false; + + if (strncmp(line, key, key_len) != 0) return false; + + // '=' pointer + char *eq = strchr(line, '='); + if (!eq) return false; + char *val = trim_whitespace(eq + 1); + + if (*val == '"' || *val == '\'') { + char quote = *val; // safe which char was used + val++; + + char *end = strchr(val, quote); + if (end != NULL) { + *end = '\0'; + } else { + char *nl = strchr(val, '\n'); + if (nl) *nl = '\0'; + } + } else { + char *nl = strchr(val, '\n'); + if (nl) *nl = '\0'; + } + + snprintf(buffer, size_out, "%s", val); + return true; +} + +void get_os_release(struct system_info_t *info) +{ + FILE *file; + char line[256]; + char id[64] = { 0 }; + char version_id[64] = { 0 }; + char pretty_name[64] = { 0 }; + + file = fopen("/etc/os-release", "r"); + if (!file) { + file = fopen("/usr/lib/os-release", "r"); + } + if (!file) { + snprintf(info->distro, sizeof(info->distro), "Unknown"); + return; + } + + // get ID, PRETTY_NAME, VERSION_ID + while (fgets(line, sizeof(line), file)) { + if (get_os_release_value(line, "ID", id, sizeof(id))) continue; + if (get_os_release_value(line, "PRETTY_NAME", pretty_name, sizeof(pretty_name))) continue; + if (get_os_release_value(line, "VERSION_ID", version_id, sizeof(version_id))) continue; + } + + // set OS + if (pretty_name[0] != '\0') { + snprintf(info->distro, sizeof(info->distro), "%s", pretty_name); + } else if (id[0] != '\0' && version_id[0] != '\0') { + snprintf(info->distro, sizeof(info->distro), "%s %s", id, version_id); + } + + fclose(file); + return; +} |
