diff options
| author | Anser <rostgans@tutamail.com> | 2026-08-13 15:28:47 +0200 |
|---|---|---|
| committer | Anser <rostgans@tutamail.com> | 2026-08-14 10:44:28 +0200 |
| commit | 9bfaf4dd363429757754d9934ca38fad4ef7dc0a (patch) | |
| tree | c13a7148dfd96178481744368dd0d29a6bdc033d | |
| parent | d195f0a76a3ce64761958f7d33ccbdbac3844fec (diff) | |
changed get_os to get_kernel, it now uses the system_info_t struct to store and display the kernel information. Same with get_cpu.
| -rw-r--r-- | include/cfetch.h | 5 | ||||
| -rw-r--r-- | src/ascii.c | 3 | ||||
| -rw-r--r-- | src/cfetch.c | 16 | ||||
| -rw-r--r-- | src/sysinfo.c | 32 | ||||
| -rw-r--r-- | src/utils.c | 24 |
5 files changed, 55 insertions, 25 deletions
diff --git a/include/cfetch.h b/include/cfetch.h index 268fa22..104ced4 100644 --- a/include/cfetch.h +++ b/include/cfetch.h @@ -13,7 +13,7 @@ typedef struct system_info_t{ char user[64]; - char kernel[64]; + char kernel[128]; char memory[64]; char cpu[64]; } system_info_t; @@ -33,7 +33,7 @@ typedef struct distro_mapping_t 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_os(system_info_t *info); +void get_kernel(system_info_t *info); void get_line_length(ascii_art_t *ascii, const char **line); @@ -41,5 +41,6 @@ void get_line_length(ascii_art_t *ascii, const char **line); char *string_format(char *string); char *concat(const char *str1, const char *str2); +char *trim_whitespace(char *str); #endif /* CFETCH_H */ diff --git a/src/ascii.c b/src/ascii.c index 2fee648..f1acce7 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -66,6 +66,7 @@ void print_ascii_art(system_info_t *info, int art) // get_max_ascii_length // und dann get_line_length? // alles im struct speicher? +/* void get_line_length(ascii_art_t *ascii, const char **line) { size_t length = 0; @@ -84,4 +85,4 @@ void get_line_length(ascii_art_t *ascii, const char **line) } } } - +*/ diff --git a/src/cfetch.c b/src/cfetch.c index caabbc9..66d115d 100644 --- a/src/cfetch.c +++ b/src/cfetch.c @@ -25,10 +25,22 @@ int main(void) { // set systeminformation // use // to uncomment or comment - get_os(&info); - get_memory(&info); + // 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; } diff --git a/src/sysinfo.c b/src/sysinfo.c index bb7f20d..f402741 100644 --- a/src/sysinfo.c +++ b/src/sysinfo.c @@ -1,12 +1,14 @@ #include "../include/cfetch.h" +#include <stdio.h> #include <sys/utsname.h> -void get_os(system_info_t *info) { +void get_kernel(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); + 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; @@ -20,39 +22,29 @@ void get_cpu(system_info_t *info) { //check file existence if (!file) { - perror("/proc/cpuinfo not found"); + snprintf(info->cpu, sizeof(info->cpu), "Unknown"); return; } char line[256]; - char *model_name = NULL; + 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) { - model_name = strchr(p, ':') + 1; //skip : + colon = 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; - } + if (colon != NULL) { + snprintf(info->cpu, sizeof(info->cpu), "%s", trim_whitespace(colon)); - } else { - printf("CPU: Error model_name not found.\n"); - } + printf("CPU: %s\n", info->cpu); + } fclose(file); return; diff --git a/src/utils.c b/src/utils.c index 5fba4dc..56cca93 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,5 @@ #include "../include/cfetch.h" +#include <string.h> /* function: char *string_format(char* string) * input: pointer to a null-terminated string (must not be NULL) @@ -109,3 +110,26 @@ char *concat(const char *s1, const char *s2) { memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator return result; //caller needs to free memory } + + +char *trim_whitespace(char* str) +{ + char *end; + + // Trim leading spaces + while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') { + str++; + } + + 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--; + + // Write new null terminator character + end[1] = '\0'; + + return str; +} |
