diff options
| author | Anser <rostgans@tutamail.com> | 2026-08-14 10:43:03 +0200 |
|---|---|---|
| committer | Anser <rostgans@tutamail.com> | 2026-08-14 10:52:39 +0200 |
| commit | 537230def11fddb3a63f8b4d0bf9b5d64b96e6d0 (patch) | |
| tree | ebb6df16806d45dce9d050b2e7917c5def09c994 | |
| parent | 2e72081d3803d5627c10924b3c20099a964d4bef (diff) | |
Changed codingstyle to C kernel style. Print of Systeminformation is now its own function
all systeminformation is now stored inside the struct
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | include/cfetch.h | 44 | ||||
| -rw-r--r-- | src/ascii.c | 133 | ||||
| -rw-r--r-- | src/cfetch.c | 52 | ||||
| -rw-r--r-- | src/sysinfo.c | 118 | ||||
| -rw-r--r-- | src/utils.c | 222 |
6 files changed, 291 insertions, 280 deletions
@@ -8,7 +8,7 @@ ## roadmap - [x] 0.1.0: get some basic system information on the screen -- [ ] 0.1.1: print out the ascii art with systeminformation +- [x] 0.1.1: print out the ascii art with systeminformation - [ ] 0.1.2+: some more systeminformation plus print ascii art left of sys information ### future things... maybe diff --git a/include/cfetch.h b/include/cfetch.h index 104ced4..b08c471 100644 --- a/include/cfetch.h +++ b/include/cfetch.h @@ -11,30 +11,32 @@ /* System information structure */ -typedef struct system_info_t{ - char user[64]; - char kernel[128]; - char memory[64]; - char cpu[64]; -} system_info_t; - -typedef struct { - char lines[MAX_ASCII_LINES][MAX_ASCII_WIDTH]; -} ascii_art_t; - -typedef struct distro_mapping_t -{ - const char *distro_pattern; - const char *ascii_file; -} distro_mapping_t; +struct system_info_t { + char user[64]; + char kernel[128]; + char memory[64]; + char cpu[64]; + char debug; + short ascii_art; +}; + +struct ascii_art_t { + char lines[MAX_ASCII_LINES][MAX_ASCII_WIDTH]; +}; + +struct ascii_mapping_t { + const char *ascii_pattern; + const char *ascii_file; +}; /* System info functions */ -void print_ascii_art(system_info_t *info, int art); -void get_cpu(system_info_t *info); -void get_memory(system_info_t *info); -void get_kernel(system_info_t *info); -void get_line_length(ascii_art_t *ascii, const char **line); +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_line_length(struct ascii_art_t *ascii, const char **line); +void print_system_info(struct system_info_t *info); /* Util functions */ diff --git a/src/ascii.c b/src/ascii.c index f1acce7..f2e466f 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -4,62 +4,67 @@ #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[] = +static const struct ascii_mapping_t ascii_mappings[] = { - {"cccp", "cccp.ascii"}, - {"cccp_shield", "cccp_shield.ascii"}, - {NULL, NULL} + {"cccp", "cccp.ascii"}, + {"cccp_shield", "cccp_shield.ascii"}, + {NULL, NULL} }; -const char* get_ascii_name(const distro_mapping_t *d_map, int art) +const char* get_ascii_name(const struct ascii_mapping_t *d_map, int art) +{ + if (art > MAX_LOGOS) { + fprintf(stderr, "get_ascii_path: index out of bounce\n"); + fprintf(stderr, "Get default ascii art.\n"); + art = 0; + } + + const char* ascii_name = d_map[art].ascii_file; + return ascii_name; +} + +void print_ascii_art(struct system_info_t *info) { - if (art > MAX_LOGOS) { - fprintf(stderr, "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; + // get ascii name + const char* name = get_ascii_name(ascii_mappings, info->ascii_art); + char* path = concat("ascii/", name); + + if (info->debug == 1) { + printf("path: %s\n", path); + } + + 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 + char line[64]; + while (fgets(line, sizeof(line), file)) { + printf("%s", line); + } + + // free pointers + free(path); } -void print_ascii_art(system_info_t *info, int art) +void print_system_info(struct system_info_t *info) { - // get ascii name - const char* name = get_ascii_name(distro_mappings, art); - char* path = concat("ascii/", name); - printf("path: %s\n", path); - - 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 - char line[64]; - while (fgets(line, sizeof(line), file)) { - printf("%s", line); - } - - // free pointers - free(path); + printf("Kernel: %s\n", info->kernel); + printf("CPU: %s\n", info->cpu); + printf("%s\n", info->memory); + //printf(logo) + + print_ascii_art(info); } // brauche ich diese function überhaupt, mit dieser signatur ? @@ -67,22 +72,22 @@ void print_ascii_art(system_info_t *info, int art) // und dann get_line_length? // alles im struct speicher? /* -void get_line_length(ascii_art_t *ascii, const char **line) -{ - size_t length = 0; + void get_line_length(ascii_art_t *ascii, const char **line) + { + size_t length = 0; - if (line == NULL) - { - perror("get_line_length: pointer NULL"); - return; - } + if (line == NULL) + { + perror("get_line_length: pointer NULL"); + return; + } - for (int i = 0; line[i] != NULL; i++) { - length = strlen(line[i]); + for (int i = 0; line[i] != NULL; i++) { + length = strlen(line[i]); - if (length > ascii->lines) { - max_length = length; - } - } -} -*/ + if (length > ascii->lines) { + max_length = length; + } + } + } + */ diff --git a/src/cfetch.c b/src/cfetch.c index e9f473d..6edcd87 100644 --- a/src/cfetch.c +++ b/src/cfetch.c @@ -8,7 +8,7 @@ */ #ifndef CFETCH_VERSION -#define CFETCH_VERSION 0.1.0 +#define CFETCH_VERSION 0.1.1 #endif #include "../include/cfetch.h" @@ -18,29 +18,29 @@ int main(void) { - - system_info_t info = { 0 }; - // set default artwork - int art_logo = 1; - - // set systeminformation - // use // to uncomment or comment - // get_os: Ubuntu <version> - // get_host: Laptop name? Precision 7680 - get_kernel(&info); - // get_uptime - // get_packages - // get_shell - // get_resolution - // get_de - // get_wm - // get_terminal - get_cpu(&info); - //get_gpu - get_memory(&info); - - print_ascii_art(&info, art_logo); - - // get_colours ? - return 0; + struct system_info_t info = { 0 }; + //info.debug = 1; + // set default artwork + info.ascii_art = 0; + + // set systeminformation + // use // to uncomment or comment + // get_os: Ubuntu <version> + // get_host: Laptop name? Precision 7680 + get_kernel(&info); + // get_uptime + // get_packages + // get_shell + // get_resolution + // get_de + // get_wm + // get_terminal + get_cpu(&info); + //get_gpu + get_memory(&info); + + print_system_info(&info); + + // get_colours ? + return 0; } diff --git a/src/sysinfo.c b/src/sysinfo.c index f402741..eddaa02 100644 --- a/src/sysinfo.c +++ b/src/sysinfo.c @@ -2,75 +2,75 @@ #include <stdio.h> #include <sys/utsname.h> -void get_kernel(system_info_t *info) { +void get_kernel(struct system_info_t *info) +{ #ifdef __linux__ - struct utsname u; - - if (uname(&u) == 0) { - snprintf(info->kernel, sizeof(info->kernel), "%.30s %.30s", u.sysname, u.release); - printf("Kernel: %s\n", info->kernel); - } else { - perror("Error while accessing uname"); - return; - } + struct utsname u; + + if (uname(&u) == 0) { + snprintf(info->kernel, sizeof(info->kernel), "%.30s %.30s", u.sysname, u.release); + } else { + perror("Error while accessing uname"); + return; + } #endif - return; + return; } -void get_cpu(system_info_t *info) { - FILE* file = fopen("/proc/cpuinfo", "r"); - - //check file existence - if (!file) { - snprintf(info->cpu, sizeof(info->cpu), "Unknown"); - return; - } - - char line[256]; - char cpu_name[256] = "Unknown"; - char *colon = NULL; - - //read per line - while (fgets(line, sizeof(line), file)) { - char* p = strstr(line, "model name"); - - if (p != NULL) { - colon = strchr(p, ':') + 1; //skip : - break; - } - } - - if (colon != NULL) { - snprintf(info->cpu, sizeof(info->cpu), "%s", trim_whitespace(colon)); - - printf("CPU: %s\n", info->cpu); - } - - fclose(file); - return; +void get_cpu(struct system_info_t *info) +{ + FILE* file = fopen("/proc/cpuinfo", "r"); + + //check file existence + if (!file) { + snprintf(info->cpu, sizeof(info->cpu), "Unknown"); + return; + } + + char line[256]; + char *cpu_name = NULL; + + //read per line + while (fgets(line, sizeof(line), file)) { + char* p = strstr(line, "model name"); + + if (p != NULL) { + cpu_name = strchr(p, ':') + 1; //skip : + break; + } + } + + if (cpu_name != NULL) { + snprintf(info->cpu, sizeof(info->cpu), "%s", trim_whitespace(cpu_name)); + } + + fclose(file); + return; } -void get_memory(system_info_t *info) { - FILE *file = fopen("/proc/meminfo", "r"); +void get_memory(struct system_info_t *info) +{ + FILE *file = fopen("/proc/meminfo", "r"); - if (!file) { - perror("Error fopen meminfo"); - return; - } + if (!file) { + snprintf(info->memory, sizeof(info->memory), "Unknown"); + return; + } - char line[256]; - long total = 0; - long available = 0; + 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; - } + // 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); + fclose(file); - printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); + snprintf(info->memory, sizeof(info->memory), "Memory %.2f GB / %.2f GB", + (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); - return; + return; } diff --git a/src/utils.c b/src/utils.c index 56cca93..16f7589 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,127 +9,131 @@ * 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; +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 +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 } +// input: pointer to string +// output: pointer to string with leading and tailing whitespaces removed char *trim_whitespace(char* str) { - char *end; + char *end; + + // Trim leading spaces + while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') { + str++; + } + + if (*str == '\0') return str; // all spaces - // Trim leading spaces - while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') { - str++; - } + // Trim trailing space + end = str + strlen(str) - 1; - if (*str == '\0') return str; // all spaces - - // Trim trailing space - end = str + strlen(str) - 1; + while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) end--; - while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) end--; - - // Write new null terminator character - end[1] = '\0'; + // Write new null terminator character + end[1] = '\0'; - return str; + return str; } |
