fast dmenu_path script 15505bd7
Connor Lane Smith · 2011-05-14 20:43 4 file(s) · +13 −108
Makefile +3 −7
3 3
4 4
include config.mk
5 5
6 -
all: options dmenu dmenu_path
6 +
all: options dmenu
7 7
8 8
options:
9 9
	@echo dmenu build options:
15 15
	@echo CC -o $@
16 16
	@${CC} -o $@ dmenu.o draw.o ${LDFLAGS}
17 17
18 -
dmenu_path: dmenu_path.o
19 -
	@echo CC -o $@
20 -
	@${CC} -o $@ dmenu_path.o ${LDFLAGS}
21 -
22 18
.c.o: config.mk
23 19
	@echo CC -c $<
24 20
	@${CC} -c $< ${CFLAGS}
25 21
26 22
clean:
27 23
	@echo cleaning
28 -
	@rm -f dmenu dmenu.o draw.o dmenu_path dmenu_path.o dmenu-${VERSION}.tar.gz
24 +
	@rm -f dmenu dmenu.o draw.o dmenu-${VERSION}.tar.gz
29 25
30 26
dist: clean
31 27
	@echo creating dist tarball
32 28
	@mkdir -p dmenu-${VERSION}
33 -
	@cp LICENSE Makefile README config.mk dmenu.1 dmenu.c draw.c draw.h dmenu_path.c dmenu_run dmenu-${VERSION}
29 +
	@cp LICENSE Makefile README config.mk dmenu.1 dmenu.c draw.c draw.h dmenu_path dmenu_run dmenu-${VERSION}
34 30
	@tar -cf dmenu-${VERSION}.tar dmenu-${VERSION}
35 31
	@gzip dmenu-${VERSION}.tar
36 32
	@rm -rf dmenu-${VERSION}
dmenu.1 +1 −1
42 42
executes the selected item.
43 43
.P
44 44
.B dmenu_path
45 -
is a program used by dmenu_run to find and cache a list of executables.
45 +
is a script used by dmenu_run to find and cache a list of executables.
46 46
.SH OPTIONS
47 47
.TP
48 48
.B \-b
dmenu_path (added) +9 −0
1 +
#!/bin/sh
2 +
CACHE=$HOME/.dmenu_cache
3 +
IFS=:
4 +
5 +
if ! test -f "$CACHE" || find $PATH -type d -newer "$CACHE" | grep -q .; then
6 +
	find $PATH -type f \( -perm -1 -o -perm -10 -o -perm -100 \) | sort -u > "$CACHE"
7 +
fi
8 +
9 +
cat "$CACHE"
dmenu_path.c (deleted) +0 −100
1 -
/* See LICENSE file for copyright and license details. */
2 -
#include <dirent.h>
3 -
#include <limits.h>
4 -
#include <stdio.h>
5 -
#include <stdlib.h>
6 -
#include <string.h>
7 -
#include <unistd.h>
8 -
#include <sys/stat.h>
9 -
10 -
static void die(const char *s);
11 -
static int qstrcmp(const void *a, const void *b);
12 -
static void scan(void);
13 -
static int uptodate(void);
14 -
15 -
static char **items = NULL;
16 -
static const char *home, *path;
17 -
18 -
int
19 -
main(void) {
20 -
	if(!(home = getenv("HOME")))
21 -
		die("no $HOME");
22 -
	if(!(path = getenv("PATH")))
23 -
		die("no $PATH");
24 -
	if(chdir(home) < 0)
25 -
		die("chdir failed");
26 -
	if(uptodate()) {
27 -
		execl("/bin/cat", "cat", CACHE, NULL);
28 -
		die("exec failed");
29 -
	}
30 -
	scan();
31 -
	return EXIT_SUCCESS;
32 -
}
33 -
34 -
void
35 -
die(const char *s) {
36 -
	fprintf(stderr, "dmenu_path: %s\n", s);
37 -
	exit(EXIT_FAILURE);
38 -
}
39 -
40 -
int
41 -
qstrcmp(const void *a, const void *b) {
42 -
	return strcmp(*(const char **)a, *(const char **)b);
43 -
}
44 -
45 -
void
46 -
scan(void) {
47 -
	char buf[PATH_MAX];
48 -
	char *dir, *p;
49 -
	size_t i, count;
50 -
	struct dirent *ent;
51 -
	DIR *dp;
52 -
	FILE *cache;
53 -
54 -
	count = 0;
55 -
	if(!(p = strdup(path)))
56 -
		die("strdup failed");
57 -
	for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":")) {
58 -
		if(!(dp = opendir(dir)))
59 -
			continue;
60 -
		while((ent = readdir(dp))) {
61 -
			snprintf(buf, sizeof buf, "%s/%s", dir, ent->d_name);
62 -
			if(ent->d_name[0] == '.' || access(buf, X_OK) < 0)
63 -
				continue;
64 -
			if(!(items = realloc(items, ++count * sizeof *items)))
65 -
				die("malloc failed");
66 -
			if(!(items[count-1] = strdup(ent->d_name)))
67 -
				die("strdup failed");
68 -
		}
69 -
		closedir(dp);
70 -
	}
71 -
	qsort(items, count, sizeof *items, qstrcmp);
72 -
	if(!(cache = fopen(CACHE, "w")))
73 -
		die("open failed");
74 -
	for(i = 0; i < count; i++) {
75 -
		if(i > 0 && !strcmp(items[i], items[i-1]))
76 -
			continue;
77 -
		fprintf(cache,  "%s\n", items[i]);
78 -
		fprintf(stdout, "%s\n", items[i]);
79 -
	}
80 -
	fclose(cache);
81 -
	free(p);
82 -
}
83 -
84 -
int
85 -
uptodate(void) {
86 -
	char *dir, *p;
87 -
	time_t mtime;
88 -
	struct stat st;
89 -
90 -
	if(stat(CACHE, &st) < 0)
91 -
		return 0;
92 -
	mtime = st.st_mtime;
93 -
	if(!(p = strdup(path)))
94 -
		die("strdup failed");
95 -
	for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":"))
96 -
		if(!stat(dir, &st) && st.st_mtime > mtime)
97 -
			return 0;
98 -
	free(p);
99 -
	return 1;
100 -
}