aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnser <rostgans@tutamail.com>2026-08-17 11:51:54 +0200
committerAnser <rostgans@tutamail.com>2026-08-17 11:54:00 +0200
commitab497e439d9c8c41d7f43998a7b782fe4720d856 (patch)
tree6d1589c0e950b52125c6d516fa31152173d62ff9 /src
parent5f068ebcc05703ac7e4f1051fbe5eaf21f0d7f75 (diff)
added OS/distro name
Diffstat (limited to 'src')
-rw-r--r--src/ascii.c1
-rw-r--r--src/cfetch.c1
-rw-r--r--src/sysinfo.c72
3 files changed, 73 insertions, 1 deletions
diff --git a/src/ascii.c b/src/ascii.c
index 328ec7f..5fc81be 100644
--- a/src/ascii.c
+++ b/src/ascii.c
@@ -79,6 +79,7 @@ void print_ascii_art(struct system_info_t *info)
void print_system_info(struct system_info_t *info)
{
+ printf("OS: %s\n", info->distro);
printf("Kernel: %s\n", info->kernel);
printf("CPU: %s\n", info->cpu);
printf("%s\n", info->memory);
diff --git a/src/cfetch.c b/src/cfetch.c
index c0ea8ab..5b41121 100644
--- a/src/cfetch.c
+++ b/src/cfetch.c
@@ -45,6 +45,7 @@ int main(int argc, char **argv)
get_kernel(&info);
get_cpu(&info);
get_memory(&info);
+ get_os_release(&info);
print_system_info(&info);
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;
+}