I'm tired

This commit is contained in:
Pascal Puffke 2021-03-19 19:38:46 +01:00 committed by GitHub
parent a647bfc6ba
commit 9e299dc8a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -89,47 +89,41 @@ int main(int argc, char *argv[]) {
} }
int pkgman() { // this is just a function that returns the total of installed packages int pkgman() { // this is just a function that returns the total of installed packages
int apt, apk, dnf, emerge, flatpak, guix, nix, pacman, rpm, xbps, total = 0; int total = 0;
FILE *file[10]; // when you add a new package manager support, make sure to update the array size. // TODO: should this be at the top of the program? maybe in a config.c file?
file[0] = popen("dpkg-query -f '${binary:Package}\n' -W 2> /dev/null | wc -l", "r"); // TODO: do we need to `free()` this? I have no idea how to do memory management in C...
file[1] = popen("apk info 2> /dev/null | wc -l", "r"); struct package_manager {
file[2] = popen("dnf list installed 2> /dev/null | wc -l", "r"); char command_string[128]; // command to get number of packages installed
file[3] = popen("qlist -I 2> /dev/null | wc -l", "r"); char pkgman_name[16]; // name of the package manager
file[4] = popen("flatpak list 2> /dev/null | wc -l", "r"); };
file[5] = popen("guix package --list-installed 2> /dev/null | wc -l", "r");
file[6] = popen("nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "r");
file[7] = popen("pacman -Qq 2> /dev/null | wc -l", "r");
file[8] = popen("rpm -qa --last 2> /dev/null | wc -l", "r");
file[9] = popen("xbps-query -l 2> /dev/null | wc -l", "r");
// the if statements are there for error handling and for preventing the #27 issue struct package_manager pkgmans[] = {
if (fscanf(file[0], "%d", &apt) == 3) apt = 0; { "apk info 2> /dev/null | wc -l", "(apk)" },
if (fscanf(file[1], "%d", &apk) == 3) apk = 0; { "dnf list installed 2> /dev/null | wc -l", "(dnf)" },
if (fscanf(file[2], "%d", &dnf) == 3) dnf = 0; { "qlist -I 2> /dev/null | wc -l", "(emerge)" },
if (fscanf(file[3], "%d", &emerge) == 3) emerge = 0; { "flatpak list 2> /dev/null | wc -l", "(flatpack)" },
if (fscanf(file[4], "%d", &flatpak) == 3) flatpak = 0; { "guix package --list-installed 2> /dev/null | wc -l", "(guix)" },
if (fscanf(file[5], "%d", &guix) == 3) guix = 0; { "nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "(nix)" },
if (fscanf(file[6], "%d", &nix) == 3) nix = 0; { "pacman -Qq 2> /dev/null | wc -l", "(pacman)" },
if (fscanf(file[7], "%d", &pacman) == 3) pacman = 0; { "rpm -qa --last 2> /dev/null | wc -l", "(rpm)" },
if (fscanf(file[8], "%d", &rpm) == 3) rpm = 0; { "xbps-query -l 2> /dev/null | wc -l", "(xbps)" }
if (fscanf(file[9], "%d", &xbps) == 3) xbps = 0; };
for (int i = 0; i < 8; i++) fclose(file[i]); const unsigned long pkgman_count = sizeof(pkgmans) / sizeof(pkgmans[0]);
#define ADD_PKGMAN_NAME(package_count, pkgman_to_add) if (package_count > 0) { total += package_count; strcat(pkgman_name, pkgman_to_add); } for (long unsigned int i = 0; i < pkgman_count; i++) { // long unsigned int instead of int because of -Wsign-compare
ADD_PKGMAN_NAME(apt, "(apt)") struct package_manager *current = &pkgmans[i];
ADD_PKGMAN_NAME(apk, "(apk)")
ADD_PKGMAN_NAME(dnf, "(dnf)")
ADD_PKGMAN_NAME(emerge, "(emerge)")
ADD_PKGMAN_NAME(flatpak,"(flatpak)")
ADD_PKGMAN_NAME(guix ,"(guix)")
ADD_PKGMAN_NAME(nix, "(nix)")
ADD_PKGMAN_NAME(pacman, "(pacman)")
ADD_PKGMAN_NAME(rpm, "(rpm)")
ADD_PKGMAN_NAME(xbps, "(xbps)")
#undef ADD_PKGMAN_NAME
FILE *fp = popen(current->command_string, "r");
unsigned int pkg_count;
if (fscanf(fp, "%u", &pkg_count) == 3) continue;
fclose(fp);
total += pkg_count;
if (pkg_count > 0) strcat(pkgman_name, current->pkgman_name);
}
return total; return total;
} }
@ -169,7 +163,7 @@ void get_info() { // get all necessary info
char line[256]; // var to scan file lines char line[256]; // var to scan file lines
// terminal width // terminal width
// used for truncating long names // used to truncate long names
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
target_width = win.ws_col - 28; target_width = win.ws_col - 28;
@ -206,7 +200,10 @@ void get_info() { // get all necessary info
// system resources // system resources
uname(&sys_var); uname(&sys_var);
sysinfo(&sys); sysinfo(&sys);
truncate_name(sys_var.release);
truncate_name(sys_var.machine);
// ram // ram
FILE *meminfo = fopen("/proc/meminfo", "r"); FILE *meminfo = fopen("/proc/meminfo", "r");
while (fgets(line, sizeof(line), meminfo)) { while (fgets(line, sizeof(line), meminfo)) {
@ -233,9 +230,9 @@ void get_info() { // get all necessary info
while (fgets(line, sizeof(line), gpu)) if (sscanf(line, "%[^\n]", gpu_model[0])) break; while (fgets(line, sizeof(line), gpu)) if (sscanf(line, "%[^\n]", gpu_model[0])) break;
} }
fclose(gpu); fclose(gpu);
// GPU name shortening // truncate GPU name
for (int i = 0; i < gpun; i++) { for(int i = 0; i < gpun; i++) {
truncate_name(gpu_model[i]); truncate_name(gpu_model[i]);
} }