diff --git a/src/main.c b/src/main.c index 03901d6..0ae0c61 100644 --- a/src/main.c +++ b/src/main.c @@ -15,8 +15,6 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) -static const char* pac_mans[] = PAC_MANS; - /// Gets the size of the current terminal window. /// /// @param width @@ -115,22 +113,8 @@ int main(int argc, char** argv) { mem_unit = unit_from_power(e, base_1024); - DIR* dir = opendir("/bin"); - - if (dir) { - struct dirent* dirent; - unsigned int prev_pac = 0 - 1; - - while ((dirent = readdir(dir))) { - for (unsigned int i = 0; i < sizeof(pac_mans) / sizeof(*pac_mans); i++) { - if (!strcmp(dirent->d_name, pac_mans[i]) && i < prev_pac) { - pac_man = pac_mans[i]; - prev_pac = i; - } - } - } - } - + pac_man = get_pac_man(); + // Currently excludes memory and storage longest_entry_len = MAX( MAX( diff --git a/src/pac_man.c b/src/pac_man.c new file mode 100644 index 0000000..eb9daf4 --- /dev/null +++ b/src/pac_man.c @@ -0,0 +1,60 @@ +// Copyright (c) 2023 Evan Overman (https://an-prata.it). Licensed under the MIT License. +// See LICENSE file in repository root for complete license text. + +#include +#include +#include +#include +#include +#include "pac_man.h" + +static const char* pac_mans[] = PAC_MANS; + +static char* get_sub_str(char* str, char delim, size_t len) { + for (unsigned int i = 0; i < len; i++) { + if (!str[i]) { + if (i + 1 < len) { + str = &str[i + 1]; + len -= i; + i = 0; + } + + continue; + } + + if (str[i] == delim) { + str[i] = '\0'; + puts(str); + return str; + } + } + + puts(str); + return NULL; +} + +const char* get_pac_man() { + const char* pac_man, * current_dir; + char* path = getenv("PATH"); + size_t path_len = strlen(path); + + while ((current_dir = get_sub_str(path, ':', path_len))) { + DIR* dir = opendir(current_dir); + + if (dir) { + struct dirent* dirent; + unsigned int prev_pac = 0 - 1; + + while ((dirent = readdir(dir))) { + for (unsigned int i = 0; i < sizeof(pac_mans) / sizeof(*pac_mans); i++) { + if (!strcmp(dirent->d_name, pac_mans[i]) && i < prev_pac) { + pac_man = pac_mans[i]; + prev_pac = i; + } + } + } + } + } + + return pac_man; +} diff --git a/src/pac_man.h b/src/pac_man.h index 23f2fa7..00da469 100644 --- a/src/pac_man.h +++ b/src/pac_man.h @@ -12,4 +12,6 @@ "apt" \ } +const char* get_pac_man(); + #endif // PAC_MAN_H