From eb6f986361713d6ce47c6d4572bb24d9f6207ddf Mon Sep 17 00:00:00 2001 From: Anser Date: Tue, 11 Aug 2026 00:18:57 +0200 Subject: TOBESQUISHED: safe in reconstruction of the codes layout and rework of information handeling also ascii art printing --- src/cfetch.c | 32 +++++-------- src/cpu.c | 44 +++++++++++++++++ src/logo.c | 110 ++++++++++++++++++++++++++++++++++++++++++ src/logo/logo.c | 84 -------------------------------- src/logo/logo.h | 6 --- src/memory.c | 26 ++++++++++ src/modules/cpu/cpu.c | 48 ------------------- src/modules/cpu/cpu.h | 6 --- src/modules/memory/memory.c | 27 ----------- src/modules/memory/memory.h | 6 --- src/modules/os/os.c | 19 -------- src/modules/os/os.h | 6 --- src/modules/utils/utils.c | 114 -------------------------------------------- src/modules/utils/utils.h | 21 -------- src/os.c | 18 +++++++ src/utils.c | 111 ++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 321 insertions(+), 357 deletions(-) create mode 100644 src/cpu.c create mode 100644 src/logo.c delete mode 100644 src/logo/logo.c delete mode 100644 src/logo/logo.h create mode 100644 src/memory.c delete mode 100644 src/modules/cpu/cpu.c delete mode 100644 src/modules/cpu/cpu.h delete mode 100644 src/modules/memory/memory.c delete mode 100644 src/modules/memory/memory.h delete mode 100644 src/modules/os/os.c delete mode 100644 src/modules/os/os.h delete mode 100644 src/modules/utils/utils.c delete mode 100644 src/modules/utils/utils.h create mode 100644 src/os.c create mode 100644 src/utils.c (limited to 'src') diff --git a/src/cfetch.c b/src/cfetch.c index ac5c6a6..3977bb1 100644 --- a/src/cfetch.c +++ b/src/cfetch.c @@ -7,32 +7,24 @@ * (at your option) any later version. */ -#include "modules/os/os.h" -#include "modules/memory/memory.h" -#include "modules/cpu/cpu.h" -#include "logo/logo.h" -#include "modules/utils/utils.h" +#include "../include/cfetch.h" -#include -#include -#include #include +#include -int main(void) { - os(); - memory(); - cpu(); - print_logo(); - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); +int main(void) { - printf("lines %d\n", w.ws_row); - printf("columns %d\n", w.ws_col); + system_info_t info = { 0 }; + // set default artwork + int art_logo = 0; - /* char *s = concat("derp", "herp"); - printf("%s\n", s); - free(s); */ + // set systeminformation + // use // to uncomment or comment + get_os(&info); + get_memory(&info); + get_cpu(&info); + print_ascii_art(&info, art_logo); return 0; } diff --git a/src/cpu.c b/src/cpu.c new file mode 100644 index 0000000..c45cd9e --- /dev/null +++ b/src/cpu.c @@ -0,0 +1,44 @@ +#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 new file mode 100644 index 0000000..641de75 --- /dev/null +++ b/src/logo.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/logo/logo.c b/src/logo/logo.c deleted file mode 100644 index 98cda8d..0000000 --- a/src/logo/logo.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "logo.h" -#include -#include -#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 **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; - } -} - -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; -} - -void print_logo(void) { - int length = 0; - - for (int i = 0; i < MAX_LOGOS; i++) { - const char **lines = logo(i); - if (lines == NULL) continue; - - length = get_line_length(lines); - - for (int j = 0; lines[j] != NULL ; j++) { - printf("%s\n", lines[j]); - } - printf("%d\n", length); - } -} diff --git a/src/logo/logo.h b/src/logo/logo.h deleted file mode 100644 index fc55db6..0000000 --- a/src/logo/logo.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef LOGO_H -#define LOGO_H - -void print_logo(void); - -#endif //LOGO_H diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..9e880d2 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,26 @@ +#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/modules/cpu/cpu.c b/src/modules/cpu/cpu.c deleted file mode 100644 index a6dac93..0000000 --- a/src/modules/cpu/cpu.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include "cpu.h" -#include "../utils/utils.h" - -int cpu(void) { - FILE* file = fopen("/proc/cpuinfo", "r"); - - //check file existence - if (!file) { - perror("/proc/cpuinfo not found"); - return 1; - } - - 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 0; -} diff --git a/src/modules/cpu/cpu.h b/src/modules/cpu/cpu.h deleted file mode 100644 index 63aa12f..0000000 --- a/src/modules/cpu/cpu.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef CPU_H -#define CPU_H - -int cpu(void); - -#endif // CPU_H diff --git a/src/modules/memory/memory.c b/src/modules/memory/memory.c deleted file mode 100644 index 62eeafe..0000000 --- a/src/modules/memory/memory.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include "memory.h" - -char *memory(void) { - FILE *file = fopen("/proc/meminfo", "r"); - - if (!file) { - perror("Error fopen meminfo"); - return NULL; - } - - 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 0; -} diff --git a/src/modules/memory/memory.h b/src/modules/memory/memory.h deleted file mode 100644 index 5c35178..0000000 --- a/src/modules/memory/memory.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef MEMORY_H -#define MEMORY_H - -char *memory(void); - -#endif // MEMORY_H diff --git a/src/modules/os/os.c b/src/modules/os/os.c deleted file mode 100644 index 6059c03..0000000 --- a/src/modules/os/os.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include "os.h" - -int os(void) { -#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 1; - } -#endif - return 0; -} diff --git a/src/modules/os/os.h b/src/modules/os/os.h deleted file mode 100644 index d90792e..0000000 --- a/src/modules/os/os.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef OS_H -#define OS_H - -int os(void); - -#endif diff --git a/src/modules/utils/utils.c b/src/modules/utils/utils.c deleted file mode 100644 index 2e8aa5c..0000000 --- a/src/modules/utils/utils.c +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include -#include - - -/* function: char *string_format(char* string) - * input: pointer to a null-terminated string (must not be NULL) - * return: pointer to a newly allocated string containing all space-separated words, - * joined by single spaces; leading/trailing/multiple spaces are ignored. - * on-error: returns NULL if memory allocation fails - * misc: the returned pointer must be freed by the caller using free() - * limits: processes at most 10 words, each up to 49 characters (last '\0') - */ -char *string_format(char* string) { - // Wörterbuch array [Wort][Zeichenkette] - char woerter[10][50] = { 0 }; - int wort_index = 0; - int char_index = 0; - - while (*string != '\0') { - // add '\0' stringterminator to end of word - if (*string == ' ' && char_index > 0) { - // safty check for array length - if (wort_index > 9 || char_index > 49) { - break; - } - - // next slot is char - // add '\0' to end string - woerter[wort_index][char_index] = '\0'; - wort_index++; - char_index = 0; - string++; - continue; - } - // skip extra spaces - else if (*string == ' ' && char_index == 0) { - string++; - continue; - } - - // safty check for array length - if (char_index >= 49) { - break; - } - - woerter[wort_index][char_index] = *string; - char_index++; - string++; - } - - // terminate last word with '\0' - if (char_index > 0 && wort_index < 10 && char_index < 49) { - woerter[wort_index][char_index] = '\0'; - wort_index++; - } - - /* - * return - */ - char *ptr = NULL; - - // get length of woerter array - if (wort_index == 0) { - ptr = NULL; - } else { - int sum = 0; - - for (int i = 0; i < wort_index; i++) { - sum += strlen(woerter[i]); - } - sum += wort_index - 1; // for spaces - sum += 1; // for Nulltermination - - - ptr = (char *)malloc(sum); - - // check for NULL pointer - if (ptr == NULL) { - return NULL; - } - - //strcpy - char *ende = ptr; - - for (int i = 0; i < wort_index; i++) { - ende = stpcpy(ende, woerter[i]); - - if (i != wort_index - 1) { - *(ende++) = ' '; - } - } - } - - return ptr; -} - -// input two char pointer -// output: one char pointer -// misc: caller needs to free the memory -char *concat(const char *s1, const char *s2) { - const size_t len1 = strlen(s1); - const size_t len2 = strlen(s2); - char *result = malloc(len1 + len2 + 1); // +1 for '\0' - - if (result == NULL) { - perror("allocating memory for strings failed"); - return NULL; - } - - memcpy(result, s1, len1); - memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator - return result; //caller needs to free memory -} diff --git a/src/modules/utils/utils.h b/src/modules/utils/utils.h deleted file mode 100644 index 7a0ab02..0000000 --- a/src/modules/utils/utils.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -/* function: char *string_format(char* string) - * input: pointer to a null-terminated string (must not be NULL) - * return: pointer to a newly allocated string containing all space-separated words, - * joined by single spaces; leading/trailing/multiple spaces are ignored. - * on-error: returns NULL if memory allocation fails - * misc: the returned pointer must be freed by the caller using free() - * limits: processes at most 10 words, each up to 49 characters (last '\0') - */ -char *string_format(char* string); - -// function: char *concat(const char *str1, const char *str2) -// input two char pointer -// return: pointer to string -// on-error: returns NULL -// misc: caller needs to free the memory -char *concat(const char *str1, const char *str2); - -#endif diff --git a/src/os.c b/src/os.c new file mode 100644 index 0000000..fd5b7ae --- /dev/null +++ b/src/os.c @@ -0,0 +1,18 @@ +#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/utils.c b/src/utils.c new file mode 100644 index 0000000..5fba4dc --- /dev/null +++ b/src/utils.c @@ -0,0 +1,111 @@ +#include "../include/cfetch.h" + +/* function: char *string_format(char* string) + * input: pointer to a null-terminated string (must not be NULL) + * return: pointer to a newly allocated string containing all space-separated words, + * joined by single spaces; leading/trailing/multiple spaces are ignored. + * on-error: returns NULL if memory allocation fails + * misc: the returned pointer must be freed by the caller using free() + * limits: processes at most 10 words, each up to 49 characters (last '\0') + */ +char *string_format(char* string) { + // Wörterbuch array [Wort][Zeichenkette] + char woerter[10][50] = { 0 }; + int wort_index = 0; + int char_index = 0; + + while (*string != '\0') { + // add '\0' stringterminator to end of word + if (*string == ' ' && char_index > 0) { + // safty check for array length + if (wort_index > 9 || char_index > 49) { + break; + } + + // next slot is char + // add '\0' to end string + woerter[wort_index][char_index] = '\0'; + wort_index++; + char_index = 0; + string++; + continue; + } + // skip extra spaces + else if (*string == ' ' && char_index == 0) { + string++; + continue; + } + + // safty check for array length + if (char_index >= 49) { + break; + } + + woerter[wort_index][char_index] = *string; + char_index++; + string++; + } + + // terminate last word with '\0' + if (char_index > 0 && wort_index < 10 && char_index < 49) { + woerter[wort_index][char_index] = '\0'; + wort_index++; + } + + /* + * return + */ + char *ptr = NULL; + + // get length of woerter array + if (wort_index == 0) { + ptr = NULL; + } else { + int sum = 0; + + for (int i = 0; i < wort_index; i++) { + sum += strlen(woerter[i]); + } + sum += wort_index - 1; // for spaces + sum += 1; // for Nulltermination + + + ptr = (char *)malloc(sum); + + // check for NULL pointer + if (ptr == NULL) { + return NULL; + } + + //strcpy + char *ende = ptr; + + for (int i = 0; i < wort_index; i++) { + ende = stpcpy(ende, woerter[i]); + + if (i != wort_index - 1) { + *(ende++) = ' '; + } + } + } + + return ptr; +} + +// input two char pointer +// output: one char pointer +// misc: caller needs to free the memory +char *concat(const char *s1, const char *s2) { + const size_t len1 = strlen(s1); + const size_t len2 = strlen(s2); + char *result = malloc(len1 + len2 + 1); // +1 for '\0' + + if (result == NULL) { + perror("allocating memory for strings failed"); + return NULL; + } + + memcpy(result, s1, len1); + memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator + return result; //caller needs to free memory +} -- cgit v1.2.3