Code cleanup: rewrote pkgmans() to be cleaner
It uses now a `package_manager` struct, and is overall nicer to work with, especially to add new package managers. You now just have to add a new entry to the array.
This commit is contained in:
parent
c3af321196
commit
9e373494fe
1 changed files with 32 additions and 37 deletions
69
uwufetch.c
69
uwufetch.c
|
@ -86,46 +86,41 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
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.
|
||||
file[0] = popen("dpkg-query -f '${binary:Package}\n' -W 2> /dev/null | wc -l", "r");
|
||||
file[1] = popen("apk info 2> /dev/null | wc -l", "r");
|
||||
file[2] = popen("dnf list installed 2> /dev/null | wc -l", "r");
|
||||
file[3] = popen("qlist -I 2> /dev/null | wc -l", "r");
|
||||
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");
|
||||
// TODO: should this be at the top of the program? maybe in a config.c file?
|
||||
// TODO: do we need to `free()` this? I have no idea how to do memory management in C...
|
||||
struct package_manager {
|
||||
char command_string[128]; // command to get number of packages installed
|
||||
char pkgman_name[16]; // name of the package manager
|
||||
};
|
||||
|
||||
// the if statements are there for error handling and for preventing the #27 issue
|
||||
if (fscanf(file[0], "%d", &apt) == 3) apt = 0;
|
||||
if (fscanf(file[1], "%d", &apk) == 3) apk = 0;
|
||||
if (fscanf(file[2], "%d", &dnf) == 3) dnf = 0;
|
||||
if (fscanf(file[3], "%d", &emerge) == 3) emerge = 0;
|
||||
if (fscanf(file[4], "%d", &flatpak) == 3) flatpak = 0;
|
||||
if (fscanf(file[5], "%d", &guix) == 3) guix = 0;
|
||||
if (fscanf(file[6], "%d", &nix) == 3) nix = 0;
|
||||
if (fscanf(file[7], "%d", &pacman) == 3) pacman = 0;
|
||||
if (fscanf(file[8], "%d", &rpm) == 3) rpm = 0;
|
||||
if (fscanf(file[9], "%d", &xbps) == 3) xbps = 0;
|
||||
struct package_manager pkgmans[] = {
|
||||
{ "apk info 2> /dev/null | wc -l", "(apk)" },
|
||||
{ "dnf list installed 2> /dev/null | wc -l", "(dnf)" },
|
||||
{ "qlist -I 2> /dev/null | wc -l", "(emerge)" },
|
||||
{ "flatpak list 2> /dev/null | wc -l", "(flatpack)" },
|
||||
{ "guix package --list-installed 2> /dev/null | wc -l", "(guix)" },
|
||||
{ "nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "(nix)" },
|
||||
{ "pacman -Qq 2> /dev/null | wc -l", "(pacman)" },
|
||||
{ "rpm -qa --last 2> /dev/null | wc -l", "(rpm)" },
|
||||
{ "xbps-query -l 2> /dev/null | wc -l", "(xbps)" },
|
||||
};
|
||||
|
||||
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); }
|
||||
ADD_PKGMAN_NAME(apt, "(apt)")
|
||||
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
|
||||
for (int i = 0; i < pkgman_count; i++) {
|
||||
struct package_manager *current = &pkgmans[i];
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -473,4 +468,4 @@ void uwu_name() { // changes distro name to uwufied(?) name
|
|||
}
|
||||
}
|
||||
#undef STRING_TO_UWU
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue