Replace fetchy.c

This commit is contained in:
Јован Ђокић-Шумарац 2021-05-17 17:54:37 +00:00
parent ccf01cd73c
commit 14fa47b2d7

194
fetchy.c
View file

@ -4,18 +4,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <dirent.h> #include <dirent.h>
#include "ascii.h"
#define BUF_SIZE 50 #define BUF_SIZE 50
void truncate_spaces_leading(char *str); void truncate_spaces_leading(char *str);
void truncate_spaces_trailing(char *str); void truncate_spaces_trailing(char *str);
void read_line(char x); void read_line(char x);
void cache_info(char *cache_path, char **cpu_name, char **gpu); void cache_info(char *cache_path, char **cpu_name, char **gpu);
static int count_files(DIR *package_dir); static int count_files(DIR *package_dir);
static char *get_sys(char *sys, char *os_name); static char *get_sys(char *sys, char *os_name);
static char *get_kernel(char *kern_name); static char *get_kernel(char *kern_name);
static char *get_uptime(char *uptime); static char *get_uptime(char *uptime);
@ -23,74 +23,37 @@ static char *get_RAM(char *ram_info);
static char *get_cpu(char *cpuname); static char *get_cpu(char *cpuname);
static char *get_gpu(char *gpu); static char *get_gpu(char *gpu);
static char *get_packages(char *package_count, char sys); static char *get_packages(char *package_count, char sys);
void concatenate_and_print(char *ascii, int line_n);
void concatenate_and_print(char sys, char *os, char *cpu_name, char *gpu);
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
char sys; char sys,
char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL, *gpu = NULL, *package_count = NULL; *os_name = NULL,
char *os = malloc(BUF_SIZE), *cache_path = malloc(BUF_SIZE * 2); *cpu_name = NULL,
*gpu = NULL,
*os = malloc(BUF_SIZE),
*cache_path = malloc(BUF_SIZE * 2);
cache_info(cache_path, &cpu_name, &gpu); cache_info(cache_path, &cpu_name, &gpu);
os = get_sys(&sys, os_name); os = get_sys(&sys, os_name);
switch(sys){
case 'x': //This one is in logos.h
case 'a': concatenate_and_print(sys, os, cpu_name, gpu);
printf("\n");
printf("\x1b[1m . %s\n", os);
printf("\x1b[1m / \\ %s\n", get_kernel(kern_name));
printf("\x1b[1m / \\ %s\n", get_uptime(uptime));
printf("\x1b[1m /^. \\ %s\n", get_packages(package_count, sys));
printf("\x1b[1m / .-. \\ \n");
printf("\x1b[1m / ( ) _\\ %s\n", cpu_name);
printf("\x1b[1m / _.~ ~._^\\ %s\n", gpu);
printf("\x1b[1m /.^ ^.\\ %s\n", get_RAM(ram_info));
printf("\n\n");
break;
case 'u':
printf("\n");
printf("\x1b[1m .-. %s\n", os);
printf("\x1b[1m .-'``(|||) %s\n", get_kernel(kern_name));
printf("\x1b[1m ,`\\ \\ `-`. %s\n", get_uptime(uptime));
printf("\x1b[1m / \\ '``-. ` %s\n", get_packages(package_count, sys)); //%d\n", get_packages(sys));
printf("\x1b[1m .-. , `___: \n");
printf("\x1b[1m (:::) : ___ %s\n", cpu_name);
printf("\x1b[1m `-` ` , : %s\n", gpu);
printf("\x1b[1m \\ / ,..-` , %s\n", get_RAM(ram_info));
printf("\x1b[1m `./ / .-.` \n");
printf("\x1b[1m `-..-( ) \n");
printf("\x1b[1m `-` \n");
printf("\n\n");
break;
/* case 'x':
strcat(artix[3], os);
for (int i = 0; i < 14; i++)
printf("%s\n", artix[i]);
*/
default:
printf("\n\n ERROR : Unsupported system\n\n");
exit(-1);
}
free(os_name); free(uptime); free(ram_info); free(kern_name); free(cpu_name); free(gpu); free(cache_path); free(package_count); free(os_name);
free(cpu_name);
free(gpu);
free(cache_path);
return 0; return 0;
} }
@ -104,8 +67,9 @@ void read_line(char x){
// taken from https://github.com/ss7m/paleofetch // taken from https://github.com/ss7m/paleofetch
void truncate_spaces_leading(char *str){ void truncate_spaces_leading(char *str){
int src = 0, dst = 0; int src = 0, dst = 0;
while(*(str + dst) == ' ') dst++; while(*(str + dst) == ' ') dst++;
@ -121,6 +85,8 @@ void truncate_spaces_leading(char *str){
} }
void truncate_spaces_trailing(char *str){ void truncate_spaces_trailing(char *str){
int index, i = -1; int index, i = -1;
@ -138,6 +104,8 @@ void truncate_spaces_trailing(char *str){
} }
void cache_info(char *cache_path, char **cpu_name, char **gpu){ void cache_info(char *cache_path, char **cpu_name, char **gpu){
snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME")); snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME"));
@ -164,6 +132,13 @@ void cache_info(char *cache_path, char **cpu_name, char **gpu){
else { else {
FCache = fopen(cache_path, "w"); FCache = fopen(cache_path, "w");
if( FCache == NULL ){
printf("\nCan't open cache file.\n");
exit(EXIT_FAILURE);
}
fprintf(FCache, "CPU : %s\n", get_cpu(*cpu_name)); fprintf(FCache, "CPU : %s\n", get_cpu(*cpu_name));
fprintf(FCache, "GPU : %s\n", get_gpu(*gpu)); fprintf(FCache, "GPU : %s\n", get_gpu(*gpu));
fclose(FCache); fclose(FCache);
@ -175,6 +150,8 @@ void cache_info(char *cache_path, char **cpu_name, char **gpu){
} }
} }
static int count_files(DIR *package_dir){ static int count_files(DIR *package_dir){
struct dirent * entry; struct dirent * entry;
@ -192,19 +169,28 @@ static int count_files(DIR *package_dir){
static char *get_sys(char *sys, char *os_name){ static char *get_sys(char *sys, char *os_name){
os_name = malloc(BUF_SIZE); os_name = malloc(BUF_SIZE);
char *name = malloc(BUF_SIZE); char *name = malloc(BUF_SIZE);
FILE *sysName = fopen("/etc/issue", "rt"); FILE *sysName = fopen("/etc/issue", "rt");
if( sysName == NULL ){
printf("\nCan't get os name\n");
exit(EXIT_FAILURE);
}
fscanf(sysName, "%s ", name); fscanf(sysName, "%s ", name);
fclose(sysName); fclose(sysName);
truncate_spaces_leading(name); truncate_spaces_leading(name);
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s Linux", name); snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s Linux", name);
if ( !strcmp( name, "Arch")) *sys = 'a'; if ( !strcmp( name, "Arch") || !strcmp( name, "Arco") ) *sys = 'a';
if ( !strcmp( name, "Artix")) *sys = 'x'; if ( !strcmp( name, "Artix") ) *sys = 'x';
if ( !strcmp( name, "Debian")) *sys = 'd';
if ( !strcmp( name, "Ubuntu")) *sys = 'u'; if ( !strcmp( name, "Ubuntu")) *sys = 'u';
free(name); free(name);
@ -212,11 +198,22 @@ static char *get_sys(char *sys, char *os_name){
} }
static char *get_cpu(char *cpu_name){ static char *get_cpu(char *cpu_name){
char *line = malloc(BUF_SIZE * 4); char *line = malloc(BUF_SIZE * 4);
cpu_name = malloc(BUF_SIZE * 4); cpu_name = malloc(BUF_SIZE * 4);
FILE *cpu = fopen("/proc/cpuinfo", "rt"); FILE *cpu = fopen("/proc/cpuinfo", "rt");
if( cpu == NULL ){
printf("\nCan't get cpu info\n");
exit(EXIT_FAILURE);
}
for(int i = 0; i < 5; i++) for(int i = 0; i < 5; i++)
fgets(line, BUF_SIZE * 4, cpu); fgets(line, BUF_SIZE * 4, cpu);
@ -230,6 +227,8 @@ static char *get_cpu(char *cpu_name){
} }
static char *get_gpu(char *gpu){ static char *get_gpu(char *gpu){
gpu = malloc(BUF_SIZE * 3); gpu = malloc(BUF_SIZE * 3);
@ -250,11 +249,21 @@ static char *get_gpu(char *gpu){
} }
static char *get_kernel(char *kern_name){ static char *get_kernel(char *kern_name){
kern_name = malloc(BUF_SIZE); kern_name = malloc(BUF_SIZE);
char *kernel = malloc(BUF_SIZE); char *kernel = malloc(BUF_SIZE);
FILE *kInfo = fopen("/proc/version", "rt"); FILE *kInfo = fopen("/proc/version", "rt");
if( kInfo == NULL ){
printf("\nCan't get kernel info\n");
exit(EXIT_FAILURE);
}
fscanf(kInfo, "Linux version %s ", kernel); fscanf(kInfo, "Linux version %s ", kernel);
fclose(kInfo); fclose(kInfo);
@ -266,11 +275,25 @@ static char *get_kernel(char *kern_name){
} }
static char *get_uptime(char *uptime){ static char *get_uptime(char *uptime){
int sec, hr, min;
int sec,
hr,
min;
FILE *FUp = fopen("/proc/uptime", "rt"); FILE *FUp = fopen("/proc/uptime", "rt");
uptime = malloc(BUF_SIZE); uptime = malloc(BUF_SIZE);
if( FUp == NULL ){
printf("\nCan't get uptime info\n");
exit(EXIT_FAILURE);
}
fscanf(FUp, "%d", &sec); fscanf(FUp, "%d", &sec);
fclose(FUp); fclose(FUp);
@ -282,20 +305,30 @@ static char *get_uptime(char *uptime){
} }
static char *get_packages(char *package_count, char sys){ static char *get_packages(char *package_count, char sys){
//DIR *package_dir;
int pkg_count = 0; int pkg_count = 0;
package_count = malloc(BUF_SIZE); package_count = malloc(BUF_SIZE);
switch (sys){ switch (sys){
case 'x': case 'x':
case 'a': case 'a':
pkg_count = count_files(opendir("/var/lib/pacman/local")); pkg_count = count_files(opendir("/var/lib/pacman/local"));
break; break;
case 'd':
case 'u': case 'u':
pkg_count = count_files(opendir("/usr/bin")) + count_files(opendir("/sbin")); pkg_count = count_files(opendir("/usr/bin")) + count_files(opendir("/sbin"));
break; break;
default: default:
printf("\n\n\aUnable to find package list\n\n"); printf("\n\n\aUnable to find package list\n\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -308,13 +341,21 @@ static char *get_packages(char *package_count, char sys){
} }
static char *get_RAM(char *ram_info){ static char *get_RAM(char *ram_info){
int bar_percent; int bar_percent;
float total, free_mem, used;
float real_percent; float total,
free_mem,
used,
real_percent;
ram_info = malloc(BUF_SIZE * 3); ram_info = malloc(BUF_SIZE * 3);
char *line = malloc(BUF_SIZE * 3);
char bar[] = "[----------]"; char *line = malloc(BUF_SIZE * 3),
bar[] = "[----------]";
FILE *RAM = fopen("/proc/meminfo", "rt"); FILE *RAM = fopen("/proc/meminfo", "rt");
@ -332,14 +373,17 @@ static char *get_RAM(char *ram_info){
fclose(RAM); fclose(RAM);
used = total - free_mem; used = total - free_mem;
real_percent = (used / total) * 100; real_percent = (used / total) * 100;
bar_percent = (real_percent / 10 + 0.35); bar_percent = (real_percent / 10 + 0.35);
for( int i = 1; i <= bar_percent; i++){ for( int i = 1; i <= bar_percent; i++){
bar[i] = '*'; bar[i] = '*';
} }
snprintf(ram_info, BUF_SIZE * 3, "\x1b[36mRAM\x1b[0m -> %.2f GB of %.2f GB, %s -> %.2f %%", used / 1000000, total / 1000000, bar, real_percent); snprintf(ram_info, BUF_SIZE * 3, "\x1b[36mRAM\x1b[0m -> %.2f GB of %.2f GB, %s -> %.2f %%", used / 1000000, total / 1000000, bar, real_percent);
free(line); free(line);
@ -348,18 +392,4 @@ static char *get_RAM(char *ram_info){
#include "logos.h"
/*void concatenate_and_print(char *ascii, int line_n){
strcat(ascii[0], os);
strcat(ascii[1], os);
strcat(ascii[2], os);
strcat(ascii[3], os);
strcat(ascii[4], os);
strcat(ascii[5], os);
strcat(ascii[6], os);
strcat(ascii[7], os);
for (int i = 0; i < line_n ; i++)
printf("%s\n", artix[i]);
}*/