From 083d601f11dded7873133ae9b2fab2c0a416f488 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 09:10:06 +0000 Subject: [PATCH] Replace fetchy.c --- fetchy.c | 107 +++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/fetchy.c b/fetchy.c index 56337be..ecc10dd 100644 --- a/fetchy.c +++ b/fetchy.c @@ -14,46 +14,38 @@ void get_gpu(char gpu[]); static char *get_kernel(void); static char *get_uptime(void); static int get_packages(char sys); -void get_RAM(float *total, float *used, char bar[]); - +static char *get_RAM(); +void read_line(char x); -char *os_name = NULL, *uptime = NULL, *kern_name = NULL; +char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL; int main(int argc, char *argv[]){ - - char cpu[70], gpu[70], sys; - char bar[] = "[----------]"; - float total = 0, used = 0; - - - //These get info - + char *os = malloc(BUF_SIZE); get_gpu(gpu); get_cpu(cpu); - get_RAM(&total, &used, bar); + os = get_sys(&sys); - - if ( !strcmp(argv[1], "-arch") ) { + if ( sys == 'a' ) { printf("\n"); - printf("\x1b[1m . \x1b[36mOS\x1b[0m -> %s\n", get_sys(&sys)); + printf("\x1b[1m . \x1b[36mOS\x1b[0m -> %s\n", os); printf("\x1b[1m / \\ \x1b[36mKRNL\x1b[0m -> %s\n", get_kernel()); 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[36mGPU\x1b[0m -> %s\n", gpu); - printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %.2f GB of %.2f GB, %s -> %.2f %%\n", used / 1000000, total / 1000000, bar, (used/total) * 100); + printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %s\n", get_RAM()); printf("\n\n"); } - if ( !strcmp(argv[1], "-ubuntu") ) { + if ( sys == 'u' ) { printf("\n"); printf("\x1b[1m .-. \x1b[36mOS\x1b[0m -> %s\n", get_sys(&sys)); @@ -63,7 +55,7 @@ int main(int argc, char *argv[]){ printf("\x1b[1m .-. , `___: \x1b[36m\x1b[0m \n"); printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", cpu); printf("\x1b[1m `-` ` , : \x1b[36mGPU\x1b[0m -> %s\n", gpu); - printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %.2f GB of %.2f GB, %s -> %.2f %%\n", used / 1000000, total / 1000000, bar, (used/total) * 100); + printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %s\n", get_RAM()); printf("\x1b[1m `./ / .-.` \x1b[36m\x1b[0m \n"); printf("\x1b[1m `-..-( ) \x1b[36m\x1b[0m \n"); printf("\x1b[1m `-` \x1b[36m\x1b[0m \n"); @@ -74,11 +66,21 @@ int main(int argc, char *argv[]){ free(os_name); free(uptime); + free(ram_info); free(kern_name); return 0; } + + +void read_line(char x) { + int c; + while (( c = getchar()) != x && c != EOF) { } +} + + + void truncate_spaces(char *str) { int src = 0, dst = 0; while(*(str + dst) == ' ') dst++; @@ -103,15 +105,14 @@ static char *get_sys(char *sys){ fscanf(sysName, "%s ", name); fclose(sysName); - snprintf(os_name, BUF_SIZE, "%s Linux", name ); + truncate_spaces(name); + + snprintf(os_name, BUF_SIZE, "%s Linux", name); - if ( !strcmp( name, "Arch")) - *sys = 'a'; - if ( !strcmp( name, "Ubuntu")) - *sys = 'u'; + if ( !strcmp( name, "Arch")) *sys = 'a'; + if ( !strcmp( name, "Ubuntu")) *sys = 'u'; free(name); - truncate_spaces(os_name); return os_name; } @@ -132,7 +133,7 @@ void get_gpu(char gpu[]){ -static char *get_kernel(){ +static char *get_kernel(void){ kern_name = malloc(BUF_SIZE); char *kernel = malloc(BUF_SIZE); FILE *kInfo = fopen("/proc/version", "r"); @@ -194,45 +195,43 @@ static int get_packages(char sys){ } -void get_RAM(float *total, float *used, char bar[]){ - int usedPercent; +static char *get_RAM(){ + int bar_percent; + float total, free_mem, used; + float real_percent; + ram_info = malloc(BUF_SIZE * 3); + char *line = malloc(BUF_SIZE * 3); + char bar[] = "[----------]"; - FILE *RAM = popen("free | grep Mem", "r"); + FILE *RAM = fopen("/proc/meminfo", "r"); + + + + fgets(line, BUF_SIZE * 3, RAM); + truncate_spaces(line); + sscanf(line, " MemTotal: %f", &total); + + fgets(line, BUF_SIZE * 3, RAM); + truncate_spaces(line); + sscanf(line, " MemFree: %f", &free_mem); - fscanf(RAM, "Mem: %f %f ", total, used); fclose(RAM); - usedPercent = round(((*used) / (*total)) * 10); + used = total - free_mem; + real_percent = (used / total) * 100; + bar_percent = round(real_percent / 10); - for( int i = 1; i <= usedPercent; i++){ + for( int i = 1; i <= bar_percent; i++){ bar[i] = '*'; } + + snprintf(ram_info, BUF_SIZE * 3, "%.2f GB of %.2f GB, %s -> %.2f %%", used / 1000000, total / 1000000, bar, real_percent); + + free(line); + return ram_info; } -/* -char *LOGO[] = { -" ▄ ", -" ▟█▙ ", -" ▟███▙ ", -" ▟█████▙ ", -" ▟███████▙ ", -" ▂▔▀▜██████▙ ", -" ▟██▅▂▝▜█████▙ ", -" ▟█████████████▙ ", -" ▟███████████████▙ ", -" ▟█████████████████▙ ", -" ▟███████████████████▙ ", -" ▟█████████▛▀▀▜████████▙ ", -" ▟████████▛ ▜███████▙ ", -" ▟█████████ ████████▙ ", -" ▟██████████ █████▆▅▄▃▂ ", -" ▟██████████▛ ▜█████████▙ ", -" ▟██████▀▀▀ ▀▀██████▙ ", -" ▟███▀▘ ▝▀███▙ ", -" ▟▛▀ ▀▜▙ " -}; -*/