2021-05-10 13:47:11 +02:00
# pragma GCC diagnostic ignored "-Wunused-function"
2021-05-03 21:47:45 +02:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
2021-05-10 08:25:03 +02:00
# include <math.h>
# include <dirent.h>
2021-05-03 21:47:45 +02:00
2021-05-13 18:43:02 +02:00
# define BUF_SIZE 50
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
2021-05-10 12:24:24 +02:00
void truncate_spaces_leading ( char * str ) ;
2021-05-12 15:08:06 +02:00
void truncate_spaces_trailing ( char * str ) ;
2021-05-10 12:24:24 +02:00
void read_line ( char x ) ;
2021-05-13 18:43:02 +02:00
void cache_info ( char * cache_path , char * * cpu_name , char * * gpu ) ;
2021-05-10 12:24:24 +02:00
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
static int count_files ( DIR * package_dir ) ;
2021-05-10 13:09:05 +02:00
static char * get_sys ( char * sys , char * os_name ) ;
static char * get_kernel ( char * kern_name ) ;
static char * get_uptime ( char * uptime ) ;
static char * get_RAM ( char * ram_info ) ;
static char * get_cpu ( char * cpuname ) ;
2021-05-12 15:08:06 +02:00
static char * get_gpu ( char * gpu ) ;
2021-05-13 18:43:02 +02:00
static char * get_packages ( char * package_count , char sys ) ;
2021-05-10 08:25:03 +02:00
2021-05-13 18:43:02 +02:00
2021-05-17 19:54:37 +02:00
void concatenate_and_print ( char sys , char * os , char * cpu_name , char * gpu ) ;
2021-05-13 18:43:02 +02:00
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
int main ( int argc , char * argv [ ] ) {
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
char sys ,
* os_name = NULL ,
* cpu_name = NULL ,
* gpu = NULL ,
* os = malloc ( BUF_SIZE ) ,
* cache_path = malloc ( BUF_SIZE * 2 ) ;
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
cache_info ( cache_path , & cpu_name , & gpu ) ;
os = get_sys ( & sys , os_name ) ;
2021-05-13 18:43:02 +02:00
2021-05-18 10:15:32 +02:00
if ( argv [ 1 ] ! = NULL ) {
sys = * ( argv [ 1 ] + 1 ) ;
}
2021-05-13 18:43:02 +02:00
2021-05-17 19:54:37 +02:00
//This one is in logos.h
concatenate_and_print ( sys , os , cpu_name , gpu ) ;
2021-05-13 18:43:02 +02:00
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
free ( os_name ) ;
free ( cpu_name ) ;
free ( gpu ) ;
free ( cache_path ) ;
2021-05-10 08:25:03 +02:00
return 0 ;
2021-05-03 21:47:45 +02:00
}
2021-05-10 11:10:06 +02:00
2021-05-10 12:24:24 +02:00
void read_line ( char x ) {
2021-05-10 11:10:06 +02:00
int c ;
while ( ( c = getchar ( ) ) ! = x & & c ! = EOF ) { }
}
2021-05-13 02:09:25 +02:00
2021-05-17 19:54:37 +02:00
// taken from https://github.com/ss7m/paleofetch
2021-05-10 12:24:24 +02:00
void truncate_spaces_leading ( char * str ) {
2021-05-17 19:54:37 +02:00
2021-05-10 08:25:03 +02:00
int src = 0 , dst = 0 ;
while ( * ( str + dst ) = = ' ' ) dst + + ;
2021-05-03 21:47:45 +02:00
2021-05-10 08:25:03 +02:00
while ( * ( str + dst ) ! = ' \0 ' ) {
* ( str + src ) = * ( str + dst ) ;
if ( * ( str + ( dst + + ) ) = = ' ' )
while ( * ( str + dst ) = = ' ' ) dst + + ;
2021-05-03 21:47:45 +02:00
2021-05-10 08:25:03 +02:00
src + + ;
}
2021-05-03 21:47:45 +02:00
2021-05-10 08:25:03 +02:00
* ( str + src ) = ' \0 ' ;
}
2021-05-03 21:47:45 +02:00
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-10 12:24:24 +02:00
void truncate_spaces_trailing ( char * str ) {
int index , i = - 1 ;
2021-05-03 21:47:45 +02:00
2021-05-13 02:09:25 +02:00
2021-05-10 12:24:24 +02:00
i = 0 ;
while ( str [ i ] ! = ' \0 ' ) {
if ( str [ i ] ! = ' ' & & str [ i ] ! = ' \t ' & & str [ i ] ! = ' \n ' ) {
index = i ;
}
i + + ;
}
2021-05-13 02:09:25 +02:00
2021-05-10 12:24:24 +02:00
str [ index + 1 ] = ' \0 ' ;
}
2021-05-03 21:47:45 +02:00
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-13 18:43:02 +02:00
void cache_info ( char * cache_path , char * * cpu_name , char * * gpu ) {
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 , " \x1b [36mCPU \x1b [0m -> %s " , strchr ( line , ' : ' ) + 2 ) ;
truncate_spaces_trailing ( * cpu_name ) ;
fgets ( line , BUF_SIZE * 4 , FCache ) ;
snprintf ( * gpu , BUF_SIZE * 4 , " \x1b [36mGPU \x1b [0m -> %s " , strchr ( line , ' : ' ) + 2 ) ;
truncate_spaces_trailing ( * gpu ) ;
fclose ( FCache ) ;
free ( line ) ;
}
else {
FCache = fopen ( cache_path , " w " ) ;
2021-05-17 19:54:37 +02:00
if ( FCache = = NULL ) {
printf ( " \n Can't open cache file. \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2021-05-13 18:43:02 +02:00
fprintf ( FCache , " CPU : %s \n " , get_cpu ( * cpu_name ) ) ;
fprintf ( FCache , " GPU : %s \n " , get_gpu ( * gpu ) ) ;
fclose ( FCache ) ;
printf ( " \n \a FILE CACHING DONE!! \n \n Values returned : \n CPU -> %s \n GPU -> %s \n \n Caching occurs only once, every subsequent execution will run program normally. Enjoy! \n \n " , get_cpu ( * cpu_name ) , get_gpu ( * gpu ) ) ;
free ( * cpu_name ) ; free ( * gpu ) ;
exit ( 0 ) ;
}
}
2021-05-17 19:54:37 +02:00
2021-05-13 16:09:45 +02:00
static int count_files ( DIR * package_dir ) {
struct dirent * entry ;
int file_count = 0 ;
while ( ( entry = readdir ( package_dir ) ) ! = NULL ) {
if ( entry - > d_type = = DT_DIR ) {
file_count + + ;
}
}
closedir ( package_dir ) ;
return file_count ;
}
2021-05-17 19:54:37 +02:00
2021-05-10 13:09:05 +02:00
static char * get_sys ( char * sys , char * os_name ) {
2021-05-10 08:25:03 +02:00
os_name = malloc ( BUF_SIZE ) ;
char * name = malloc ( BUF_SIZE ) ;
2021-05-10 12:24:24 +02:00
FILE * sysName = fopen ( " /etc/issue " , " rt " ) ;
2021-05-10 08:25:03 +02:00
2021-05-17 19:54:37 +02:00
if ( sysName = = NULL ) {
printf ( " \n Can't get os name \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2021-05-10 08:25:03 +02:00
fscanf ( sysName , " %s " , name ) ;
fclose ( sysName ) ;
2021-05-10 12:24:24 +02:00
truncate_spaces_leading ( name ) ;
2021-05-10 11:10:06 +02:00
2021-05-13 18:43:02 +02:00
snprintf ( os_name , BUF_SIZE , " \x1b [36mOS \x1b [0m -> %s Linux " , name ) ;
2021-05-10 08:25:03 +02:00
2021-05-20 00:53:11 +02:00
if ( ! strcmp ( name , " Arch " ) ) * sys = ' a ' ;
if ( ! strcmp ( name , " Artix " ) ) * sys = ' x ' ;
if ( ! strcmp ( name , " Arco " ) ) * sys = ' r ' ;
2021-05-17 19:54:37 +02:00
if ( ! strcmp ( name , " Debian " ) ) * sys = ' d ' ;
if ( ! strcmp ( name , " Ubuntu " ) ) * sys = ' u ' ;
2021-05-10 08:25:03 +02:00
free ( name ) ;
return os_name ;
2021-05-03 21:47:45 +02:00
}
2021-05-17 19:54:37 +02:00
2021-05-10 13:09:05 +02:00
static char * get_cpu ( char * cpu_name ) {
2021-05-17 19:54:37 +02:00
2021-05-10 12:24:24 +02:00
char * line = malloc ( BUF_SIZE * 4 ) ;
2021-05-17 19:54:37 +02:00
cpu_name = malloc ( BUF_SIZE * 4 ) ;
2021-05-10 12:24:24 +02:00
FILE * cpu = fopen ( " /proc/cpuinfo " , " rt " ) ;
2021-05-17 19:54:37 +02:00
if ( cpu = = NULL ) {
printf ( " \n Can't get cpu info \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2021-05-10 12:24:24 +02:00
for ( int i = 0 ; i < 5 ; i + + )
2021-05-17 19:54:37 +02:00
fgets ( line , BUF_SIZE * 4 , cpu ) ;
2021-05-10 12:24:24 +02:00
snprintf ( cpu_name , BUF_SIZE * 4 , " %s " , strchr ( line , ' : ' ) + 2 ) ;
truncate_spaces_trailing ( cpu_name ) ;
fclose ( cpu ) ;
free ( line ) ;
2021-05-03 21:47:45 +02:00
2021-05-10 12:24:24 +02:00
return cpu_name ;
2021-05-03 21:47:45 +02:00
}
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-12 15:08:06 +02:00
static char * get_gpu ( char * gpu ) {
gpu = malloc ( BUF_SIZE * 3 ) ;
2021-05-03 21:47:45 +02:00
FILE * gpuName = popen ( " lspci -v | grep VGA -m 1 | awk -F'[' '{ print $2 }' | awk -F']' '{ print $1 }' " , " r " ) ;
fscanf ( gpuName , " %[^ \n ]%s " , gpu ) ;
2021-05-12 15:08:06 +02:00
truncate_spaces_leading ( gpu ) ;
truncate_spaces_trailing ( gpu ) ;
2021-05-03 21:47:45 +02:00
fclose ( gpuName ) ;
2021-05-12 16:22:29 +02:00
2021-05-12 16:25:44 +02:00
if ( strstr ( gpu , " VGA " ) )
2021-05-12 16:22:29 +02:00
strcpy ( gpu , " CPU's integrated graphics " ) ;
2021-05-12 15:08:06 +02:00
return gpu ;
2021-05-03 21:47:45 +02:00
}
2021-05-17 19:54:37 +02:00
2021-05-10 13:09:05 +02:00
static char * get_kernel ( char * kern_name ) {
2021-05-17 19:54:37 +02:00
2021-05-10 08:25:03 +02:00
kern_name = malloc ( BUF_SIZE ) ;
char * kernel = malloc ( BUF_SIZE ) ;
2021-05-10 12:24:24 +02:00
FILE * kInfo = fopen ( " /proc/version " , " rt " ) ;
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
if ( kInfo = = NULL ) {
printf ( " \n Can't get kernel info \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2021-05-10 08:25:03 +02:00
fscanf ( kInfo , " Linux version %s " , kernel ) ;
2021-05-03 21:47:45 +02:00
fclose ( kInfo ) ;
2021-05-10 12:24:24 +02:00
truncate_spaces_leading ( kernel ) ;
2021-05-20 00:53:11 +02:00
snprintf ( kern_name , BUF_SIZE + 5 , " \x1b [36mKERN \x1b [0m -> Linux %s " , kernel ) ;
2021-05-10 08:25:03 +02:00
free ( kernel ) ;
return kern_name ;
}
2021-05-03 21:47:45 +02:00
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-10 13:09:05 +02:00
static char * get_uptime ( char * uptime ) {
2021-05-17 19:54:37 +02:00
int sec ,
hr ,
min ;
2021-05-10 12:24:24 +02:00
FILE * FUp = fopen ( " /proc/uptime " , " rt " ) ;
2021-05-17 19:54:37 +02:00
uptime = malloc ( BUF_SIZE ) ;
if ( FUp = = NULL ) {
printf ( " \n Can't get uptime info \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2021-05-03 21:47:45 +02:00
fscanf ( FUp , " %d " , & sec ) ;
fclose ( FUp ) ;
2021-05-10 08:25:03 +02:00
hr = ( sec / 60 / 60 % 24 ) ;
min = ( sec / 60 % 60 ) ;
2021-05-13 18:43:02 +02:00
snprintf ( uptime , BUF_SIZE , " \x1b [36mUPT \x1b [0m -> %dh, %dmin " , hr , min ) ;
2021-05-10 08:25:03 +02:00
return uptime ;
2021-05-03 21:47:45 +02:00
}
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-13 18:43:02 +02:00
static char * get_packages ( char * package_count , char sys ) {
2021-05-10 08:25:03 +02:00
2021-05-13 16:09:45 +02:00
int pkg_count = 0 ;
2021-05-17 19:54:37 +02:00
package_count = malloc ( BUF_SIZE ) ;
2021-05-03 21:47:45 +02:00
switch ( sys ) {
2021-05-17 19:54:37 +02:00
2021-05-20 00:53:11 +02:00
case ' r ' :
2021-05-17 19:54:37 +02:00
case ' x ' :
2021-05-03 21:47:45 +02:00
case ' a ' :
2021-05-17 19:54:37 +02:00
2021-05-13 16:09:45 +02:00
pkg_count = count_files ( opendir ( " /var/lib/pacman/local " ) ) ;
2021-05-03 21:47:45 +02:00
break ;
2021-05-17 19:54:37 +02:00
case ' d ' :
2021-05-03 21:47:45 +02:00
case ' u ' :
2021-05-17 19:54:37 +02:00
2021-05-13 16:09:45 +02:00
pkg_count = count_files ( opendir ( " /usr/bin " ) ) + count_files ( opendir ( " /sbin " ) ) ;
2021-05-03 21:47:45 +02:00
break ;
2021-05-17 19:54:37 +02:00
2021-05-03 21:47:45 +02:00
default :
printf ( " \n \n \a Unable to find package list \n \n " ) ;
2021-05-17 15:45:11 +02:00
exit ( EXIT_FAILURE ) ;
2021-05-03 21:47:45 +02:00
}
2021-05-13 18:43:02 +02:00
snprintf ( package_count , BUF_SIZE , " \x1b [36mPKGS \x1b [0m -> %d " , pkg_count ) ;
return package_count ;
2021-05-03 21:47:45 +02:00
}
2021-05-10 13:09:05 +02:00
2021-05-17 19:54:37 +02:00
2021-05-10 13:09:05 +02:00
static char * get_RAM ( char * ram_info ) {
2021-05-17 19:54:37 +02:00
2021-05-10 11:10:06 +02:00
int bar_percent ;
2021-05-17 19:54:37 +02:00
float total ,
free_mem ,
used ,
real_percent ;
ram_info = malloc ( BUF_SIZE * 3 ) ;
char * line = malloc ( BUF_SIZE * 3 ) ,
bar [ ] = " [----------] " ;
2021-05-10 11:10:06 +02:00
2021-05-10 12:24:24 +02:00
FILE * RAM = fopen ( " /proc/meminfo " , " rt " ) ;
2021-05-04 16:46:09 +02:00
2021-05-10 11:10:06 +02:00
fgets ( line , BUF_SIZE * 3 , RAM ) ;
2021-05-10 12:24:24 +02:00
truncate_spaces_leading ( line ) ;
2021-05-10 11:10:06 +02:00
sscanf ( line , " MemTotal: %f " , & total ) ;
2021-05-17 19:54:37 +02:00
fgets ( line , BUF_SIZE * 3 , RAM ) ;
2021-05-13 02:09:25 +02:00
2021-05-10 11:10:06 +02:00
fgets ( line , BUF_SIZE * 3 , RAM ) ;
2021-05-10 12:24:24 +02:00
truncate_spaces_leading ( line ) ;
2021-05-13 02:09:25 +02:00
sscanf ( line , " MemAvailable: %f " , & free_mem ) ;
2021-05-04 16:46:09 +02:00
fclose ( RAM ) ;
2021-05-17 19:54:37 +02:00
2021-05-10 11:10:06 +02:00
used = total - free_mem ;
real_percent = ( used / total ) * 100 ;
2021-05-12 15:08:06 +02:00
bar_percent = ( real_percent / 10 + 0.35 ) ;
2021-05-04 16:46:09 +02:00
2021-05-17 19:54:37 +02:00
2021-05-10 11:10:06 +02:00
for ( int i = 1 ; i < = bar_percent ; i + + ) {
2021-05-04 16:46:09 +02:00
bar [ i ] = ' * ' ;
}
2021-05-10 11:10:06 +02:00
2021-05-17 19:54:37 +02:00
2021-05-13 18:43:02 +02:00
snprintf ( ram_info , BUF_SIZE * 3 , " \x1b [36mRAM \x1b [0m -> %.2f GB of %.2f GB, %s -> %.2f %% " , used / 1000000 , total / 1000000 , bar , real_percent ) ;
2021-05-10 11:10:06 +02:00
free ( line ) ;
return ram_info ;
2021-05-04 16:46:09 +02:00
}
2021-05-10 08:25:03 +02:00
2021-05-13 18:43:02 +02:00
2021-05-17 19:54:37 +02:00
# include "logos.h"