fetchy/fetchy.c

465 lines
9.2 KiB
C
Raw Normal View History

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-26 16:01:41 +02:00
#include <dirent.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <pci/pci.h>
2021-05-03 21:47:45 +02:00
2021-05-13 18:43:02 +02:00
#define BUF_SIZE 50
2021-05-26 12:23:42 +02:00
#define COLS 8
#define ROWS 100
2021-05-26 16:01:41 +02:00
//for easier checks
2021-05-26 12:23:42 +02:00
#define SUPPORTED_OS "xadurm"
#define ARCH_BASED "axmr"
#define DEBIAN_BASED "du"
2021-05-26 20:10:19 +02:00
#define OS_TABLE "a:Arch|x:Artix|r:Arco|m:Manjaro|u:Ubuntu|d:Debian"
2021-05-26 12:23:42 +02:00
2021-05-26 16:01:41 +02:00
//ascii art header file
2021-05-26 12:23:42 +02:00
#include "logos.h"
2021-05-03 21:47:45 +02:00
2021-05-26 16:01:41 +02:00
// standardish functions
2021-05-10 12:24:24 +02:00
void read_line(char x);
2021-05-26 20:10:19 +02:00
void cache_info(char **cpu_name, char **gpu);
2021-05-17 19:54:37 +02:00
static int count_files(DIR *package_dir);
2021-05-26 16:01:41 +02:00
// 'get info' functions
2021-05-26 20:10:19 +02:00
static char *get_sys(char *s_os, char *os_name);
2021-05-10 13:09:05 +02:00
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-26 12:23:42 +02:00
static char *get_packages(char *package_count);
2021-05-26 16:01:41 +02:00
// final function
2021-05-26 12:23:42 +02:00
void concatenate_print(char *os, char *cpu_name, char *gpu);
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
// easier if global, don't hate me
2021-05-26 12:23:42 +02:00
char sys;
int logo_number = 0;
2021-05-13 18:43:02 +02:00
2021-05-03 21:47:45 +02:00
2021-05-26 16:01:41 +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-26 16:01:41 +02:00
char *os_name = NULL,
2021-05-26 12:23:42 +02:00
*cpu_name = NULL,
2021-05-26 16:01:41 +02:00
*gpu = NULL,
2021-05-26 20:10:19 +02:00
*s_os = SUPPORTED_OS,
*os = malloc(BUF_SIZE);
2021-05-26 12:23:42 +02:00
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
// if cache file exits, read it, otherwise cache info
2021-05-26 20:10:19 +02:00
cache_info(&cpu_name, &gpu);
os = get_sys(s_os, os_name);
2021-05-26 12:23:42 +02:00
2021-05-26 20:10:19 +02:00
// check if custom ascii art argument is passed
2021-05-26 12:23:42 +02:00
if ( argv[1] != NULL ) {
2021-05-21 10:14:02 +02:00
2021-05-26 12:23:42 +02:00
if ( strchr(SUPPORTED_OS, *(argv[1] + 1)) )
logo_number = (int) (strchr(s_os, *(argv[1] + 1) ) - s_os);
else {
fprintf(stderr, "\n\nArgument not supported\n\n");
exit(EXIT_FAILURE);
}
}
2021-05-13 18:43:02 +02:00
2021-05-17 19:54:37 +02:00
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
concatenate_print( os, cpu_name, gpu);
2021-05-26 12:23:42 +02:00
2021-05-17 19:54:37 +02:00
free(os_name);
free(cpu_name);
2021-05-26 16:01:41 +02:00
free(gpu);
2021-05-26 20:10:19 +02:00
free(os);
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +02:00
return EXIT_SUCCESS;
2021-05-03 21:47:45 +02:00
}
2021-05-10 11:10:06 +02:00
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +02:00
void read_line(char x) {
2021-05-10 11:10:06 +02:00
int c;
2021-05-26 16:01:41 +02:00
while (( c = getchar()) != x && c != EOF) {}
2021-05-10 11:10:06 +02:00
}
2021-05-13 02:09:25 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
void truncate_spaces(char *str) {
int src = 0, dst = 0, index, i = -1;
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-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;
2021-05-26 12:23:42 +02:00
}
2021-05-10 12:24:24 +02:00
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-26 20:10:19 +02:00
void cache_info(char **cpu_name, char **gpu) {
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
char *cache_path = malloc(BUF_SIZE * 2);
FILE *f_cache = NULL;
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
// construct path to file
snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME"));
if( (f_cache = fopen(cache_path, "r")) ){
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
char *line = malloc(BUF_SIZE * 4);
*cpu_name = malloc(BUF_SIZE * 4);
*gpu = malloc(BUF_SIZE * 4);
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
//get CPU
fgets(line, BUF_SIZE * 4, f_cache);
2021-05-26 12:23:42 +02:00
truncate_spaces(line);
snprintf(*cpu_name, BUF_SIZE * 4, "\x1b[36mCPU\x1b[0m -> %s", strchr(line, ':') +2);
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
//get GPU
fgets(line, BUF_SIZE * 4, f_cache);
2021-05-26 12:23:42 +02:00
truncate_spaces(line);
snprintf(*gpu, BUF_SIZE * 4, "\x1b[36mGPU\x1b[0m -> %s", strchr(line, ':') +2);
2021-05-13 18:43:02 +02:00
2021-05-26 16:01:41 +02:00
fclose(f_cache);
2021-05-26 12:23:42 +02:00
free(line);
2021-05-26 16:01:41 +02:00
}
2021-05-13 18:43:02 +02:00
2021-05-26 12:23:42 +02:00
else {
2021-05-26 16:01:41 +02:00
f_cache = fopen(cache_path, "w");
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
if( f_cache == NULL ){
2021-05-17 19:54:37 +02:00
printf("\nCan't open cache file.\n");
exit(EXIT_FAILURE);
}
2021-05-26 16:01:41 +02:00
// Write info to file
fprintf(f_cache, "CPU : %s\n", get_cpu(*cpu_name));
fprintf(f_cache, "GPU : %s\n", get_gpu(*gpu));
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
fclose(f_cache);
2021-05-13 18:43:02 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
printf("\n FILE CACHING DONE!!\n");
printf("Caching occurs only once,\n");
printf("every subsequent execution will\n");
printf("run program normally. Enjoy!\n\n");
2021-05-26 20:10:19 +02:00
cache_info(cpu_name, gpu);
2021-05-26 12:23:42 +02:00
}
2021-05-26 16:01:41 +02:00
free(cache_path);
}
2021-05-26 12:23:42 +02:00
static int count_files(DIR *package_dir) {
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
struct dirent * entry;
int file_count = 0;
2021-05-10 11:10:06 +02:00
2021-05-26 12:23:42 +02:00
while ((entry = readdir(package_dir)) != NULL) {
if (entry->d_type == DT_DIR) {
file_count++;
}
}
closedir(package_dir);
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
return file_count;
}
2021-05-21 22:09:19 +02:00
2021-05-26 20:10:19 +02:00
static char *get_sys(char *s_os, char *os_name) {
2021-05-21 22:09:19 +02:00
2021-05-26 12:23:42 +02:00
char *name = malloc(BUF_SIZE);
2021-05-26 16:01:41 +02:00
FILE *f_sys_name = fopen("/etc/issue", "rt");
2021-05-26 20:10:19 +02:00
os_name = malloc(BUF_SIZE);
2021-05-21 22:09:19 +02:00
2021-05-26 16:01:41 +02:00
if( f_sys_name == NULL ){
2021-05-26 12:23:42 +02:00
fprintf(stderr, "\nCan't get os name\n");
exit(EXIT_FAILURE);
}
2021-05-21 22:09:19 +02:00
2021-05-26 16:01:41 +02:00
fscanf(f_sys_name, "%s %*s", name);
fclose(f_sys_name);
2021-05-26 12:23:42 +02:00
truncate_spaces(name);
2021-05-21 22:09:19 +02:00
2021-05-26 12:23:42 +02:00
2021-05-26 20:10:19 +02:00
sys = *(strstr(OS_TABLE, name) - 2);
2021-05-26 12:23:42 +02:00
logo_number = (int) (strchr(s_os, sys) - s_os);
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +02:00
if( sys == 'r' )
2021-05-26 16:01:41 +02:00
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s", name); // Arco has a retarded name to be honest, without a space
2021-05-26 12:23:42 +02:00
else
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s Linux", name);
2021-05-20 09:52:43 +02:00
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +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-26 12:23:42 +02:00
char *line = malloc(BUF_SIZE * 4);
cpu_name = malloc(BUF_SIZE * 4);
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
FILE *cpu = fopen("/proc/cpuinfo", "rt");
2021-05-17 19:54:37 +02:00
if( cpu == NULL ){
printf("\nCan't get cpu info\n");
exit(EXIT_FAILURE);
}
2021-05-26 16:01:41 +02:00
// Skip first four lines, cpu info is on fifth
2021-05-26 12:23:42 +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
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +02:00
snprintf(cpu_name, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
truncate_spaces(cpu_name);
2021-05-26 16:01:41 +02:00
2021-05-26 12:23:42 +02:00
fclose(cpu);
free(line);
2021-05-03 21:47:45 +02:00
2021-05-26 12:23:42 +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){
2021-05-26 16:01:41 +02:00
gpu = malloc(BUF_SIZE);
2021-05-26 12:23:42 +02:00
2021-05-26 16:01:41 +02:00
// https://github.com/pciutils/pciutils/blob/master/example.c
char buffer[BUF_SIZE];
struct pci_access * pacc = pci_alloc();
pci_init(pacc);
pci_scan_bus(pacc);
struct pci_dev * dev = pacc -> devices;
while (dev) {
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
if (dev -> device_class == 768) {
break;
}
dev = dev -> next;
}
char * model = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE, dev -> vendor_id, dev -> device_id);
pci_cleanup(pacc);
char * vendor = "unknown vendor";
switch(dev -> vendor_id){
case 4098:
vendor = "AMD";
break;
case 4318:
vendor = "NVIDIA";
break;
case 32902:
vendor = "Intel";
break;
}
snprintf(gpu, BUF_SIZE, "%s %s", vendor, model);
2021-05-26 12:23:42 +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-26 12:23:42 +02:00
kern_name = malloc(BUF_SIZE);
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
struct utsname u_info;
uname(&u_info);
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
snprintf(kern_name, BUF_SIZE, "\x1b[36mKERN\x1b[0m -> Linux %s", u_info.release);
2021-05-10 08:25:03 +02:00
2021-05-26 12:23:42 +02:00
return kern_name;
2021-05-10 08:25:03 +02:00
}
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-26 16:01:41 +02:00
uptime = malloc(BUF_SIZE);
unsigned int sec;
short hr, min;
struct sysinfo sys_info;
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
sysinfo(&sys_info);
2021-05-03 21:47:45 +02:00
2021-05-26 16:01:41 +02:00
sec = sys_info.uptime;
hr = (sec/60/60%24);
2021-05-26 12:23:42 +02:00
min = (sec/60%60);
2021-05-10 08:25:03 +02:00
2021-05-26 16:01:41 +02:00
snprintf(uptime, BUF_SIZE, "\x1b[36mUPT\x1b[0m -> %hd h, %hd min", hr, min);
2021-05-26 12:23:42 +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-26 12:23:42 +02:00
static char *get_packages(char *package_count){
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
int pkg_count = 0;
package_count = malloc(BUF_SIZE);
2021-05-03 21:47:45 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
if( strchr(ARCH_BASED, sys) )
pkg_count = count_files(opendir("/var/lib/pacman/local"));
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
else if( strchr(DEBIAN_BASED, sys) )
2021-05-26 16:01:41 +02:00
pkg_count = count_files(opendir("/usr/bin")) + count_files(opendir("/sbin")); // still not sure where to look
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
else{
printf("\n\n\aUnable to find package list\n\n");
exit(EXIT_FAILURE);
}
2021-05-03 21:47:45 +02:00
2021-05-26 12:23:42 +02:00
snprintf(package_count, BUF_SIZE, "\x1b[36mPKGS\x1b[0m -> %d", pkg_count);
2021-05-13 18:43:02 +02:00
2021-05-26 12:23:42 +02:00
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-26 12:23:42 +02:00
int bar_percent;
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
float total,
2021-05-17 19:54:37 +02:00
free_mem,
used,
2021-05-26 12:23:42 +02:00
real_percent;
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
ram_info = malloc(BUF_SIZE * 3);
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
char *line = malloc(BUF_SIZE * 3),
bar[] = "[----------]";
2021-05-10 11:10:06 +02:00
2021-05-26 12:23:42 +02:00
FILE *RAM = fopen("/proc/meminfo", "rt");
2021-05-04 16:46:09 +02:00
2021-05-26 12:23:42 +02:00
2021-05-26 16:01:41 +02:00
// Get total amount of RAM
2021-05-26 12:23:42 +02:00
fgets(line, BUF_SIZE * 3, RAM);
truncate_spaces(line);
sscanf(line, " MemTotal: %f", &total);
2021-05-10 11:10:06 +02:00
2021-05-26 16:01:41 +02:00
// throw away second line of file
2021-05-26 12:23:42 +02:00
fgets(line, BUF_SIZE * 3, RAM);
2021-05-13 02:09:25 +02:00
2021-05-26 16:01:41 +02:00
// Get free memory
2021-05-26 12:23:42 +02:00
fgets(line, BUF_SIZE * 3, RAM);
truncate_spaces(line);
sscanf(line, " MemAvailable: %f", &free_mem);
2021-05-04 16:46:09 +02:00
2021-05-26 12:23:42 +02:00
fclose(RAM);
2021-05-04 16:46:09 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 12:23:42 +02:00
used = total - free_mem;
real_percent = (used / total) * 100;
2021-05-26 16:01:41 +02:00
bar_percent = (real_percent / 10 + 0.25);
2021-05-04 16:46:09 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +02:00
for(int i = 1; i <= bar_percent; i++){
2021-05-26 12:23:42 +02:00
bar[i] = '*';
}
2021-05-10 11:10:06 +02:00
2021-05-17 19:54:37 +02:00
2021-05-26 16:01:41 +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
2021-05-26 12:23:42 +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-26 12:23:42 +02:00
void concatenate_print(char *os, char *cpu_name, char *gpu){
char *uptime = NULL,
*kern_name = NULL,
*ram_info = NULL,
*package_count = NULL;
strcat( (logo + logo_number)->logo[0], os);
strcat( (logo + logo_number)->logo[1], get_kernel(kern_name));
strcat( (logo + logo_number)->logo[2], get_uptime(uptime));
strcat( (logo + logo_number)->logo[3], get_packages(package_count));
strcat( (logo + logo_number)->logo[4], cpu_name);
strcat( (logo + logo_number)->logo[5], gpu);
strcat( (logo + logo_number)->logo[6], get_RAM(ram_info));
2021-05-26 16:01:41 +02:00
printf("\n");
2021-05-26 12:23:42 +02:00
for(int i = 0; i < COLS ; i++)
printf("%s\n", (logo + logo_number)->logo[i]);
free(uptime);
free(ram_info);
free(package_count);
2021-05-26 16:01:41 +02:00
free(kern_name);
2021-05-26 12:23:42 +02:00
}