mirror of
https://github.com/arthur-pbty/optifetch.git
synced 2026-08-01 20:29:33 +02:00
fix make & mise au propre du projet
This commit is contained in:
@@ -1,21 +1,31 @@
|
||||
#include "optifetch.h"
|
||||
|
||||
static void read_file(const char *path, char *buf, size_t size) {
|
||||
static void read_file(const char *path, char *buf, size_t size)
|
||||
{
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) { if(size>0) buf[0] = '\0'; return; }
|
||||
if (!f)
|
||||
{
|
||||
if (size > 0)
|
||||
buf[0] = '\0';
|
||||
return;
|
||||
}
|
||||
size_t n = fread(buf, 1, size - 1, f);
|
||||
buf[n] = '\0';
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static void extract_value(const char *buf, const char *key, char *out, size_t size) {
|
||||
static void extract_value(const char *buf, const char *key, char *out, size_t size)
|
||||
{
|
||||
out[0] = '\0';
|
||||
const char *p = strstr(buf, key);
|
||||
if (p) {
|
||||
if (p)
|
||||
{
|
||||
p += strlen(key);
|
||||
while (*p == ' ' || *p == '\t' || *p == ':' || *p == '=' || *p == '"') p++;
|
||||
while (*p == ' ' || *p == '\t' || *p == ':' || *p == '=' || *p == '"')
|
||||
p++;
|
||||
size_t i = 0;
|
||||
while (p[i] && p[i] != '\n' && p[i] != '"' && i < size - 1) {
|
||||
while (p[i] && p[i] != '\n' && p[i] != '"' && i < size - 1)
|
||||
{
|
||||
out[i] = p[i];
|
||||
i++;
|
||||
}
|
||||
@@ -23,17 +33,19 @@ static void extract_value(const char *buf, const char *key, char *out, size_t si
|
||||
}
|
||||
}
|
||||
|
||||
static void clean_newline(char *str) {
|
||||
static void clean_newline(char *str)
|
||||
{
|
||||
str[strcspn(str, "\n")] = 0;
|
||||
}
|
||||
|
||||
void get_info(SysInfo *info) {
|
||||
void get_info(SysInfo *info)
|
||||
{
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
snprintf(info->user, sizeof(info->user), "%s", pw ? pw->pw_name : "unknown");
|
||||
gethostname(info->hostname, sizeof(info->hostname));
|
||||
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
|
||||
read_file("/etc/os-release", buf, sizeof(buf));
|
||||
extract_value(buf, "PRETTY_NAME", info->os, sizeof(info->os));
|
||||
extract_value(buf, "ID", info->os_id, sizeof(info->os_id));
|
||||
@@ -66,7 +78,8 @@ void get_info(SysInfo *info) {
|
||||
|
||||
read_file("/sys/class/dmi/id/product_name", info->host_model, sizeof(info->host_model));
|
||||
clean_newline(info->host_model);
|
||||
if (strlen(info->host_model) == 0) snprintf(info->host_model, sizeof(info->host_model), "Unknown");
|
||||
if (strlen(info->host_model) == 0)
|
||||
snprintf(info->host_model, sizeof(info->host_model), "Unknown");
|
||||
|
||||
info->ram_total_mb = s_info.totalram * s_info.mem_unit / (1024 * 1024);
|
||||
info->ram_used_mb = (s_info.totalram - s_info.freeram) * s_info.mem_unit / (1024 * 1024);
|
||||
@@ -74,14 +87,23 @@ void get_info(SysInfo *info) {
|
||||
info->swap_used_mb = (s_info.totalswap - s_info.freeswap) * s_info.mem_unit / (1024 * 1024);
|
||||
|
||||
struct statvfs vfs;
|
||||
if (statvfs("/", &vfs) == 0) {
|
||||
if (statvfs("/", &vfs) == 0)
|
||||
{
|
||||
info->disk_total_mb = (vfs.f_blocks * vfs.f_frsize) / (1024 * 1024);
|
||||
info->disk_used_mb = info->disk_total_mb - ((vfs.f_bfree * vfs.f_frsize) / (1024 * 1024));
|
||||
} else {
|
||||
info->disk_total_mb = 0; info->disk_used_mb = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
info->disk_total_mb = 0;
|
||||
info->disk_used_mb = 0;
|
||||
}
|
||||
FILE *fp_fs = popen("findmnt -no FSTYPE / 2>/dev/null", "r");
|
||||
if (fp_fs) { if (fgets(info->disk_fs, sizeof(info->disk_fs), fp_fs)) clean_newline(info->disk_fs); pclose(fp_fs); }
|
||||
if (fp_fs)
|
||||
{
|
||||
if (fgets(info->disk_fs, sizeof(info->disk_fs), fp_fs))
|
||||
clean_newline(info->disk_fs);
|
||||
pclose(fp_fs);
|
||||
}
|
||||
|
||||
const char *de = getenv("XDG_CURRENT_DESKTOP");
|
||||
const char *wm = getenv("DESKTOP_SESSION");
|
||||
@@ -93,42 +115,76 @@ void get_info(SysInfo *info) {
|
||||
|
||||
info->gpu[0] = '\0';
|
||||
FILE *fdrm = popen("cat /sys/class/drm/card*/device/uevent 2>/dev/null | grep DRIVER | head -n 1", "r");
|
||||
if (fdrm) {
|
||||
if (fdrm)
|
||||
{
|
||||
char drmbuf[256];
|
||||
if (fgets(drmbuf, sizeof(drmbuf), fdrm)) {
|
||||
if (fgets(drmbuf, sizeof(drmbuf), fdrm))
|
||||
{
|
||||
extract_value(drmbuf, "DRIVER", info->gpu, sizeof(info->gpu));
|
||||
if(info->gpu[0] >= 'a' && info->gpu[0] <= 'z') info->gpu[0] -= 32;
|
||||
if (info->gpu[0] >= 'a' && info->gpu[0] <= 'z')
|
||||
info->gpu[0] -= 32;
|
||||
}
|
||||
pclose(fdrm);
|
||||
}
|
||||
|
||||
FILE *fp_th = popen("gsettings get org.gnome.desktop.interface gtk-theme 2>/dev/null | tr -d \"'\"", "r");
|
||||
if (fp_th) { if (fgets(info->theme, sizeof(info->theme), fp_th)) clean_newline(info->theme); pclose(fp_th); }
|
||||
if (fp_th)
|
||||
{
|
||||
if (fgets(info->theme, sizeof(info->theme), fp_th))
|
||||
clean_newline(info->theme);
|
||||
pclose(fp_th);
|
||||
}
|
||||
FILE *fp_fn = popen("gsettings get org.gnome.desktop.interface font-name 2>/dev/null | tr -d \"'\"", "r");
|
||||
if (fp_fn) { if (fgets(info->font, sizeof(info->font), fp_fn)) clean_newline(info->font); pclose(fp_fn); }
|
||||
if (fp_fn)
|
||||
{
|
||||
if (fgets(info->font, sizeof(info->font), fp_fn))
|
||||
clean_newline(info->font);
|
||||
pclose(fp_fn);
|
||||
}
|
||||
|
||||
int pkg_count = 0;
|
||||
DIR *d; struct dirent *dir;
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
d = opendir("/var/lib/pacman/local");
|
||||
if (d) { while ((dir = readdir(d)) != NULL) if (dir->d_type == DT_DIR && dir->d_name[0] != '.') pkg_count++; closedir(d); }
|
||||
else {
|
||||
if (d)
|
||||
{
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
if (dir->d_type == DT_DIR && dir->d_name[0] != '.')
|
||||
pkg_count++;
|
||||
closedir(d);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *fp = popen("ls /var/lib/dpkg/info 2>/dev/null | grep -c '.list$' ; rpm -qa 2>/dev/null | wc -l", "r");
|
||||
if(fp) { char out[32]; if(fgets(out, sizeof(out), fp)) pkg_count = atoi(out); pclose(fp); }
|
||||
if (fp)
|
||||
{
|
||||
char out[32];
|
||||
if (fgets(out, sizeof(out), fp))
|
||||
pkg_count = atoi(out);
|
||||
pclose(fp);
|
||||
}
|
||||
}
|
||||
snprintf(info->packages, sizeof(info->packages), "%d", pkg_count);
|
||||
|
||||
char bat_path[256] = "";
|
||||
if (access("/sys/class/power_supply/BAT0/capacity", F_OK) == 0) snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT0");
|
||||
else if (access("/sys/class/power_supply/BAT1/capacity", F_OK) == 0) snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT1");
|
||||
if (strlen(bat_path) > 0) {
|
||||
if (access("/sys/class/power_supply/BAT0/capacity", F_OK) == 0)
|
||||
snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT0");
|
||||
else if (access("/sys/class/power_supply/BAT1/capacity", F_OK) == 0)
|
||||
snprintf(bat_path, sizeof(bat_path), "/sys/class/power_supply/BAT1");
|
||||
if (strlen(bat_path) > 0)
|
||||
{
|
||||
char cap_path[512], stat_path[512];
|
||||
snprintf(cap_path, sizeof(cap_path), "%s/capacity", bat_path);
|
||||
snprintf(stat_path, sizeof(stat_path), "%s/status", bat_path);
|
||||
read_file(cap_path, buf, sizeof(buf)); clean_newline(buf);
|
||||
read_file(cap_path, buf, sizeof(buf));
|
||||
clean_newline(buf);
|
||||
snprintf(info->battery, sizeof(info->battery), "%s%%", buf);
|
||||
read_file(stat_path, buf, sizeof(buf)); clean_newline(buf);
|
||||
read_file(stat_path, buf, sizeof(buf));
|
||||
clean_newline(buf);
|
||||
snprintf(info->battery_status, sizeof(info->battery_status), "%s", buf);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(info->battery, sizeof(info->battery), "N/A");
|
||||
snprintf(info->battery_status, sizeof(info->battery_status), "N/A");
|
||||
}
|
||||
@@ -140,14 +196,19 @@ void get_info(SysInfo *info) {
|
||||
|
||||
info->ip_local[0] = '\0';
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
if (getifaddrs(&ifaddr) == 0) {
|
||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr == NULL) continue;
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||
if (getifaddrs(&ifaddr) == 0)
|
||||
{
|
||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
|
||||
{
|
||||
if (ifa->ifa_addr == NULL)
|
||||
continue;
|
||||
if (ifa->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *sa = (struct sockaddr_in *)ifa->ifa_addr;
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &sa->sin_addr, ip, INET_ADDRSTRLEN);
|
||||
if (strcmp(ip, "127.0.0.1") != 0) {
|
||||
if (strcmp(ip, "127.0.0.1") != 0)
|
||||
{
|
||||
snprintf(info->ip_local, sizeof(info->ip_local), "%s (%s)", ip, ifa->ifa_name);
|
||||
break;
|
||||
}
|
||||
@@ -161,9 +222,19 @@ void get_info(SysInfo *info) {
|
||||
|
||||
info->wifi[0] = '\0';
|
||||
FILE *fp_wf = popen("iwgetid -r 2>/dev/null", "r");
|
||||
if (fp_wf) { if (fgets(info->wifi, sizeof(info->wifi), fp_wf)) clean_newline(info->wifi); pclose(fp_wf); }
|
||||
if (fp_wf)
|
||||
{
|
||||
if (fgets(info->wifi, sizeof(info->wifi), fp_wf))
|
||||
clean_newline(info->wifi);
|
||||
pclose(fp_wf);
|
||||
}
|
||||
|
||||
info->ip_public[0] = '\0';
|
||||
FILE *fp_ip = popen("curl -s --max-time 2 ifconfig.me 2>/dev/null", "r");
|
||||
if (fp_ip) { if (fgets(info->ip_public, sizeof(info->ip_public), fp_ip)) clean_newline(info->ip_public); pclose(fp_ip); }
|
||||
if (fp_ip)
|
||||
{
|
||||
if (fgets(info->ip_public, sizeof(info->ip_public), fp_ip))
|
||||
clean_newline(info->ip_public);
|
||||
pclose(fp_ip);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user