1 /* See LICENSE file for copyright and license details. */
10 #define CACHE ".dmenu_cache"
12 static void die(const char *s);
13 static int qstrcmp(const void *a, const void *b);
14 static void scan(void);
15 static int uptodate(void);
17 static char **items = NULL;
18 static const char *home, *path;
22 if(!(home = getenv("HOME")))
24 if(!(path = getenv("PATH")))
29 execlp("cat", "cat", CACHE, NULL);
38 fprintf(stderr, "dmenu_path: %s\n", s);
43 qstrcmp(const void *a, const void *b) {
44 return strcmp(*(const char **)a, *(const char **)b);
57 if(!(p = strdup(path)))
59 for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":")) {
60 if(!(dp = opendir(dir)))
62 while((ent = readdir(dp))) {
63 snprintf(buf, sizeof buf, "%s/%s", dir, ent->d_name);
64 if(ent->d_name[0] == '.' || access(buf, X_OK) < 0)
66 if(!(items = realloc(items, ++count * sizeof *items)))
68 if(!(items[count-1] = strdup(ent->d_name)))
73 qsort(items, count, sizeof *items, qstrcmp);
74 if(!(cache = fopen(CACHE, "w")))
76 for(i = 0; i < count; i++) {
77 if(i > 0 && !strcmp(items[i], items[i-1]))
79 fprintf(cache, "%s\n", items[i]);
80 fprintf(stdout, "%s\n", items[i]);
92 if(stat(CACHE, &st) < 0)
95 if(!(p = strdup(path)))
97 for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":"))
98 if(!stat(dir, &st) && st.st_mtime > mtime)