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 | |
| parent | 5f068ebcc05703ac7e4f1051fbe5eaf21f0d7f75 (diff) | |
added OS/distro name
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | ascii/east_germany.ascii | 2 | ||||
| -rw-r--r-- | include/cfetch.h | 4 | ||||
| -rw-r--r-- | src/ascii.c | 1 | ||||
| -rw-r--r-- | src/cfetch.c | 1 | ||||
| -rw-r--r-- | src/sysinfo.c | 72 |
6 files changed, 81 insertions, 4 deletions
@@ -15,6 +15,7 @@ - randomize the ascii art - change the way one chooses the ascii art for easy change +- coloured ascii art ## Building / Installation @@ -68,11 +69,11 @@ cfetch | Feature | Linux | |---------|-------| | OS Info | OK | -| Kernel | todo | +| Kernel | OK | | CPU | OK | | Memory | OK | +| ascii_art | basic | | username | todo | -| ascii_art | todo | ## Reference diff --git a/ascii/east_germany.ascii b/ascii/east_germany.ascii index 33f1d63..f9f8b74 100644 --- a/ascii/east_germany.ascii +++ b/ascii/east_germany.ascii @@ -1,6 +1,6 @@ ╔════════════════════════╗ ║ ║ -║ ☭ DDR ☭ ║ +║ ☭ DDR ☭ ║ ║ Deutsche Demokratische ║ ║ Republik ║ ║ ║ diff --git a/include/cfetch.h b/include/cfetch.h index 47a21d8..6b4f7b7 100644 --- a/include/cfetch.h +++ b/include/cfetch.h @@ -31,6 +31,7 @@ struct system_info_t { char user[64]; char kernel[128]; + char distro[128]; char memory[64]; char cpu[64]; char debug; @@ -52,8 +53,11 @@ void print_ascii_art(struct system_info_t *info); void get_cpu(struct system_info_t *info); void get_memory(struct system_info_t *info); void get_kernel(struct system_info_t *info); +void get_os_release(struct system_info_t *info); void get_line_length(struct ascii_art_t *ascii, const char **line); void print_system_info(struct system_info_t *info); +void parse_args(int argc, char** argv); +void print_help(); /* Util functions */ 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; +} |
