Replace fetchy.c

This commit is contained in:
Јован Ђокић-Шумарац 2021-05-12 13:08:06 +00:00
parent a154185fb2
commit b34ba4e25d

View file

@ -9,6 +9,7 @@
void truncate_spaces_leading(char *str); void truncate_spaces_leading(char *str);
void truncate_spaces_trailing(char *str);
void read_line(char x); void read_line(char x);
int count_files(char *path); int count_files(char *path);
@ -16,21 +17,55 @@ int count_files(char *path);
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);
static int get_packages(char sys);
static char *get_RAM(char *ram_info); static char *get_RAM(char *ram_info);
static char *get_cpu(char *cpuname); static char *get_cpu(char *cpuname);
void get_gpu(char *gpu); static char *get_gpu(char *gpu);
static int get_packages(char sys);
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
char gpu[70], sys; char sys;
char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL; char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL, *gpu = NULL;
char *os = malloc(BUF_SIZE); char *os = malloc(BUF_SIZE), *cache_path = malloc(BUF_SIZE * 2);
snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME"));
FILE *FCache = NULL;
if( (FCache = fopen(cache_path, "r")) ){
cpu_name = malloc(BUF_SIZE * 4);
gpu = malloc(BUF_SIZE * 4);
char *line = malloc(BUF_SIZE * 4);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(cpu_name, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
truncate_spaces_trailing(cpu_name);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(gpu, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
truncate_spaces_trailing(gpu);
fclose(FCache);
free(line);
}
else {
FCache = fopen(cache_path, "w");
fprintf(FCache, "CPU : %s\n", get_cpu(cpu_name));
fprintf(FCache, "GPU : %s\n", get_gpu(gpu));
fclose(FCache);
printf("\n\n\aFILE CACHING DONE!! \n\nValues returned : \nCPU -> %s\nGPU -> %s\n\n Caching occurs only once, every subsequent execution will run program normally. Enjoy!\n\n", get_cpu(cpu_name), get_gpu(gpu));
exit(0);
}
//get_gpu(gpu);
os = get_sys(&sys, os_name); os = get_sys(&sys, os_name);
@ -44,8 +79,8 @@ int main(int argc, char *argv[]){
printf("\x1b[1m / \\ \x1b[36mUPT\x1b[0m -> %s\n", get_uptime(uptime)); printf("\x1b[1m / \\ \x1b[36mUPT\x1b[0m -> %s\n", get_uptime(uptime));
printf("\x1b[1m /^. \\ \x1b[36mPKGS\x1b[0m -> %d\n", get_packages(sys)); printf("\x1b[1m /^. \\ \x1b[36mPKGS\x1b[0m -> %d\n", get_packages(sys));
printf("\x1b[1m / .-. \\ \x1b[36m\x1b[0m \n"); printf("\x1b[1m / .-. \\ \x1b[36m\x1b[0m \n");
printf("\x1b[1m / ( ) _\\ \x1b[36mCPU\x1b[0m -> %s\n", get_cpu(cpu_name)); printf("\x1b[1m / ( ) _\\ \x1b[36mCPU\x1b[0m -> %s\n", cpu_name);
printf("\x1b[1m / _.~ ~._^\\ \x1b[36mGPU\x1b[0m -> *W.I.P.*\n"); printf("\x1b[1m / _.~ ~._^\\ \x1b[36mGPU\x1b[0m -> %s\n", gpu);
printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info)); printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info));
printf("\n\n"); printf("\n\n");
break; break;
@ -59,7 +94,7 @@ int main(int argc, char *argv[]){
printf("\x1b[1m ,`\\ \\ `-`. \x1b[36mUPTIME\x1b[0m -> %s\n", get_uptime(uptime)); printf("\x1b[1m ,`\\ \\ `-`. \x1b[36mUPTIME\x1b[0m -> %s\n", get_uptime(uptime));
printf("\x1b[1m / \\ '``-. ` \x1b[36mPACKAGES\x1b[0m -> *W.I.P.*\n"); //%d\n", get_packages(sys)); printf("\x1b[1m / \\ '``-. ` \x1b[36mPACKAGES\x1b[0m -> *W.I.P.*\n"); //%d\n", get_packages(sys));
printf("\x1b[1m .-. , `___: \x1b[36m\x1b[0m \n"); printf("\x1b[1m .-. , `___: \x1b[36m\x1b[0m \n");
printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", get_cpu(cpu_name)); printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", cpu_name);
printf("\x1b[1m `-` ` , : \x1b[36mGPU\x1b[0m -> %s\n", gpu); printf("\x1b[1m `-` ` , : \x1b[36mGPU\x1b[0m -> %s\n", gpu);
printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info)); printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info));
printf("\x1b[1m `./ / .-.` \x1b[36m\x1b[0m \n"); printf("\x1b[1m `./ / .-.` \x1b[36m\x1b[0m \n");
@ -80,6 +115,8 @@ int main(int argc, char *argv[]){
free(ram_info); free(ram_info);
free(kern_name); free(kern_name);
free(cpu_name); free(cpu_name);
free(gpu);
free(cache_path);
return 0; return 0;
} }
@ -162,11 +199,19 @@ static char *get_cpu(char *cpu_name){
} }
void get_gpu(char *gpu){ static char *get_gpu(char *gpu){
gpu = malloc(BUF_SIZE * 3);
FILE *gpuName = popen("lspci -v | grep VGA -m 1 | awk -F'[' '{ print $2 }' | awk -F']' '{ print $1 }' ", "r"); FILE *gpuName = popen("lspci -v | grep VGA -m 1 | awk -F'[' '{ print $2 }' | awk -F']' '{ print $1 }' ", "r");
fscanf(gpuName, "%[^\n]%s", gpu); fscanf(gpuName, "%[^\n]%s", gpu);
truncate_spaces_leading(gpu);
truncate_spaces_trailing(gpu);
fclose(gpuName); fclose(gpuName);
return gpu;
} }
@ -259,7 +304,7 @@ static char *get_RAM(char *ram_info){
used = total - free_mem; used = total - free_mem;
real_percent = (used / total) * 100; real_percent = (used / total) * 100;
bar_percent = round(real_percent / 10); 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] = '*';