diff options
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | include/cfetch.h | 44 | ||||
| -rw-r--r-- | src/cfetch.c | 32 | ||||
| -rw-r--r-- | src/cpu.c (renamed from src/modules/cpu/cpu.c) | 12 | ||||
| -rw-r--r-- | src/logo.c (renamed from src/logo/logo.c) | 100 | ||||
| -rw-r--r-- | src/logo/logo.h | 6 | ||||
| -rw-r--r-- | src/memory.c (renamed from src/modules/memory/memory.c) | 9 | ||||
| -rw-r--r-- | src/modules/cpu/cpu.h | 6 | ||||
| -rw-r--r-- | src/modules/memory/memory.h | 6 | ||||
| -rw-r--r-- | src/modules/os/os.h | 6 | ||||
| -rw-r--r-- | src/modules/utils/utils.h | 21 | ||||
| -rw-r--r-- | src/os.c (renamed from src/modules/os/os.c) | 11 | ||||
| -rw-r--r-- | src/utils.c (renamed from src/modules/utils/utils.c) | 5 |
13 files changed, 139 insertions, 131 deletions
@@ -8,16 +8,16 @@ # CC = gcc -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra PREFIX ?= /usr/local TARGET = cfetch SRC = src/cfetch.c \ - src/modules/os/os.c \ - src/modules/memory/memory.c \ - src/modules/cpu/cpu.c \ - src/logo/logo.c \ - src/modules/utils/utils.c + src/os.c \ + src/memory.c \ + src/cpu.c \ + src/logo.c \ + src/utils.c .PHONY: all clean install uninstall debug diff --git a/include/cfetch.h b/include/cfetch.h new file mode 100644 index 0000000..57c8781 --- /dev/null +++ b/include/cfetch.h @@ -0,0 +1,44 @@ +#ifndef CFETCH_H +#define CFETCH_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MAX_ASCII_LINES 50 +#define MAX_ASCII_WIDTH 256 + + +/* System information structure */ + +typedef struct system_info_t{ + char user[64]; + char kernel[64]; + 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; + + +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); + + +/* Util functions */ +char *string_format(char *string); +char *concat(const char *str1, const char *str2); + + + +#endif /* CFETCH_H */ 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 <stdio.h> -#include <sys/ioctl.h> -#include <stdlib.h> #include <unistd.h> +#include <stddef.h> -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/modules/cpu/cpu.c b/src/cpu.c index a6dac93..c45cd9e 100644 --- a/src/modules/cpu/cpu.c +++ b/src/cpu.c @@ -1,16 +1,12 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include "cpu.h" -#include "../utils/utils.h" +#include "../include/cfetch.h" -int cpu(void) { +void get_cpu(system_info_t *info) { FILE* file = fopen("/proc/cpuinfo", "r"); //check file existence if (!file) { perror("/proc/cpuinfo not found"); - return 1; + return; } char line[256]; @@ -44,5 +40,5 @@ int cpu(void) { } fclose(file); - return 0; + return; } diff --git a/src/logo/logo.c b/src/logo.c index 98cda8d..641de75 100644 --- a/src/logo/logo.c +++ b/src/logo.c @@ -1,21 +1,77 @@ -#include "logo.h" +#include "../include/cfetch.h" #include <stddef.h> -#include <stdio.h> -#include <string.h> #define MAX_LOGOS 2 -typedef struct distro_mapping_t { +/* +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 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[] = { @@ -26,7 +82,7 @@ const char **logo(int index) { " | ☆ ☆ ☆ |", " \\_____________/", " Рабочие мира, объединяйтесь!", - NULL + NULL }; @@ -51,34 +107,4 @@ const char **logo(int index) { 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/modules/memory/memory.c b/src/memory.c index 62eeafe..9e880d2 100644 --- a/src/modules/memory/memory.c +++ b/src/memory.c @@ -1,12 +1,11 @@ -#include <stdio.h> -#include "memory.h" +#include "../include/cfetch.h" -char *memory(void) { +void get_memory(system_info_t *info) { FILE *file = fopen("/proc/meminfo", "r"); if (!file) { perror("Error fopen meminfo"); - return NULL; + return; } char line[256]; @@ -23,5 +22,5 @@ char *memory(void) { printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0)); - return 0; + return; } 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.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.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.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/modules/os/os.c b/src/os.c index 6059c03..fd5b7ae 100644 --- a/src/modules/os/os.c +++ b/src/os.c @@ -1,9 +1,8 @@ -#include <stdio.h> -#include <string.h> +#include "../include/cfetch.h" + #include <sys/utsname.h> -#include "os.h" -int os(void) { +void get_os(system_info_t *info) { #ifdef __linux__ struct utsname u; @@ -12,8 +11,8 @@ int os(void) { printf("line length: %ld\n", length); } else { perror("Error while accessing uname"); - return 1; + return; } #endif - return 0; + return; } diff --git a/src/modules/utils/utils.c b/src/utils.c index 2e8aa5c..5fba4dc 100644 --- a/src/modules/utils/utils.c +++ b/src/utils.c @@ -1,7 +1,4 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> - +#include "../include/cfetch.h" /* function: char *string_format(char* string) * input: pointer to a null-terminated string (must not be NULL) |
