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 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 (int i = 0; i < pkgman_count; i++) {
|
||||||
ADD_PKGMAN_NAME(apt, "(apt)")
|
struct package_manager *current = &pkgmans[i];
|
||||||
ADD_PKGMAN_NAME(apk, "(apk)")
|
|
||||||
ADD_PKGMAN_NAME(dnf, "(dnf)")
|
FILE *fp = popen(current->command_string, "r");
|
||||||
ADD_PKGMAN_NAME(emerge, "(emerge)")
|
unsigned int pkg_count;
|
||||||
ADD_PKGMAN_NAME(flatpak,"(flatpak)")
|
|
||||||
ADD_PKGMAN_NAME(guix ,"(guix)")
|
if (fscanf(fp, "%u", &pkg_count) == 3) continue;
|
||||||
ADD_PKGMAN_NAME(nix, "(nix)")
|
fclose(fp);
|
||||||
ADD_PKGMAN_NAME(pacman, "(pacman)")
|
|
||||||
ADD_PKGMAN_NAME(rpm, "(rpm)")
|
total += pkg_count;
|
||||||
ADD_PKGMAN_NAME(xbps, "(xbps)")
|
if (pkg_count > 0) strcat(pkgman_name, current->pkgman_name);
|
||||||
#undef ADD_PKGMAN_NAME
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
@ -473,4 +468,4 @@ void uwu_name() { // changes distro name to uwufied(?) name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#undef STRING_TO_UWU
|
#undef STRING_TO_UWU
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue