aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile10
-rw-r--r--dmenu.12
-rwxr-xr-xdmenu_path9
-rw-r--r--dmenu_path.c100
4 files changed, 13 insertions, 108 deletions
diff --git a/Makefile b/Makefile
index dd744e1..60e53d1 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
3 3
4include config.mk 4include config.mk
5 5
6all: options dmenu dmenu_path 6all: options dmenu
7 7
8options: 8options:
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
18dmenu_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
26clean: 22clean:
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
30dist: clean 26dist: 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}
diff --git a/dmenu.1 b/dmenu.1
index a4fcfd9..5f138c8 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -42,7 +42,7 @@ is a dmenu script used by dwm which lists programs in the user's PATH and
42executes the selected item. 42executes the selected item.
43.P 43.P
44.B dmenu_path 44.B dmenu_path
45is a program used by dmenu_run to find and cache a list of executables. 45is 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
2CACHE=$HOME/.dmenu_cache
3IFS=:
4
5if ! 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"
7fi
8
9cat "$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
10static void die(const char *s);
11static int qstrcmp(const void *a, const void *b);
12static void scan(void);
13static int uptodate(void);
14
15static char **items = NULL;
16static const char *home, *path;
17
18int
19main(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
34void
35die(const char *s) {
36 fprintf(stderr, "dmenu_path: %s\n", s);
37 exit(EXIT_FAILURE);
38}
39
40int
41qstrcmp(const void *a, const void *b) {
42 return strcmp(*(const char **)a, *(const char **)b);
43}
44
45void
46scan(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
84int
85uptodate(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}