From 729746e50ebc8e4eb4c98f7556a241aec111e07d Mon Sep 17 00:00:00 2001 From: Anser Date: Tue, 11 Aug 2026 13:11:39 +0200 Subject: some more rework in readme.md and src file structure --- src/ascii.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cfetch.c | 2 +- src/cpu.c | 44 ----------------------- src/logo.c | 110 ---------------------------------------------------------- src/memory.c | 26 -------------- src/os.c | 18 ---------- src/sysinfo.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+), 199 deletions(-) create mode 100644 src/ascii.c delete mode 100644 src/cpu.c delete mode 100644 src/logo.c delete mode 100644 src/memory.c delete mode 100644 src/os.c create mode 100644 src/sysinfo.c (limited to 'src') diff --git a/src/ascii.c b/src/ascii.c new file mode 100644 index 0000000..b4df816 --- /dev/null +++ b/src/ascii.c @@ -0,0 +1,110 @@ +#include "../include/cfetch.h" +#include + +#define MAX_LOGOS 2 + +/* +typedef struct distro_mapping_t +{ + const char *distro_pattern; + const char *ascii_file; +} distro_mapping_t; +*/ + + +static const distro_mapping_t distro_mappings[] = +{ + {"cccp", "cccp.ascii"}, + {"cccp_shield", "cccp_shield.ascii"}, + {NULL, NULL} +}; + +const char* get_ascii_name(const distro_mapping_t *d_map, int art) +{ + if (art > MAX_LOGOS) { + fprintf(stdout, "get_ascii_path: index out of bounce:\n"); + fprintf(stdout, "get default ascii art.\n"); + art = 0; + } + + const char* ascii_name = d_map[art].ascii_file; + return ascii_name; +} + +void print_ascii_art(system_info_t *info, int art) +{ + // get ascii name + const char* name = get_ascii_name(distro_mappings, art); + char* path = concat("../ascii/", name); + + if (path == NULL) + { + perror("print_ascii_art: could not concat"); + return; + } + FILE* file = fopen(path, "r"); + + if (file == NULL) { + perror("print_ascii_art: could not open file"); + return; + } + // get mapping + + // return path ? + free(path); +} + +/* +int get_line_length(const char **line) +{ + size_t length = 0; + size_t max_length = 0; + + if (line == NULL) return -1; + for (int i = 0; line[i] != NULL; i++) { + length = strlen(line[i]); + + if (length > max_length) { + max_length = length; + } + } + return max_length; +} + + +const char **logo(int index) { + + static const char *logo0[] = { +" _____________", +" / \\", +" | ☆ ☆ ☆ |", +" | ☆ ☭ ☆ |", +" | ☆ ☆ ☆ |", +" \\_____________/", +" Рабочие мира, объединяйтесь!", + NULL + }; + + + static const char *logo1[] = { +" ╔═══════════════════╗", +" ║ ║", +" ║ ☭ C C C P ☭ ║", +" ║ ║", +" ╚═══════════════════╝", +" Власть — Советам!", + NULL + }; + + if (index < 0 || index >= MAX_LOGOS) return NULL; + + switch (index) { + case 0: + return logo0; + case 1: + return logo1; + default: + return NULL; + } +} +*/ diff --git a/src/cfetch.c b/src/cfetch.c index 3977bb1..92157d4 100644 --- a/src/cfetch.c +++ b/src/cfetch.c @@ -24,7 +24,7 @@ int main(void) { get_os(&info); get_memory(&info); get_cpu(&info); - print_ascii_art(&info, art_logo); + //print_ascii_art(&info, art_logo); return 0; } diff --git a/src/cpu.c b/src/cpu.c deleted file mode 100644 index c45cd9e..0000000 --- a/src/cpu.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "../include/cfetch.h" - -void get_cpu(system_info_t *info) { - FILE* file = fopen("/proc/cpuinfo", "r"); - - //check file existence - if (!file) { - perror("/proc/cpuinfo not found"); - return; - } - - char line[256]; - char *model_name = NULL; - - //read per line - while (fgets(line, sizeof(line), file)) { - char* p = strstr(line, "model name"); - - if (p != NULL) { - model_name = strchr(p, ':') + 1; //skip : - break; - } - } - - if (model_name != NULL) { - char *string = string_format(model_name); - - if (string == NULL) { - printf("CPU: Error string not found.\n"); - free(string); - model_name = NULL; - } else { - printf("CPU: %s\n", string); - free(string); - model_name = NULL; - } - - } else { - printf("CPU: Error model_name not found.\n"); - } - - fclose(file); - return; -} diff --git a/src/logo.c b/src/logo.c deleted file mode 100644 index 641de75..0000000 --- a/src/logo.c +++ /dev/null @@ -1,110 +0,0 @@ -#include "../include/cfetch.h" -#include - -#define MAX_LOGOS 2 - -/* -typedef struct distro_mapping_t -{ - const char *distro_pattern; - const char *ascii_file; -} distro_mapping_t; -*/ - - -static const distro_mapping_t distro_mappings[] = -{ - {"cccp", "cccp.ascii"}, - {"cccp_shield", "cccp_shield.ascii"}, - {NULL, NULL} -}; - -const char* get_ascii_name(const distro_mapping_t *d_map, int art) -{ - if (art > MAX_LOGOS) { - fprintf(stdout, "get_ascii_path: index out of bounce:\n"); - fprintf(stdout, "get default ascii art.\n"); - art = 0; - } - - const char* ascii_name = d_map[art].ascii_file; - return ascii_name; -} - -void print_ascii_art(system_info_t *info, int art) -{ - // get ascii name - const char* name = get_ascii_name(distro_mappings[], art); - char* path = concat("../ascii/", name); - - if (path == NULL) - { - perror("print_ascii_art: could not concat"); - return; - } - FILE* file = fopen(path, "r"); - - if (file == NULL) { - perror("print_ascii_art: could not open file"); - return; - } - // get mapping - - // return path ? - free(path); -} - -/* -int get_line_length(const char **line) -{ - size_t length = 0; - size_t max_length = 0; - - if (line == NULL) return -1; - for (int i = 0; line[i] != NULL; i++) { - length = strlen(line[i]); - - if (length > max_length) { - max_length = length; - } - } - return max_length; -} - - -const char **logo(int index) { - - static const char *logo0[] = { -" _____________", -" / \\", -" | ☆ ☆ ☆ |", -" | ☆ ☭ ☆ |", -" | ☆ ☆ ☆ |", -" \\_____________/", -" Рабочие мира, объединяйтесь!", - NULL - }; - - - static const char *logo1[] = { -" ╔═══════════════════╗", -" ║ ║", -" ║ ☭ C C C P ☭ ║", -" ║ ║", -" ╚═══════════════════╝", -" Власть — Советам!", - NULL - }; - - if (index < 0 || index >= MAX_LOGOS) return NULL; - - switch (index) { - case 0: - return logo0; - case 1: - return logo1; - default: - return NULL; - } -} -*/ diff --git a/src/memory.c b/src/memory.c deleted file mode 100644 index 9e880d2..0000000 --- a/src/memory.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "../include/cfetch.h" - -void get_memory(system_info_t *info) { - FILE *file = fopen("/proc/meminfo", "r"); - - if (!file) { - perror("Error fopen meminfo"); - return; - } - - char line[256]; - long total = 0; - long available = 0; - - // read per line - while (fgets(line, sizeof(line), file)) { - if (sscanf(line, "MemTotal: %ld kB", &total) == 1) continue; - if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) continue; - } - - fclose(file); - - printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); - - return; -} diff --git a/src/os.c b/src/os.c deleted file mode 100644 index fd5b7ae..0000000 --- a/src/os.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "../include/cfetch.h" - -#include - -void get_os(system_info_t *info) { -#ifdef __linux__ - struct utsname u; - - if (uname(&u) == 0) { - size_t length = printf("OS: %s %s %s %s\n", u.sysname, u.release, u.version, u.machine); - printf("line length: %ld\n", length); - } else { - perror("Error while accessing uname"); - return; - } -#endif - return; -} diff --git a/src/sysinfo.c b/src/sysinfo.c new file mode 100644 index 0000000..0a91eca --- /dev/null +++ b/src/sysinfo.c @@ -0,0 +1,85 @@ +#include "../include/cfetch.h" +#include + +void get_os(system_info_t *info) { +#ifdef __linux__ + struct utsname u; + + if (uname(&u) == 0) { + size_t length = printf("OS: %s %s %s %s\n", u.sysname, u.release, u.version, u.machine); + printf("line length: %ld\n", length); + } else { + perror("Error while accessing uname"); + return; + } +#endif + return; +} + +void get_cpu(system_info_t *info) { + FILE* file = fopen("/proc/cpuinfo", "r"); + + //check file existence + if (!file) { + perror("/proc/cpuinfo not found"); + return; + } + + char line[256]; + char *model_name = NULL; + + //read per line + while (fgets(line, sizeof(line), file)) { + char* p = strstr(line, "model name"); + + if (p != NULL) { + model_name = strchr(p, ':') + 1; //skip : + break; + } + } + + if (model_name != NULL) { + char *string = string_format(model_name); + + if (string == NULL) { + printf("CPU: Error string not found.\n"); + free(string); + model_name = NULL; + } else { + printf("CPU: %s\n", string); + free(string); + model_name = NULL; + } + + } else { + printf("CPU: Error model_name not found.\n"); + } + + fclose(file); + return; +} + +void get_memory(system_info_t *info) { + FILE *file = fopen("/proc/meminfo", "r"); + + if (!file) { + perror("Error fopen meminfo"); + return; + } + + char line[256]; + long total = 0; + long available = 0; + + // read per line + while (fgets(line, sizeof(line), file)) { + if (sscanf(line, "MemTotal: %ld kB", &total) == 1) continue; + if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) continue; + } + + fclose(file); + + printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); + + return; +} -- cgit v1.2.3