diff options
author | Connor Lane Smith <cls@lubutu.com> | 2011-05-14 20:43:11 +0100 |
---|---|---|
committer | Connor Lane Smith <cls@lubutu.com> | 2011-05-14 20:43:11 +0100 |
commit | 15505bd711d77fbd88dbdcbc464c78d863e03250 (patch) | |
tree | 41e1d4feb5d75cc7caa750a7c5e498295e1f2344 | |
parent | 86468aafe52a94ce6ba1a3601a587c65724a61aa (diff) |
fast dmenu_path script
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | dmenu.1 | 2 | ||||
-rwxr-xr-x | dmenu_path | 9 | ||||
-rw-r--r-- | dmenu_path.c | 100 |
4 files changed, 13 insertions, 108 deletions
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | include config.mk | 4 | include config.mk |
5 | 5 | ||
6 | all: options dmenu dmenu_path | 6 | all: options dmenu |
7 | 7 | ||
8 | options: | 8 | options: |
9 | @echo dmenu build options: | 9 | @echo dmenu build options: |
@@ -15,22 +15,18 @@ dmenu: dmenu.o draw.o | |||
15 | @echo CC -o $@ | 15 | @echo CC -o $@ |
16 | @${CC} -o $@ dmenu.o draw.o ${LDFLAGS} | 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 | .c.o: config.mk | 18 | .c.o: config.mk |
23 | @echo CC -c $< | 19 | @echo CC -c $< |
24 | @${CC} -c $< ${CFLAGS} | 20 | @${CC} -c $< ${CFLAGS} |
25 | 21 | ||
26 | clean: | 22 | clean: |
27 | @echo cleaning | 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 | dist: clean | 26 | dist: clean |
31 | @echo creating dist tarball | 27 | @echo creating dist tarball |
32 | @mkdir -p dmenu-${VERSION} | 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 | @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} | 30 | @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} |
35 | @gzip dmenu-${VERSION}.tar | 31 | @gzip dmenu-${VERSION}.tar |
36 | @rm -rf dmenu-${VERSION} | 32 | @rm -rf dmenu-${VERSION} |
@@ -42,7 +42,7 @@ is a dmenu script used by dwm which lists programs in the user's PATH and | |||
42 | executes the selected item. | 42 | executes the selected item. |
43 | .P | 43 | .P |
44 | .B dmenu_path | 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 | .SH OPTIONS | 46 | .SH OPTIONS |
47 | .TP | 47 | .TP |
48 | .B \-b | 48 | .B \-b |
diff --git a/dmenu_path b/dmenu_path new file mode 100755 index 0000000..f3a4701 --- /dev/null +++ b/dmenu_path | |||
@@ -0,0 +1,9 @@ | |||
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" | ||
diff --git a/dmenu_path.c b/dmenu_path.c deleted file mode 100644 index 407477a..0000000 --- a/dmenu_path.c +++ /dev/null | |||
@@ -1,100 +0,0 @@ | |||
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 | } | ||