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 --- .gitignore | 1 + Makefile | 25 ++++++++++++++++--------- array.h | 1 + swenu-exes.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ swenu-path | 14 ++++++++++---- 5 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 swenu-exes.c diff --git a/.gitignore b/.gitignore index 96bdd0c..2c3cd6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ swenu +swenu-exes objs/ diff --git a/Makefile b/Makefile index fcd9772..d5b6a44 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +PREFIX=/usr BIN=swenu CC=gcc OBJDIR=objs @@ -8,13 +9,13 @@ VPATH=/usr/share/wayland-protocols/staging/cursor-shape/:/usr/share/wayland-prot WLPROT=cursor-shape-v1.xml tablet-v2.xml wlr-layer-shell-unstable-v1.xml xdg-shell.xml WLC=$(patsubst %.xml,$(OBJDIR)/%.c, $(WLPROT)) WLH=$(patsubst %.xml,$(OBJDIR)/%.h, $(WLPROT)) -SRC=$(WLC) $(wildcard *.c) $(wildcard glad/*.c) +SRC=$(WLC) $(filter-out swenu-exes.c , $(wildcard *.c)) $(wildcard glad/*.c) OBJ=$(patsubst %.c,$(OBJDIR)/%.o, $(notdir $(SRC))) HDEPS=state.h wayland.h config.h -.SILENT: $(OBJ) $(WLC) $(WLH) $(BIN) $(OBJDIR) compile_flags +.SILENT: $(OBJ) $(WLC) $(WLH) $(BIN) $(OBJDIR) compile_flags swenu-exes $(OBJDIR)/swenu-exes.o -all: $(OBJDIR) $(BIN) +all: $(OBJDIR) $(BIN) swenu-exes $(OBJDIR): [ -d $@ ] || mkdir -p $@ @@ -23,6 +24,10 @@ $(BIN): $(OBJ) printf " LD %s\n" $@ $(CC) $(LDFLAGS) $^ -o $@ +swenu-exes: $(OBJDIR)/swenu-exes.o + printf " LD %s\n" $@ + $(CC) $(LDFLAGS) $^ -o $@ + $(OBJDIR)/%.o: %.c %.h $(HDEPS) printf " CC %s\n" $@ $(CC) $(CFLAGS) -c $< -o $@ @@ -52,14 +57,16 @@ clean: rm -rf $(BIN) $(OBJDIR) install: all - install -m 755 ./$(BIN) $(PREFIX)/usr/bin - install -m 755 ./swenu-run $(PREFIX)/usr/bin - install -m 755 ./swenu-path $(PREFIX)/usr/bin + install -m 755 ./$(BIN) $(PREFIX)/bin + install -m 755 ./swenu-run $(PREFIX)/bin + install -m 755 ./swenu-path $(PREFIX)/bin + install -m 755 ./swenu-exes $(PREFIX)/bin uninstall: - rm -f $(PREFIX)/usr/bin/$(BIN) - rm -f $(PREFIX)/usr/bin/swenu-run - rm -f $(PREFIX)/usr/bin/swenu-path + rm -f $(PREFIX)/bin/$(BIN) + rm -f $(PREFIX)/bin/swenu-run + rm -f $(PREFIX)/bin/swenu-path + rm -f $(PREFIX)/bin/swenu-exes run: all ./swenu-run.sh diff --git a/array.h b/array.h index 3c410e2..4975261 100644 --- a/array.h +++ b/array.h @@ -1,6 +1,7 @@ #ifndef ARRAY_H_ #define ARRAY_H_ #include +#include typedef struct { size_t size; 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; +} diff --git a/swenu-path b/swenu-path index a720d5c..fb58b5b 100755 --- a/swenu-path +++ b/swenu-path @@ -1,6 +1,12 @@ #!/bin/sh -IFS=: -for dir in $PATH; do - [ -e ${dir} ] && find ${dir} -executable -printf '%f\n' -done | sort -u +cachedir="${XDG_CACHE_HOME:-"$HOME/.cache"}" +cache="$cachedir/swenu-run" + +[ ! -e "$cachedir" ] && mkdir -p "$cachedir" + +if swenu-exes "$cache"; then + swenu-exes | sort -u | tee $cache +else + cat $cache +fi -- cgit v1.2.3