From d39eee7a5e6f66289f91c0519f92203dd1b7469e Mon Sep 17 00:00:00 2001 From: Riley Beckett Date: Mon, 20 Oct 2025 17:12:32 -0400 Subject: generate list of executables and cache faster --- swenu-exes.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 swenu-exes.c (limited to 'swenu-exes.c') diff --git a/swenu-exes.c b/swenu-exes.c new file mode 100644 index 0000000..07acda9 --- /dev/null +++ b/swenu-exes.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) { + char* pathenv = getenv("PATH"); + if (!pathenv) { + fprintf(stderr, "Failed to find PATH enviroment variable\n"); + return 1; + } + + if (argc == 2) { + struct stat file; + if (stat(argv[1], &file) == -1) { + return 0; + } + char* dir = NULL; + for (dir = strtok(pathenv, ":"); (dir = strtok(NULL, ":"));) { + struct stat buf; + if (stat(dir, &buf) == -1) { + fprintf(stderr, "%s: %s\n", strerror(errno), argv[1]); + continue; + } + if (S_ISDIR(buf.st_mode) && buf.st_mtime > file.st_mtime) { + return 0; + } + } + return 1; + } + + char path[PATH_MAX]; + + char* dir = NULL; + for (dir = strtok(pathenv, ":"); (dir = strtok(NULL, ":"));) { + DIR* dirfd = opendir(dir); + if (!dirfd) { + fprintf(stderr, "%s: %s\n", strerror(errno), dir); + continue; + } + + struct dirent* d = NULL; + while ((d = readdir(dirfd))) { + int r = snprintf(path, sizeof(path), "%s/%s", dir, d->d_name); + if (r <= 0 || r >= PATH_MAX) continue; + struct stat buf; + if (stat(path, &buf) == -1) { + fprintf(stderr, "%s: %s\n", strerror(errno), path); + continue; + } + if (S_ISREG(buf.st_mode) && !access(path, X_OK)) { + puts(d->d_name); + } + } + closedir(dirfd); + } + return 0; +} -- cgit v1.2.3