From e20d4dc7056a686314b4d58441335a8bcb335e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=88=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=82=D0=BE=D0=BA=D0=B8?= =?UTF-8?q?=D1=9B-=D0=A8=D1=83=D0=BC=D0=B0=D1=80=D0=B0=D1=86?= Date: Mon, 10 May 2021 10:24:24 +0000 Subject: [PATCH] Replace fetchy.c --- fetchy.c | 74 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/fetchy.c b/fetchy.c index ecc10dd..d38c16b 100644 --- a/fetchy.c +++ b/fetchy.c @@ -7,26 +7,27 @@ #define BUF_SIZE 20 -void truncate_spaces(char *str); +void truncate_spaces_leading(char *str); +void read_line(char x); +int count_files(char *path); + static char *get_sys(char *sys); -void get_cpu(char cpu[]); -void get_gpu(char gpu[]); static char *get_kernel(void); static char *get_uptime(void); static int get_packages(char sys); -static char *get_RAM(); -void read_line(char x); +static char *get_RAM(void); +static char *get_cpu(void); +void get_gpu(char gpu[]); -char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL; +char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL; int main(int argc, char *argv[]){ - char cpu[70], gpu[70], sys; + char gpu[70], sys; char *os = malloc(BUF_SIZE); get_gpu(gpu); - get_cpu(cpu); os = get_sys(&sys); @@ -38,7 +39,7 @@ int main(int argc, char *argv[]){ printf("\x1b[1m / \\ \x1b[36mUPT\x1b[0m -> %s\n", get_uptime()); printf("\x1b[1m /^. \\ \x1b[36mPKGS\x1b[0m -> %d\n", get_packages(sys)); printf("\x1b[1m / .-. \\ \x1b[36m\x1b[0m \n"); - printf("\x1b[1m / ( ) _\\ \x1b[36mCPU\x1b[0m -> %s\n", cpu); + printf("\x1b[1m / ( ) _\\ \x1b[36mCPU\x1b[0m -> %s\n", get_cpu()); printf("\x1b[1m / _.~ ~._^\\ \x1b[36mGPU\x1b[0m -> %s\n", gpu); printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %s\n", get_RAM()); printf("\n\n"); @@ -53,7 +54,7 @@ int main(int argc, char *argv[]){ printf("\x1b[1m ,`\\ \\ `-`. \x1b[36mUPTIME\x1b[0m -> %s\n", get_uptime()); printf("\x1b[1m / \\ '``-. ` \x1b[36mPACKAGES\x1b[0m -> %d\n", get_packages(sys)); printf("\x1b[1m .-. , `___: \x1b[36m\x1b[0m \n"); - printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", cpu); + printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", get_cpu()); printf("\x1b[1m `-` ` , : \x1b[36mGPU\x1b[0m -> %s\n", gpu); printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %s\n", get_RAM()); printf("\x1b[1m `./ / .-.` \x1b[36m\x1b[0m \n"); @@ -68,20 +69,21 @@ int main(int argc, char *argv[]){ free(uptime); free(ram_info); free(kern_name); + free(cpu_name); return 0; } -void read_line(char x) { +void read_line(char x){ int c; while (( c = getchar()) != x && c != EOF) { } } -void truncate_spaces(char *str) { +void truncate_spaces_leading(char *str){ int src = 0, dst = 0; while(*(str + dst) == ' ') dst++; @@ -96,16 +98,30 @@ void truncate_spaces(char *str) { *(str + src) = '\0'; } +void truncate_spaces_trailing(char *str){ + int index, i = -1; + /* Find last index of non-white space character */ + i = 0; + while(str[i] != '\0'){ + if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n'){ + index= i; + } + i++; + } + + /* Mark next character to last non-white space character as NULL */ + str[index + 1] = '\0'; +} static char *get_sys(char *sys){ os_name = malloc(BUF_SIZE); char *name = malloc(BUF_SIZE); - FILE *sysName = fopen("/etc/issue", "r"); + FILE *sysName = fopen("/etc/issue", "rt"); fscanf(sysName, "%s ", name); fclose(sysName); - truncate_spaces(name); + truncate_spaces_leading(name); snprintf(os_name, BUF_SIZE, "%s Linux", name); @@ -117,11 +133,21 @@ static char *get_sys(char *sys){ } -void get_cpu(char cpu[]){ - FILE *cpuName = popen("grep -m 1 name /proc/cpuinfo | awk -F':' '{print $2}' | cut -c 2- ", "r"); +static char *get_cpu(void){ + char *line = malloc(BUF_SIZE * 4); + cpu_name = malloc(BUF_SIZE * 4); + FILE *cpu = fopen("/proc/cpuinfo", "rt"); + + for(int i = 0; i < 5; i++) + fgets(line, BUF_SIZE * 4, cpu); - fscanf(cpuName, "%[^\n]%s", cpu); - fclose(cpuName); + snprintf(cpu_name, BUF_SIZE * 4, "%s", strchr(line, ':') +2); + truncate_spaces_trailing(cpu_name); + + fclose(cpu); + free(line); + + return cpu_name; } void get_gpu(char gpu[]){ @@ -136,12 +162,12 @@ void get_gpu(char gpu[]){ static char *get_kernel(void){ kern_name = malloc(BUF_SIZE); char *kernel = malloc(BUF_SIZE); - FILE *kInfo = fopen("/proc/version", "r"); + FILE *kInfo = fopen("/proc/version", "rt"); fscanf(kInfo, "Linux version %s ", kernel); fclose(kInfo); - truncate_spaces(kernel); + truncate_spaces_leading(kernel); snprintf(kern_name, BUF_SIZE + 5, "Linux %s", kernel); free(kernel); @@ -150,7 +176,7 @@ static char *get_kernel(void){ static char *get_uptime(void){ int sec, hr, min; - FILE *FUp = fopen("/proc/uptime", "r"); + FILE *FUp = fopen("/proc/uptime", "rt"); uptime = malloc(BUF_SIZE); fscanf(FUp, "%d", &sec); @@ -203,16 +229,16 @@ static char *get_RAM(){ char *line = malloc(BUF_SIZE * 3); char bar[] = "[----------]"; - FILE *RAM = fopen("/proc/meminfo", "r"); + FILE *RAM = fopen("/proc/meminfo", "rt"); fgets(line, BUF_SIZE * 3, RAM); - truncate_spaces(line); + truncate_spaces_leading(line); sscanf(line, " MemTotal: %f", &total); fgets(line, BUF_SIZE * 3, RAM); - truncate_spaces(line); + truncate_spaces_leading(line); sscanf(line, " MemFree: %f", &free_mem); fclose(RAM);