Reworked quite a lot
This commit is contained in:
parent
167bf81633
commit
6ba54810c1
2
.SRCINFO
2
.SRCINFO
|
@ -1,6 +1,6 @@
|
|||
pkgbase = fetchy-git
|
||||
pkgdesc = Cli system info tool written in C.
|
||||
pkgver = 1.6
|
||||
pkgver = 2.0
|
||||
pkgrel = 1
|
||||
url = https://gitlab.com/vojjvoda/fetchy.git
|
||||
arch = x86_64
|
||||
|
|
2
PKGBUILD
2
PKGBUILD
|
@ -1,6 +1,6 @@
|
|||
# Maintainer: Јован Ђокић-Шумарац <sumarac@protonmail.com>
|
||||
pkgname=fetchy-git
|
||||
pkgver=1.6
|
||||
pkgver=2.0
|
||||
pkgrel=1
|
||||
epoch=
|
||||
pkgdesc="Cli system info tool written in C. "
|
||||
|
|
213
fetchy.c
213
fetchy.c
|
@ -2,28 +2,30 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h> // directory management
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <pci/pci.h>
|
||||
|
||||
#define BUF_SIZE 50
|
||||
#define COLS 8
|
||||
#define ROWS 100
|
||||
|
||||
//for easier checks
|
||||
#define SUPPORTED_OS "xadurm"
|
||||
#define ARCH_BASED "axmr"
|
||||
#define DEBIAN_BASED "du"
|
||||
|
||||
|
||||
|
||||
//ascii art header file
|
||||
#include "logos.h"
|
||||
|
||||
|
||||
|
||||
void truncate_spaces(char *str);
|
||||
// standardish functions
|
||||
void read_line(char x);
|
||||
void cache_info(char *cache_path, char **cpu_name, char **gpu, char *os_table);
|
||||
|
||||
|
||||
void cache_info(char **cpu_name, char **gpu, char *os_table);
|
||||
static int count_files(DIR *package_dir);
|
||||
|
||||
// 'get info' functions
|
||||
static char *get_sys(char *s_os, char *os_name, char *os_table);
|
||||
static char *get_kernel(char *kern_name);
|
||||
static char *get_uptime(char *uptime);
|
||||
|
@ -32,32 +34,34 @@ static char *get_cpu(char *cpuname);
|
|||
static char *get_gpu(char *gpu);
|
||||
static char *get_packages(char *package_count);
|
||||
|
||||
|
||||
// final function
|
||||
void concatenate_print(char *os, char *cpu_name, char *gpu);
|
||||
|
||||
|
||||
// easier if global, don't hate me
|
||||
char sys;
|
||||
int logo_number = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
|
||||
|
||||
char *os_name = NULL,
|
||||
char *os_name = NULL,
|
||||
*cpu_name = NULL,
|
||||
*gpu = NULL,
|
||||
*os = malloc(BUF_SIZE),
|
||||
*gpu = NULL,
|
||||
*os = malloc(BUF_SIZE),
|
||||
*os_table = malloc(BUF_SIZE * 2),
|
||||
*cache_path = malloc(BUF_SIZE * 2),
|
||||
*s_os = SUPPORTED_OS;
|
||||
*s_os = SUPPORTED_OS;
|
||||
|
||||
|
||||
|
||||
cache_info(cache_path, &cpu_name, &gpu, os_table);
|
||||
// if cache file exits, read it, otherwise cache info
|
||||
cache_info(&cpu_name, &gpu, os_table);
|
||||
os = get_sys(s_os, os_name, os_table);
|
||||
|
||||
|
||||
// chack if custom ascii art argument is passed
|
||||
if ( argv[1] != NULL ) {
|
||||
|
||||
if ( strchr(SUPPORTED_OS, *(argv[1] + 1)) )
|
||||
|
@ -71,14 +75,12 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
|
||||
concatenate_print( os, cpu_name, gpu);
|
||||
|
||||
concatenate_print( os, cpu_name, gpu);
|
||||
|
||||
|
||||
free(os_name);
|
||||
free(cpu_name);
|
||||
free(gpu);
|
||||
free(cache_path);
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
@ -87,9 +89,13 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void read_line(char x) {
|
||||
int c;
|
||||
while (( c = getchar()) != x && c != EOF) { }
|
||||
while (( c = getchar()) != x && c != EOF) {}
|
||||
}
|
||||
|
||||
|
||||
|
@ -124,46 +130,53 @@ void truncate_spaces(char *str) {
|
|||
|
||||
|
||||
|
||||
void cache_info(char *cache_path, char **cpu_name, char **gpu, char *os_table) {
|
||||
void cache_info(char **cpu_name, char **gpu, char *os_table) {
|
||||
|
||||
char *cache_path = malloc(BUF_SIZE * 2);
|
||||
FILE *f_cache = NULL;
|
||||
|
||||
// construct path to file
|
||||
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);
|
||||
if( (f_cache = fopen(cache_path, "r")) ){
|
||||
|
||||
char *line = malloc(BUF_SIZE * 4);
|
||||
char *line = malloc(BUF_SIZE * 4);
|
||||
*cpu_name = malloc(BUF_SIZE * 4);
|
||||
*gpu = malloc(BUF_SIZE * 4);
|
||||
|
||||
fgets(line, BUF_SIZE * 4, FCache);
|
||||
//get CPU
|
||||
fgets(line, BUF_SIZE * 4, f_cache);
|
||||
truncate_spaces(line);
|
||||
snprintf(*cpu_name, BUF_SIZE * 4, "\x1b[36mCPU\x1b[0m -> %s", strchr(line, ':') +2);
|
||||
|
||||
fgets(line, BUF_SIZE * 4, FCache);
|
||||
//get GPU
|
||||
fgets(line, BUF_SIZE * 4, f_cache);
|
||||
truncate_spaces(line);
|
||||
snprintf(*gpu, BUF_SIZE * 4, "\x1b[36mGPU\x1b[0m -> %s", strchr(line, ':') +2);
|
||||
|
||||
fgets(os_table, BUF_SIZE * 2, FCache);
|
||||
//get OS table
|
||||
fgets(os_table, BUF_SIZE * 2, f_cache);
|
||||
|
||||
fclose(FCache);
|
||||
fclose(f_cache);
|
||||
free(line);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
FCache = fopen(cache_path, "w");
|
||||
f_cache = fopen(cache_path, "w");
|
||||
|
||||
if( FCache == NULL ){
|
||||
if( f_cache == NULL ){
|
||||
printf("\nCan't open cache file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Write info to file
|
||||
fprintf(f_cache, "CPU : %s\n", get_cpu(*cpu_name));
|
||||
fprintf(f_cache, "GPU : %s\n", get_gpu(*gpu));
|
||||
fprintf(f_cache, "a:Arch|x:Artix|r:Arco|m:Manjaro|u:Ubuntu|d:Debian\n");
|
||||
|
||||
fprintf(FCache, "CPU : %s\n", get_cpu(*cpu_name));
|
||||
fprintf(FCache, "GPU : %s\n", get_gpu(*gpu));
|
||||
fprintf(FCache, "a:Arch|x:Artix|r:Arco|m:Manjaro|u:Ubuntu|d:Debian\n");
|
||||
|
||||
fclose(FCache);
|
||||
fclose(f_cache);
|
||||
|
||||
|
||||
|
||||
|
@ -173,8 +186,9 @@ void cache_info(char *cache_path, char **cpu_name, char **gpu, char *os_table) {
|
|||
printf("run program normally. Enjoy!\n\n");
|
||||
|
||||
|
||||
cache_info(cache_path, cpu_name, gpu, os_table);
|
||||
cache_info(cpu_name, gpu, os_table);
|
||||
}
|
||||
free(cache_path);
|
||||
}
|
||||
|
||||
|
||||
|
@ -201,29 +215,30 @@ static char *get_sys(char *s_os, char *os_name, char *os_table) {
|
|||
|
||||
os_name = malloc(BUF_SIZE);
|
||||
char *name = malloc(BUF_SIZE);
|
||||
FILE *sysName = fopen("/etc/issue", "rt");
|
||||
FILE *f_sys_name = fopen("/etc/issue", "rt");
|
||||
|
||||
|
||||
if( sysName == NULL ){
|
||||
if( f_sys_name == NULL ){
|
||||
fprintf(stderr, "\nCan't get os name\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
fscanf(sysName, "%s ", name);
|
||||
fclose(sysName);
|
||||
fscanf(f_sys_name, "%s %*s", name);
|
||||
fclose(f_sys_name);
|
||||
truncate_spaces(name);
|
||||
|
||||
|
||||
sys = *(strstr(os_table, name) - 2);
|
||||
|
||||
logo_number = (int) (strchr(s_os, sys) - s_os);
|
||||
|
||||
|
||||
if( sys == 'r' )
|
||||
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s", name);
|
||||
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s", name); // Arco has a retarded name to be honest, without a space
|
||||
else
|
||||
snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s Linux", name);
|
||||
|
||||
|
||||
free(name);
|
||||
return os_name;
|
||||
}
|
||||
|
@ -243,13 +258,15 @@ static char *get_cpu(char *cpu_name){
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
// Skip first four lines, cpu info is on fifth
|
||||
for(int i = 0; i < 5; i++)
|
||||
fgets(line, BUF_SIZE * 4, cpu);
|
||||
|
||||
|
||||
snprintf(cpu_name, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
|
||||
truncate_spaces(cpu_name);
|
||||
|
||||
|
||||
fclose(cpu);
|
||||
free(line);
|
||||
|
||||
|
@ -261,19 +278,43 @@ static char *get_cpu(char *cpu_name){
|
|||
|
||||
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");
|
||||
gpu = malloc(BUF_SIZE);
|
||||
|
||||
fscanf(gpuName, "%[^\n]%s", gpu);
|
||||
// https://github.com/pciutils/pciutils/blob/master/example.c
|
||||
char buffer[BUF_SIZE];
|
||||
struct pci_access * pacc = pci_alloc();
|
||||
|
||||
truncate_spaces(gpu);
|
||||
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;
|
||||
}
|
||||
|
||||
fclose(gpuName);
|
||||
char * model = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE, dev -> vendor_id, dev -> device_id);
|
||||
|
||||
pci_cleanup(pacc);
|
||||
|
||||
if(strstr(gpu, "VGA"))
|
||||
strcpy(gpu, "CPU's integrated graphics");
|
||||
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);
|
||||
|
||||
return gpu;
|
||||
}
|
||||
|
@ -284,22 +325,11 @@ static char *get_gpu(char *gpu){
|
|||
static char *get_kernel(char *kern_name){
|
||||
|
||||
kern_name = malloc(BUF_SIZE);
|
||||
char *kernel = malloc(BUF_SIZE);
|
||||
FILE *kInfo = fopen("/proc/version", "rt");
|
||||
|
||||
if( kInfo == NULL ){
|
||||
printf("\nCan't get kernel info\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
struct utsname u_info;
|
||||
uname(&u_info);
|
||||
|
||||
|
||||
|
||||
fscanf(kInfo, "Linux version %s ", kernel);
|
||||
fclose(kInfo);
|
||||
|
||||
truncate_spaces(kernel);
|
||||
snprintf(kern_name, BUF_SIZE + 5, "\x1b[36mKERN\x1b[0m -> Linux %s", kernel);
|
||||
free(kernel);
|
||||
snprintf(kern_name, BUF_SIZE, "\x1b[36mKERN\x1b[0m -> Linux %s", u_info.release);
|
||||
|
||||
return kern_name;
|
||||
}
|
||||
|
@ -309,28 +339,18 @@ static char *get_kernel(char *kern_name){
|
|||
|
||||
static char *get_uptime(char *uptime){
|
||||
|
||||
int sec,
|
||||
hr,
|
||||
min;
|
||||
uptime = malloc(BUF_SIZE);
|
||||
unsigned int sec;
|
||||
short hr, min;
|
||||
struct sysinfo sys_info;
|
||||
|
||||
FILE *FUp = fopen("/proc/uptime", "rt");
|
||||
uptime = malloc(BUF_SIZE);
|
||||
sysinfo(&sys_info);
|
||||
|
||||
|
||||
if( FUp == NULL ){
|
||||
printf("\nCan't get uptime info\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
fscanf(FUp, "%d", &sec);
|
||||
fclose(FUp);
|
||||
|
||||
hr = (sec/60/60%24);
|
||||
sec = sys_info.uptime;
|
||||
hr = (sec/60/60%24);
|
||||
min = (sec/60%60);
|
||||
|
||||
snprintf(uptime, BUF_SIZE, "\x1b[36mUPT\x1b[0m -> %dh, %dmin", hr, min);
|
||||
snprintf(uptime, BUF_SIZE, "\x1b[36mUPT\x1b[0m -> %hd h, %hd min", hr, min);
|
||||
return uptime;
|
||||
}
|
||||
|
||||
|
@ -347,7 +367,7 @@ static char *get_packages(char *package_count){
|
|||
pkg_count = count_files(opendir("/var/lib/pacman/local"));
|
||||
|
||||
else if( strchr(DEBIAN_BASED, sys) )
|
||||
pkg_count = count_files(opendir("/usr/bin")) + count_files(opendir("/sbin"));
|
||||
pkg_count = count_files(opendir("/usr/bin")) + count_files(opendir("/sbin")); // still not sure where to look
|
||||
|
||||
|
||||
|
||||
|
@ -382,13 +402,15 @@ static char *get_RAM(char *ram_info){
|
|||
FILE *RAM = fopen("/proc/meminfo", "rt");
|
||||
|
||||
|
||||
|
||||
// Get total amount of RAM
|
||||
fgets(line, BUF_SIZE * 3, RAM);
|
||||
truncate_spaces(line);
|
||||
sscanf(line, " MemTotal: %f", &total);
|
||||
|
||||
// throw away second line of file
|
||||
fgets(line, BUF_SIZE * 3, RAM);
|
||||
|
||||
// Get free memory
|
||||
fgets(line, BUF_SIZE * 3, RAM);
|
||||
truncate_spaces(line);
|
||||
sscanf(line, " MemAvailable: %f", &free_mem);
|
||||
|
@ -398,15 +420,17 @@ static char *get_RAM(char *ram_info){
|
|||
|
||||
used = total - free_mem;
|
||||
real_percent = (used / total) * 100;
|
||||
bar_percent = (real_percent / 10 + 0.35);
|
||||
bar_percent = (real_percent / 10 + 0.25);
|
||||
|
||||
|
||||
for( int i = 1; i <= bar_percent; i++){
|
||||
for(int i = 1; i <= bar_percent; 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);
|
||||
return ram_info;
|
||||
|
@ -431,14 +455,13 @@ void concatenate_print(char *os, char *cpu_name, char *gpu){
|
|||
strcat( (logo + logo_number)->logo[5], gpu);
|
||||
strcat( (logo + logo_number)->logo[6], get_RAM(ram_info));
|
||||
|
||||
|
||||
|
||||
printf("\n");
|
||||
for(int i = 0; i < COLS ; i++)
|
||||
printf("%s\n", (logo + logo_number)->logo[i]);
|
||||
|
||||
|
||||
free(uptime);
|
||||
free(ram_info);
|
||||
free(kern_name);
|
||||
free(package_count);
|
||||
free(kern_name);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue